#include <iostream>
#include <unistd.h>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
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>(i + hWind,j + hWind);
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<ImgHeight && q >=0 && q < ImgWidth)
{
if(!(p == i + hWind && q==j + hWind))
{
neighbor = Img.at<uchar>(p,q);
if(neighbor>center)
{
rank1 = rank1*2;
}
else
{
rank1 = rank1*2+1;
rank_num++;
}
//cout << "rank1=" << static_cast<int>(rank1) << endl;
}
}
}
}
img_rank.at<uchar>(i+hWind,j+hWind) = rank_num;
}
}
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变换后的结果:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)