OpenCV实现图像角点检测
时间:2020-11-18 00:32:09|栏目:C代码|点击: 次
历时一个多月,于今天上午终于将项目交上去了,这期间虽很辛苦,但是成长了不少,在此将项目中涉及到的知识点进行整理,本文主要介绍图像的角点检测:
一、代码部分:
// Detect_Corners.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "opencv2/opencv.hpp" #include <opencv2/imgproc/imgproc.hpp> #include <iostream> #include "opencv2/highgui/highgui.hpp" #include <stdio.h> #include <stdlib.h> using namespace std; using namespace cv; //全局变量 Mat src, src_gray; int thresh = 200; int max_thresh = 255; char* source_window = "Source image"; //char* corners_window = "Corners detected"; //函数声明 void cornerHarris_demo(int, void*); int _tmain(int argc, _TCHAR* argv[]) { //Load source image and convert it to gray char *img_name="..\\image\\71254.png"; src=imread(img_name); imshow(source_window,src); cvtColor(src, src_gray, CV_BGR2GRAY); createTrackbar("Threshold: ", source_window, &thresh, max_thresh, cornerHarris_demo); waitKey(0); //角点检测 cornerHarris_demo(0,0); return 0; } /** 函数 cornerHarris_demo */ void cornerHarris_demo( int, void*) { Mat dst, dst_norm,dst_norm_scaled; dst = Mat::zeros(src.size(), CV_32FC1 ); // Detector parameters int blockSize = 2; int apertureSize = 3; double k = 0.04; // Detecting corners cornerHarris( src_gray, dst, blockSize, apertureSize, k, BORDER_DEFAULT ); // Normalizing normalize( dst, dst_norm, 0, 255, NORM_MINMAX, CV_32FC1, Mat() ); convertScaleAbs( dst_norm, dst_norm_scaled ); // Drawing a circle around corners for( int j = 0; j < dst_norm.rows ; j++ ) { for( int i = 0; i < dst_norm.cols; i++ ) { if( (int) dst_norm.at<float>(j,i) > thresh ) { circle( dst_norm_scaled, Point(i, j), 5, Scalar(0), 2, 8, 0 ); circle(src,Point( i, j ), 5, Scalar(255,0,0), -1, 8, 0 ); } } } // Showing the result imshow( source_window, src); }
二、检测效果图: