OpenCv-C++-自定義角點檢測器(shi-tomashi自定義)
阿新 • • 發佈:2018-11-30
前面學過了自定義角點檢測器(Harris),現在學習tomasi自定義角點檢測,總體上來說tomasi的程式碼計算量比較harris少,具體步驟跟harris自定義角點檢測類似,主要少了"計算響應"(在部落格自定義角點檢測器(harris自定義)的程式碼註釋裡)的那一步,主要使用cornerMinEigenVal()函式來計算最小特徵值。
參考:https://blog.csdn.net/u014310328/article/details/46434321
cornerMinEigenVal()
作用:計算梯度矩陣的最小特徵值用於角點檢測。
形式:void cornerMinEigenVal(InputArray src, OutputArray dst, int blockSize, int ksize=3, int borderType=BORDER_DEFAULT );
引數:
src:輸入單通道8位或浮點影象;
dst:用來儲存最小特徵值的影象;它有src相同的大小和型別為CV_32FC1;
blockSize:領域尺寸;一般取3
ksize:Sobel()運算元的孔徑引數;一般取3,5,7
borderType:畫素外推方式;
現在放上程式碼:
#include<opencv2/opencv.hpp> #include<iostream> #include<math.h> using namespace cv; using namespace std; Mat src, gray_src; Mat tomasi_res; //儲存影象中的最大值和最小值 double tomasi_min; double tomasi_max; //計算百分比,拖動TrackBar int current_value = 30; int max_value = 100; const char* output_title = "custom tomasi corner title"; void custom_tomasi_corner(int, void*); int main(int argc, char** argv) { src = imread("D:/test/大廈.jpg"); if (!src.data) { cout << "the image could not found..." << endl; return -1; } cvtColor(src, gray_src, COLOR_BGR2GRAY); tomasi_res = Mat::zeros(src.size(), CV_32FC1); imshow("input title", src); //計算最小特徵值 int blocksize = 3; int ksize = 3; cornerMinEigenVal(gray_src, tomasi_res, blocksize, ksize, BORDER_DEFAULT); minMaxLoc(tomasi_res, &tomasi_min, &tomasi_max, 0, 0, Mat()); namedWindow(output_title, CV_WINDOW_AUTOSIZE); createTrackbar("tomasi-bar", output_title, ¤t_value, max_value, custom_tomasi_corner); custom_tomasi_corner(0, 0); waitKey(0); return 0; } void custom_tomasi_corner(int, void *) { if (current_value < 10) { current_value = 10; } Mat resultImg = src.clone(); float t = tomasi_min + (((double)current_value) / max_value)*(tomasi_max - tomasi_min); for (int row = 0; row < src.rows; row++) { for (int col = 0; col < src.cols; col++) { float v = tomasi_res.at<float>(row, col); if (v > t) { circle(resultImg, Point(col, row), 2, Scalar(0, 0, 255), 2, 8, 0); } } } imshow(output_title, resultImg); }
執行結果:
原圖:
結果圖(值=30):
值=11:
與上一篇部落格的執行結果做比較,tomasi的反應速度較快,不容易出現視窗崩潰的情況,但tomasi的計算結果比較低,同是值=30,但harris的角點檢測數遠遠大於tomasi的檢點檢測數。