标签:
代码示例一:
#include<opencv2/opencv.hpp> using namespace cv; int main(){ Mat src = imread("church.jpg", 0); imshow("原始图", src); //进行Harris角点检测找出角点 Mat cornerStrength; cornerHarris(src, cornerStrength, 2, 3, 0.01); //对灰度图进行阈值操作,得到二值图并显示 Mat harrisCorner; threshold(cornerStrength, harrisCorner, 0.0001, 255, THRESH_BINARY); imshow("二值效果图",harrisCorner); waitKey(0); return 0; }
效果:
代码示例二:
#include<opencv2/opencv.hpp> using namespace cv; #define WINDOW_NAME1 "窗口1" #define WINDOW_NAME2 "窗口2" Mat src,srcClone,gray; int thresh = 30; int max_thresh = 175; //函数声明 void onCornerHarris(int, void*);//回调函数 int main(){ src = imread("church.jpg", 1); imshow("原始图", src); srcClone = src.clone(); cvtColor(srcClone, gray, COLOR_BGR2GRAY); //创建窗口和滚动条 namedWindow(WINDOW_NAME1, WINDOW_AUTOSIZE); createTrackbar("阈值:", WINDOW_NAME1, &thresh, max_thresh, onCornerHarris); //调用一次回调函数进行初始化 onCornerHarris(0, 0); waitKey(0); return 0; } void onCornerHarris(int, void*){ Mat dstImage;//目标图 Mat normImage;//归一化后的图 Mat scaledImage;//线性变换后的8位无符号整形图 //初始化 dstImage = Mat::zeros(src.size(), CV_32FC1); srcClone = src.clone(); //进行角点检测 cornerHarris(gray, dstImage, 2, 3, 0.04, BORDER_DEFAULT); //归一化与转换 normalize(dstImage, normImage, 0, 255, NORM_MINMAX, CV_32FC1, Mat()); convertScaleAbs(normImage, scaledImage); //绘制:将检测到的,符合阈值条件的角点绘制出来 for (int j = 0; j < normImage.rows; j++){ for (int i = 0; i < normImage.cols; i++){ if ((int)normImage.at<float>(j, i)>thresh + 80){ circle(srcClone, Point(i, j), 5, Scalar(10, 10, 255), 2, 8, 0); circle(scaledImage, Point(i, j), 5, Scalar(0, 10, 255), 2, 8, 0); } } } imshow(WINDOW_NAME1, srcClone); imshow(WINDOW_NAME2, scaledImage); }
效果:
标签:
原文地址:http://www.cnblogs.com/bluebean/p/5721032.html