尋找Harris、Shi-Tomasi和亞像素角點

Harris、Shi-Tomasi和亞像素角點都是角點,隸屬於特徵點這個大類(特徵點能夠分爲邊緣、角點、斑點).html

1、Harris角點檢測是一種直接基於灰度圖像的角點提取算法,穩定性較高,可是也可能出現有用信息丟失的狀況。
函數:cornerHarris()
void cv::cornerHarris ( InputArray  src,  //須要爲8位單通道
    OutputArray  dst,  //結果
    int  blockSize, //領域大小
    int  ksize, //Sobel孔徑大小
    double  k, //Harris參數
    int  borderType = BORDER_DEFAULT 
  )    

Harris corner detector.算法

The function runs the Harris corner detector on the image. Similarly to cornerMinEigenVal and cornerEigenValsAndVecs , for each pixel (x, y) it calculates a 2\times2 gradient covariance matrix M^{(x,y)} over a \texttt{blockSize} \times \texttt{blockSize} neighborhood. Then, it computes the following characteristic:less

(特徵點計算方法)ide

 

 

Corners in the image can be found as the local maxima of this response map.函數

Parameters
src Input single-channel 8-bit or floating-point image.
dst Image to store the Harris detector responses. It has the type CV_32FC1 and the same size as src .
blockSize Neighborhood size (see the details on cornerEigenValsAndVecs ).
ksize Aperture parameter for the Sobel operator.
k Harris detector free parameter. See the formula below.
borderType Pixel extrapolation method. See cv::BorderTypes.
調用:
 
    Mat srcGray = imread("e:/template/lena.jpg",IMREAD_GRAYSCALE);
    //進行角點檢測
    Mat matHarris;
    cornerHarris(srcGray,matHarris,2,3,0.01);
    //顯示
    Mat matDst;
    threshold(matHarris,matDst,0.00001,255,THRESH_BINARY);
    imshow("matDst",matDst);
    waitKey(0);
lena的結果:
2、Shi-Tomasi角點
通常認爲是Harris的改進,由於當時提出的論文叫作《Good Features to Track》,因此這種角點再OpenCV中叫作goodFeatures
函數:goodFeaturesToTrack()
void cv::goodFeaturesToTrack ( InputArray  image,//輸入圖像
    OutputArray  corners,//輸出向量
    int  maxCorners,//角點最大數量
    double  qualityLevel,//角點檢測可接受的最小特徵值
    double  minDistance,//角點之間的最小距離
    InputArray  mask = noArray(),//感興趣區域
    int  blockSize = 3,//領域範圍
    bool  useHarrisDetector = false,//true爲harris;false爲Shi-Tomasi
    double  k = 0.04 //權重係數
  )    

Determines strong corners on an image. this

The function finds the most prominent corners in the image or in the specified image region, as described in [154]atom

  • Function calculates the corner quality measure at every source image pixel using the cornerMinEigenVal or cornerHarris .
  • Function performs a non-maximum suppression (the local maximums in 3 x 3 neighborhood are retained).
  • The corners with the minimal eigenvalue less than qualityLevelmaxx,yqualityMeasureMap(x,y) are rejected.
  • The remaining corners are sorted by the quality measure in the descending order.
  • Function throws away each corner for which there is a stronger corner at a distance less than maxDistance.

The function can be used to initialize a point-based tracker of an object.idea

Note
If the function is called with different values A and B of the parameter qualityLevel , and A > B, the vector of returned corners with qualityLevel=A will be the prefix of the output vector with qualityLevel=B .
Parameters
image Input 8-bit or floating-point 32-bit, single-channel image.
corners Output vector of detected corners.
maxCorners Maximum number of corners to return. If there are more corners than are found, the strongest of them is returned. maxCorners <= 0 implies that no limit on the maximum is set and all detected corners are returned.
qualityLevel Parameter characterizing the minimal accepted quality of image corners. The parameter value is multiplied by the best corner quality measure, which is the minimal eigenvalue (see cornerMinEigenVal ) or the Harris function response (see cornerHarris ). The corners with the quality measure less than the product are rejected. For example, if the best corner has the quality measure = 1500, and the qualityLevel=0.01 , then all the corners with the quality measure less than 15 are rejected.
minDistance Minimum possible Euclidean distance between the returned corners.
mask Optional region of interest. If the image is not empty (it needs to have the type CV_8UC1 and the same size as image ), it specifies the region in which the corners are detected.
blockSize Size of an average block for computing a derivative covariation matrix over each pixel neighborhood. See cornerEigenValsAndVecs .
useHarrisDetector Parameter indicating whether to use a Harris detector (see cornerHarris) or cornerMinEigenVal.
k Free parameter of the Harris detector.
調用:
Mat srcGray = imread("e:/template/lena.jpg",IMREAD_GRAYSCALE);    
//進行角點檢測
    Mat matHarris;
    vector<Point2f> corners;//輸出向量
    goodFeaturesToTrack(srcGray,corners,100,0.01,10,Mat(),3,false,0.04);
    //顯示
    Mat matDst = srcGray.clone();
    for (int i=0;i<corners.size();i++)
    {
        circle(matDst,corners[i],2,Scalar(255));
    }
     
    imshow("matDst",matDst);
    waitKey(0);
結果:
能夠看到,眼部、帽子上面的尖端這些的倒是"GoodFeatures"的地方都被標註了出來
3、若是須要亞像素的角點,咱們必須更進一步。
函數:cornerSubPix()
void cv::cornerSubPix ( InputArray  image,
    InputOutputArray  corners,
    Size  winSize,
    Size  zeroZone,
    TermCriteria  criteria 
  )    
調用:須要注意現計算goodfeatures再算亞像素
Mat srcGray = imread("e:/template/lena.jpg",IMREAD_GRAYSCALE);
    //進行角點檢測
    Mat matHarris;
    vector<Point2f> corners;//輸出向量
    cv::goodFeaturesToTrack(
        srcGray,                              // Image to track
        corners,                          // Vector of detected corners (output)
        MAX_CORNERS,                       // Keep up to this many corners
        0.01,                              // Quality level (percent of maximum)
        5,                                 // Min distance between corners
        cv::noArray(),                     // Mask
        3,                                 // Block size
        false,                             // true: Harris, false: Shi-Tomasi
        0.04                               // method specific parameter
        );
    cv::cornerSubPix(
        srcGray,                          // Input image
        corners,                          // Vector of corners (input and output)
        cv::Size(55),      // Half side length of search window
        cv::Size(-1,-1),                   // Half side length of dead zone (-1=none)
        cv::TermCriteria(
            cv::TermCriteria::MAX_ITER | cv::TermCriteria::EPS,
            20,                              // Maximum number of iterations
            0.03                             // Minimum change per iteration
            )
        );
    //顯示
    Mat matDst = srcGray.clone();
    for (int i=0;i<corners.size();i++)
    {
        circle(matDst,corners[i],2,Scalar(255));
        cout<<"\t"<<"序號"<<i<<"亞像素座標("<<corners[i].x<<","<<corners[i].y<<")"<<endl;
    }
     
    imshow("matDst",matDst);
    waitKey(0);
結果:
能夠看到其計算處理小數點後面的值。
4、小結
角點雖然如今用的比較少了,可是做爲基本的知識有必要了解;下一步的更爲複雜的特徵點模型都是基於角點的,它們之間有着一脈相承的關係。
相關文章