opencv-Harris角点检测
1、角点检测理论
(ps:还有很多理论的数学函数,贴一个最主要的)
没有扎实的理论基础,公式可以暂时不需要理解。对于上层应用开发人员,会用API和调参应该就行了。
2、代码
(ps:这是opencv特征检测里一小节课程,代码大多使用api,所以不注释了,以后回来复习的时候应该也不会多费力)
程序用了trackbar,更好的演示程序和观察阈值对角点检测的影响。
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
const char * output_title = "output";
int thresh = 120;
int max_count = 255;
void Harris(int,void*);
Mat src,gray_src;
int main()
{
src = imread("2.jpg");
namedWindow("src", 0);
imshow("src",src);
namedWindow(output_title, 0);
cvtColor(src, gray_src, CV_BGR2GRAY);
createTrackbar("Threash",output_title,&thresh,max_count,Harris);
Harris(0, 0);
waitKey(0);
return 0;
}
void Harris(int,void*)
{
Mat norm_dst,normScaledst;
Mat dst = Mat::zeros(gray_src.size(), CV_32FC1);
int block = 5;
int ksize = 27;
double k = 0.04;
cornerHarris(gray_src, dst, block, ksize, k, BORDER_DEFAULT);
normalize(dst, norm_dst, 0, 255, NORM_MINMAX, CV_32FC1);
convertScaleAbs(norm_dst, normScaledst);
Mat res = src.clone();
for (int i = 0; i < res.rows; i++)
{
uchar* currentRow = normScaledst.ptr(i);
for (int j = 0; j < res.cols; j++)
{
int value = (int)*currentRow;
if (value > thresh)
{
circle(res, Point(i, j), 0.5, Scalar(0, 0, 255), 1, 8, 0);
}
currentRow++;
}
}
imshow(output_title, res);
}
3、效果截图