Take the 2-minute tour ×

I just want to try the openCV function -- cvCornerHarris. Here is my c++ code:

//image file
    char imagePath[256] = "./images/lena512color.tiff";
    printf("%s\n", imagePath);

    IplImage* srcImg = cvLoadImage(imagePath, 1);
    if(NULL == srcImg){
        printf("Can not open image file(s).\n");
        return -1;
    IplImage* srcImgGry = cvCreateImage(cvGetSize(srcImg), IPL_DEPTH_8U, 1);
    cvCvtColor(srcImg, srcImgGry, CV_RGB2GRAY);
    // Canny and Harris expect grayscale  (8-bit) input.
    // And output of harris image must be 32-bit float .
    IplImage* harrisImg = cvCreateImage(cvGetSize(srcImg), IPL_DEPTH_32F, 1);
    IplImage* cannyImg = cvCreateImage(cvGetSize(srcImg), IPL_DEPTH_8U, 1);

    //// Corner detection using Harris-corner
    cvCornerHarris(srcImgGry, harrisImg, 5, 5, 0.04);
    cvCanny(srcImgGry, cannyImg, 50, 100, 3);

    // (5)Display the result
    cvNamedWindow ("Img", CV_WINDOW_AUTOSIZE);
    cvShowImage ("Img", srcImgGry);
    cvNamedWindow ("Harris", CV_WINDOW_AUTOSIZE);
    cvShowImage ("Harris", harrisImg);
    cvNamedWindow ("Canny", CV_WINDOW_AUTOSIZE);
    cvShowImage ("Canny", cannyImg);
    cvWaitKey (0);

    cvDestroyWindow ("Harris");
    cvDestroyWindow ("Img");
    cvReleaseImage (&srcImg);
    cvReleaseImage (&srcImgGry);
    cvReleaseImage (&harrisImg);
    cvReleaseImage (&cannyImg);

I can get a expected output image of cvCanny (cannyImg) but the output image of cvCornerHarris (harrisImg)is an black image with nothing on it. Please help to explain how to use this function cvCornerHarris. Thanks!

share|improve this question
Where can we find the image you are using for test? –  karlphillip Jun 28 '11 at 1:41
It seems like you are using a function (cvCornerHarris) that you never used before, so don't throw it in the middle of a code and expect it to work right out of the box. This is a tip, ok? Don't be mad. This practice alone can save you a lot of time in coding. So in the future, isolate your problem to a minimal example and don't be afraid to play with it and test other parameters. If you had done it, you would eventually found a set of parameters that would work for you. –  karlphillip Jun 28 '11 at 2:39

1 Answer 1

up vote 3 down vote accepted

It's all about parameters! People tend to believe that there are magical parameters that will work for all types of images and scenarios. Unfortunately, this doesn't happen in the real world.

The parameters used to process one image may not produce the same level of results when applied to other type of image. Now, consider the following code:

IplImage* colored = cvLoadImage("house.jpg", CV_LOAD_IMAGE_UNCHANGED);
if (!colored)
    return -1;

IplImage* gray = cvCreateImage(cvGetSize(colored), IPL_DEPTH_8U, 1);
cvCvtColor(colored, gray, CV_RGB2GRAY);

IplImage* harris = cvCreateImage(cvGetSize(colored), IPL_DEPTH_32F, 1);
cvCornerHarris(gray, harris, 3, 11, 0.07);

cvNamedWindow("Harris", CV_WINDOW_AUTOSIZE);
cvShowImage ("Harris", harris);

As you can see below, these parameters produced a decent result (to my point of view). However, keep in mind that they won't probably work for you. Bad parameters will produce a black image (i.e. will detect nothing) as you have observed on your tests.

The answer is: take a look at the docs to see what those parameters mean and how they influence the result. Most importantly, play with them until they produce images that satisfy your needs.

Input image:




share|improve this answer
Thanks very much! I got a result finally. I was trying to find out the difference between cvCornerHarris and cvGoodFeaturesToTrack. I thought the result would be the locations of corners instead of edges. And at least I knew I misunderstood the function of cvCornerHarris. –  Nauhc Jun 29 '11 at 4:50
btw, are you familiar with Harris-laplace and Harris-Affine? –  Nauhc Jun 29 '11 at 4:53
Are you familiar with our working style? Review all your questions and accept the answers that helped you solved them. If there are any on this thread, click on the checkbox near the answer to select it as the official answer of this question. –  karlphillip Jun 29 '11 at 19:13

Your Answer


