#include
#include
#include
#include
#include
using namespace std;
using namespace cv;
Mat Rank_transform(Mat Img, int rows, int cols);
Mat Rank_transform(Mat Img, int rows, int cols )
{
int start ,end;
int hWind = 1; //窗口大小为3
int ImgHeight = Img.rows;
int ImgWidth = Img.cols;
int rank_num=0;
//start = getTickCout();
Mat img_rank = Mat(Img.rows,Img.cols,CV_8UC1,Scalar::all(0));
uchar center = 0;
for(int i=0;i < ImgHeight - hWind;i++)
{
for(int j=0;j< ImgWidth - hWind;j++)
{
center = Img.at
uchar rank1 = 0;
uchar neighbor = 0;
rank_num=0;//rank变换结果为1的个数
for(int p = i;p<=i+2*hWind;p++)
{
for(int q=0;q<=j+2*hWind;q++)
{
if(p>=0 && p=0 && q < ImgWidth)
{
if(!(p == i + hWind && q==j + hWind))
{
neighbor = Img.at
if(neighbor>center)
{
rank1 = rank1*2;
}
else
{
rank1 = rank1*2+1;
rank_num++;
}
//cout << "rank1=" << static_cast
}
}
}
}
img_rank.at
}
}
return img_rank;
}
int main()
{
Mat left =imread ("im2.png",0);
cout << "读取照片成功" << endl;
cout << "开始计算!" << endl;
Mat result = Rank_transform(left,left.rows,left.cols);
imwrite( "rank3.png", result );
cout << "保存完成" << endl;
waitKey(0);
pause();
return 0;
}
在这里窗口大小为3,不知道为啥窗口大小设为5的时候反而没有大小为3的效果好,还需进一步学习。代码的思路就是统计rank变换之后像素值为1的个数,然后将它设为窗口中心像素点的像素值。
结果图:
Rank变换后的结果:



