角点检测
cornerHarris 算子
void cv::cornerHarris ( InputArray src,OutputArray dst,int blockSize,int ksize,double K,int borderType = BORDER_DEFAULT)
src:待检测Harris角点的输入图像,图像必须是CV 8U或者CV 32F的单通道灰度图像
dst: 存放Harris评价系数的R矩阵,数据类型为CV 32F的单通道图像,与输入图像具有相同的尺寸
blockSize:邻域大小
ksize: Sobel算子的半径,用于得到梯度信息
k:计算Harris评价系数R的权重系数
borderType:像素外推算法标志
示例
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
Mat src, gray_src;
int thresh = 130;
int max_count = 255;
const char* output_title = "HarrisCornerDetection Result";
void Harris_Demo(int, void*);
int main(int argc, char** argv) {
src = imread("D:/vcprojects/images/home.jpg");
if (src.empty()) {
printf("无法加载图像...\n");
return -1;
}
namedWindow("输入图像", CV_WINDOW_AUTOSIZE);
imshow("输入图像", src);
namedWindow(output_title, CV_WINDOW_AUTOSIZE);
cvtColor(src, gray_src, COLOR_BGR2GRAY);
createTrackbar("阈值:", output_title, &thresh, max_count, Harris_Demo);
Harris_Demo(0, 0);
waitKey(0);
return 0;
}
void Harris_Demo(int, void*) {
Mat dst, norm_dst, normScaleDst;
int blockSize = 2;
int ksize = 3;
double k = 0.04;
cornerHarris(gray_src, dst, blockSize, ksize, k, BORDER_DEFAULT);
normalize(dst, norm_dst, 0, 255, NORM_MINMAX, CV_32FC1, Mat());
convertScaleAbs(norm_dst, normScaleDst);
Mat resultImg = src.clone();
for (int row = 0; row < resultImg.rows; row++) {
uchar* currentRow = normScaleDst.ptr(row);
for (int col = 0; col < resultImg.cols; col++) {
int value = (int)*currentRow;
if (value > thresh) {
circle(resultImg, Point(col, row), 2, Scalar(0, 0, 255), 2, 8, 0);
}
currentRow++;
}
}
imshow(output_title, resultImg);
}