代码示例一:
#include<opencv2/opencv.hpp> using namespace cv; int main(){ Mat src = imread(); imshow("原始图", src); //进行Harris角点检测找出角点 Mat cornerStrength; cornerHarris(src, cornerStrength, , , 0.01); //对灰度图进行阈值操作,得到二值图并显示 Mat harrisCorner; threshold(cornerStrength, harrisCorner, , THRESH_BINARY); imshow("二值效果图",harrisCorner); waitKey(); ; }
效果:
代码示例二:
#include<opencv2/opencv.hpp> using namespace cv; #define WINDOW_NAME1 "窗口1" #define WINDOW_NAME2 "窗口2" Mat src,srcClone,gray; ; ; //函数声明 void onCornerHarris(int, void*);//回调函数 int main(){ src = imread(); imshow("原始图", src); srcClone = src.clone(); cvtColor(srcClone, gray, COLOR_BGR2GRAY); //创建窗口和滚动条 namedWindow(WINDOW_NAME1, WINDOW_AUTOSIZE); createTrackbar("阈值:", WINDOW_NAME1, &thresh, max_thresh, onCornerHarris); //调用一次回调函数进行初始化 onCornerHarris(, ); waitKey(); ; } void onCornerHarris(int, void*){ Mat dstImage;//目标图 Mat normImage;//归一化后的图 Mat scaledImage;//线性变换后的8位无符号整形图 //初始化 dstImage = Mat::zeros(src.size(), CV_32FC1); srcClone = src.clone(); //进行角点检测 cornerHarris(gray, dstImage, , , 0.04, BORDER_DEFAULT); //归一化与转换 normalize(dstImage, normImage, , , NORM_MINMAX, CV_32FC1, Mat()); convertScaleAbs(normImage, scaledImage); //绘制:将检测到的,符合阈值条件的角点绘制出来 ; j < normImage.rows; j++){ ; i < normImage.cols; i++){ ){ circle(srcClone, Point(i, j), , Scalar(, , ), , , ); circle(scaledImage, Point(i, j), , Scalar(, , ), , , ); } } } imshow(WINDOW_NAME1, srcClone); imshow(WINDOW_NAME2, scaledImage); }
效果:
代码示例三:
#include<opencv2/opencv.hpp> using namespace cv; #include<vector> using namespace std; class HarrisDetector{ private: //表示角点强度的32位浮点图像 Mat cornerStrength; //阈值化后的32位浮点图像 Mat cornerTh; //局部极大值图像(内部) Mat localMax; //导数平滑的相邻像素的尺寸 int neighbourhood; //梯度计算的孔径大小 int aperture; //Harris参数 double k; //harris计算的最大强度 double maxStrength; //计算得到的阈值(内部) double threshold; //非极大值抑制的相邻像素的尺寸 int nonMaxSize; //非极大值抑制的核 Mat kernel; public: HarrisDetector() :neighbourhood(), aperture(), k(0.01), maxStrength(0.0), threshold(){ //创建非极大值抑制的核 } void detect(const Mat& image){ //harris计算 cornerHarris(image, cornerStrength, neighbourhood, aperture, k); //内部阈值计算 double minStrength;//未使用 minMaxLoc(cornerStrength, &minStrength, &maxStrength); //局部极大值检测 Mat dilated;//临时图像 dilate(cornerStrength, dilated, Mat()); compare(cornerStrength, dilated, localMax, CMP_EQ); } Mat getCornerMap(double qualityLevel){ Mat cornerMap; //对角点图像进行阈值化 this->threshold = qualityLevel*maxStrength; cv::threshold(cornerStrength, cornerTh, threshold, , THRESH_BINARY); //转换为8位图像 cornerTh.convertTo(cornerMap,CV_8U); //非极大值抑制 bitwise_and(cornerMap, localMax, cornerMap); return cornerMap; } void getCorners(vector<cv::Point>& points,double qualityLevel){ //得到角点图 cv::Mat cornerMap = getCornerMap(qualityLevel); getCorners(points, cornerMap); } void getCorners(vector<cv::Point>& points, const Mat& cornerMap){ //遍历像素得到所有特征 ; y < cornerMap.rows; y++){ const uchar* rowPtr = cornerMap.ptr <uchar>(y); ; x < cornerMap.cols; x++){ //如果是特征点 if (rowPtr[x]){ points.push_back(cv::Point(x, y)); } } } } //在特征点的位置绘制圆 ,,), , ){ vector<cv::Point>::const_iterator it = points.begin(); while (it != points.end()){ cv::circle(image, *it, radius, color, thickness); ++it; } } }; int main(){ Mat src = imread(); //HarrisDetector类使用方式 HarrisDetector harris; harris.detect(src); std::vector<cv::Point> pts; harris.getCorners(pts, 0.1); harris.drawOnImage(src, pts); imshow("result", src); waitKey(); }
效果: