OpenCv-C++-Harris角点检测

首先,感谢贾志刚老师的课程教学。
理论部分:
OpenCv-C++-Harris角点检测_第1张图片
我现在将代码放入:

#include
#include
#include

using namespace cv;
using namespace std;

void Harris_demo(int,void*);
int thres_value = 130;
int thres_Max = 255;
Mat src,gray_src;
const char* outputTitle = "output title";
int main(int argc, char** argv)
{
	src = imread("D:/test/大厦.jpg");
	if (src.empty())
	{
		cout << "图片为空" << endl;
		return -1;
	}
	cvtColor(src, gray_src, CV_BGR2GRAY);
	namedWindow(outputTitle, CV_WINDOW_AUTOSIZE);
	createTrackbar("Harris", outputTitle, &thres_value, thres_Max, Harris_demo);
	Harris_demo(0,0);
	imshow("input title", src);
	waitKey(0);
	return 0;


}

void Harris_demo(int, void *)
{
	Mat dst, norm_dst;
	dst = Mat::zeros(gray_src.size(), CV_32FC1);


	cornerHarris(gray_src, dst, 2, 3, 0.04, BORDER_DEFAULT);
	normalize(dst, norm_dst, 0, 255, NORM_MINMAX, CV_32FC1, Mat());
	convertScaleAbs(norm_dst, norm_dst);
	Mat resultImg = src.clone();

	for (int row = 0; row < resultImg.rows; row++)
	{
		uchar* currentRow = norm_dst.ptr(row);
		for (int col = 0; col < resultImg.cols; col++)
		{

			int value = (int)*currentRow;
			
			if (value > thres_value)
			{
				circle(resultImg, Point(col, row), 2, Scalar(0, 0, 255), 2, 8,0);
			}
			currentRow++;

		}
	}

	imshow(outputTitle, resultImg);

}

--------------------------------------------------------最终运行的结果--------------------------------------
输入的图片:
OpenCv-C++-Harris角点检测_第2张图片
结果图片(值为130):

OpenCv-C++-Harris角点检测_第3张图片
拖动滑块(值为80):
OpenCv-C++-Harris角点检测_第4张图片

你可能感兴趣的:(OpenCv-C++学习记录)