Harris角點檢測

 




程式碼

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core.hpp"
#include "opencv2/dnn.hpp"
#include "opencv2/xfeatures2d/nonfree.hpp"
#include "opencv2/features2d/features2d.hpp"
#include <iostream>
#include <fstream> 
#include<cmath>
#include<string>
#include <algorithm>
using namespace std;
using namespace cv;
using namespace cv::dnn;
using namespace cv::xfeatures2d;
Mat src, gray_src, dst;
int thrCornor = 140;
int thrMax = 255;
const char* output_title = "HarrisCorner Detect Result";
void Harris_Process(int, void*);
int main()
{

	src = imread("C:/img/2393662_orig.jpg");
	namedWindow("src", WINDOW_NORMAL);
	imshow("src", src);

	namedWindow(output_title, WINDOW_NORMAL);
	cvtColor(src, gray_src, COLOR_BGR2GRAY);
	createTrackbar("角點門檻值", output_title, &thrCornor, thrMax, Harris_Process);
	Harris_Process(0,0);

	waitKey(0);
	return 0;

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

	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 > thrCornor) {
				circle(resultImg, Point(col, row), 1, Scalar(255, 255, 0), 1, 8, 0);
			}
			currentRow++;
		}
	}
	imshow(output_title,resultImg);
}









留言

這個網誌中的熱門文章

何謂淨重(Net Weight)、皮重(Tare Weight)與毛重(Gross Weight)

Architecture(架構) 和 Framework(框架) 有何不同?_軟體設計前的事前規劃的藍圖概念

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題