Fast算子的原理很好懂,参考文章,原本是python实现的,改成c++实现,不是完整的实现,作为一个简化版本,很好理解,先把代码放上来。
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
#define WINDOW_NAME "IMAGE"
int thresh = 50;
int max_thresh = 255;
Mat srcimage,image_gray;
Mat rgb2gray(Mat image)
{
//转化为灰度图
int rows = image.rows;
int cols = image.cols;
Mat grayscale = Mat(image.size(),CV_8U);
// for(int i=0; i<rows; i++)
// {
// const float* src_ptr = image.ptr<float>(i);
// float* dst_ptr = grayscale.ptr<float>(i);
// for(int j=0; j<cols; j++)
// {
// auto red = src_ptr[j * 3];
// auto green = src_ptr[j * 3 +1];
// auto blue = src_ptr[j * 3 +2];
// dst_ptr[j] = float(0.3*red + 0.59*green + 0.11*blue);
// }
// }
for(int i=0; i<rows; i++)
{
for(int j=0; j<cols; j++)
{
float blue = image.at<Vec3b>(i,j)[0];
float green = image.at<Vec3b>(i,j)[1];
float red = image.at<Vec3b>(i,j)[2];
grayscale.at<uchar>(i,j) = float(0.229*red + 0.587*green + 0.114*blue);
}
}
// normalize(grayscale,grayscale,0,255,NORM_MINMAX,CV_32FC1,Mat());
return grayscale;
}
Mat medianBlur(Mat image,int ksize=3)
{
// 中值滤波
if( image.channels() != 1 )
{
cerr<<"The channels of input image in medianBlur filer is not 1!!"<<endl;
}
int rows = image.rows;
int cols = image.cols;
int half = ksize/2;
int startSearchRow = half;
int endSearchRow = rows-half-1;
int startSearchCol = half;
int endSearchCol = cols-half-1;
int medianValue;
Mat dst = Mat::zeros(image.size(),CV_8U);
for(int i=startSearchRow; i<endSearchRow; i++)
{
for( int j=startSearchCol; j<endSearchCol; j++)
{
vector<int> window;
for(int k=(i-half)