Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

c++ - Cant get output result using cvCornerHarris()

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

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

    IplImage* srcImg = cvLoadImage(imagePath, 1);
    if(NULL == srcImg){
        printf("Can not open image file(s).
");
        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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)
{
    printf("Can not open image file(s).
");
    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:

a_house
(source: 123desenhosparacolorir.com)

Output:

harry's_house


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...