OpenCv-C++-Harris角點檢測
阿新 • • 發佈:2018-11-30
首先,感謝賈志剛老師的課程教學。
理論部分:
我現在將程式碼放入:
#include<opencv2/opencv.hpp> #include<iostream> #include<math.h> 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); }
--------------------------------------------------------最終執行的結果--------------------------------------
輸入的圖片:
結果圖片(值為130):
拖動滑塊(值為80):