Ask Your Question
0

HarrisCorner Detection in OpenCV C++

asked 2020-03-20 06:02:25 -0600

balaji gravatar image

updated 2020-03-20 06:34:56 -0600

supra56 gravatar image

Hi, I am trying to implement Harris corner detection in OpenCV C++ without using the built-in function CornerHarris.

This is the code I wrote and is not working. What is the mistake in the code?

int main(int argc, const char** argv)
{


    Mat src;

    Mat grad_x, grad_y;
    Mat Ixx,Ixy,Iyy;
    Mat H;
    Mat Hxx, Hxy, Hyy;
    Mat tr, det, Response;

    double sigmaX, sigmaY;
    Size gaussian_window_size = Size(3, 3);

    cv::CommandLineParser parser(argc, argv,
        "{@input   |D:/Codebases/VS_Workspace/DIP_Book_Images/chessboard.png|input image}"
    );


    const String window_name = "Harris Corner Demo ";
    int ksize = 3;
    int scale = 1;
    int delta = 0;
    int ddepth = CV_16S;


    String imageName = parser.get<String>("@input");
    src = imread(samples::findFile(imageName), IMREAD_GRAYSCALE); 
    src.convertTo(src, CV_32F);


    if (src.empty())
    {
        cout << "Error reading image " << imageName << endl;
        return EXIT_FAILURE;
    }


    Sobel(src, grad_x, ddepth, 1, 0, ksize, scale, delta, BORDER_DEFAULT);



    Sobel(src, grad_y, ddepth, 0, 1, ksize, scale, delta, BORDER_DEFAULT);


    Ixx = grad_x.mul(grad_x);
    Ixy = grad_x.mul(grad_y);
    Iyy = grad_y.mul(grad_y);



    sigmaX = 0;
    sigmaY = 0;
    GaussianBlur(Ixx, Hxx, gaussian_window_size,  sigmaX, sigmaY, BORDER_DEFAULT);
    GaussianBlur(Ixy, Hxy, gaussian_window_size,  sigmaX, sigmaY, BORDER_DEFAULT);
    GaussianBlur(Iyy, Hyy, gaussian_window_size,  sigmaX, sigmaY, BORDER_DEFAULT);

    tr = Hxx + Hyy; 
    det = Hxx.mul(Hyy) - Hxy.mul(Hxy);
    Mat tr2 = tr.mul(tr);
    Mat tr3 = tr2*0.04f;  
    Response = det - tr3;

    Mat max_resp; 
    dilate(Response, max_resp, getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1)));


    Mat t1, t2;
    t1 = (Response == max_resp);
    t2 = Response > 1e4;

    Mat corners = t1&t2;


    imshow(window_name, corners);

    char key = (char)waitKey(0);


    return 0;
}
  • Thank you.
edit retag flag offensive close merge delete

Comments

2

and is not working.

never working as a problem description. please explain.

berak gravatar imageberak ( 2020-03-20 06:50:34 -0600 )edit
1

I mean, it is not detecting corners.

balaji gravatar imagebalaji ( 2020-03-20 10:57:12 -0600 )edit

1 answer

Sort by » oldest newest most voted
0

answered 2020-03-21 09:49:00 -0600

kbarni gravatar image

updated 2020-03-21 10:50:01 -0600

Didn't go through the entire code, but you have to work on subimages with the Harris detector. That means that Ixx(x,y), Ixy(x,y) and Iyy(x,y) are the neighborhoods of pixel(x,y), not the entire image. (This window has size of blockSize in the OpenCV implementation)

The structure tensor needs to be computed locally, not on the entire image.

So the algorithm becomes:

  1. Get the GX and GY gradients
  2. Using a sliding window, for each point(x,y):
    • Compute the sum(G'X²), sum(G'Y²), sum(G'X*G'Y); where G' is a subimage of G around the current point.
    • Construct M, a 2x2 matrix.
    • Compute det(M) and tr(M); then eventually lambda1 and lambda2
    • Compute R. This will be the probability of having a corner in the current point.
edit flag offensive delete link more

Comments

Do you know if OpenCV offers a function to return the response/score of Harris corners? This would be better than a list of points sorted by an unknown response.

Der Luftmensch gravatar imageDer Luftmensch ( 2020-03-21 10:17:30 -0600 )edit

Yes, OpenCV has in-built Harris detector (cornerHarris function), it returns the Harris score for each pixel (not a list of points). But the question was about implementing this function yourself.

kbarni gravatar imagekbarni ( 2020-03-21 10:47:30 -0600 )edit

I see, then on the user to sort and perform non-max suppression. Interesting how there isn’t a Harris Features2D interface returning KeyPoints.

Der Luftmensch gravatar imageDer Luftmensch ( 2020-03-22 09:06:55 -0600 )edit

There is, it is called goodFeaturesToTrack().

Eduardo gravatar imageEduardo ( 2020-03-22 10:02:40 -0600 )edit

That function unfortunately does not return the response value of the features.

Der Luftmensch gravatar imageDer Luftmensch ( 2020-03-22 19:51:01 -0600 )edit

My bad, I know see GFTTDetector.

Der Luftmensch gravatar imageDer Luftmensch ( 2020-03-23 18:16:56 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-03-20 06:02:25 -0600

Seen: 360 times

Last updated: Mar 21 '20