Harris Corner Detection

void cornerHarris(InputArray src, OutputArray dst, int blockSize, int ksize, double k, int borderType=BORDER_DEFAULT ) 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 
  • ksize – Aperture parameter for the Sobel() operator.
  • k – Harris detector free parameter. See the formula below.
  • borderType – Pixel extrapolation method.
For each pixel (x,y) it calculates a 2x2 gradient covariance matrix M(x,y) over a "blocksize x blocksize" neighborhood. Then, it computes the following characteristic:

\texttt{dst} (x,y) =  \mathrm{det} M^{(x,y)} - k  \cdot \left ( \mathrm{tr} M^{(x,y)} \right )^2
Corners in the image can be found as the local maxima of this response map.

A good example for Harris Corner Detection is provided in OpenCV Documentation.

Functions:


Example:

------------------
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace cv;
using namespace std;

int thresh = 200;

int main( )
{
    Mat src, gray;
    // Load source image and convert it to gray
    src = imread( "lena.jpg", 1 );
    cvtColor( src, gray, CV_BGR2GRAY );
    Mat dst, dst_norm, dst_norm_scaled;
    dst = Mat::zeros( src.size(), CV_32FC1 );

    // Detecting corners
    cornerHarris( gray, dst, 7, 5, 0.05, 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 );
        }
    }
    }


    // Showing the result
    namedWindow( "corners_window", CV_WINDOW_AUTOSIZE );
    imshow( "corners_window", dst_norm_scaled );

    waitKey(0);
    return(0);
}
------------------
Result:

No comments:

Post a Comment