Harris角点检测算法C++代码
时间: 2023-08-16 12:07:03 浏览: 110
以下是Harris角点检测算法的C++代码示例:
```cpp
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat src = imread("test.jpg");
if (src.empty()) {
cout << "Could not open or find the image!\n" << endl;
return -1;
}
Mat src_gray, dst, dst_norm, dst_norm_scaled;
int blockSize = 2;
int apertureSize = 3;
double k = 0.04;
int thresh = 200;
cvtColor(src, src_gray, COLOR_BGR2GRAY);
dst = Mat::zeros(src.size(), CV_32FC1);
cornerHarris(src_gray, dst, blockSize, apertureSize, k);
normalize(dst, dst_norm, 0, 255, NORM_MINMAX, CV_32FC1, Mat());
convertScaleAbs(dst_norm, dst_norm_scaled);
for (int i = 0; i < dst_norm.rows; i++) {
for (int j = 0; j < dst_norm.cols; j++) {
if ((int)dst_norm.at<float>(i, j) > thresh) {
circle(dst_norm_scaled, Point(j, i), 5, Scalar(0), 2, 8, 0);
}
}
}
namedWindow("Harris corner detector", WINDOW_NORMAL);
imshow("Harris corner detector", dst_norm_scaled);
waitKey();
return 0;
}
```
该代码使用OpenCV库实现了Harris角点检测算法,对输入的图像进行角点检测,并将结果显示出来。其中,blockSize和apertureSize分别表示Harris算法中的窗口大小和Sobel算子的大小,k是常数值,thresh是阈值。
阅读全文