问题描述
我只想尝试openCV函数 - cvCornerHarris。这是我的c ++代码:
//图片文件
char imagePath [256] =./images/lena512color .tiff;
printf(%s\\\
,imagePath);
IplImage * srcImg = cvLoadImage(imagePath,1);
if(NULL == srcImg){
printf(无法打开图像文件.\\\
);
return -1;
}
IplImage * srcImgGry = cvCreateImage(cvGetSize(srcImg),IPL_DEPTH_8U,1);
cvCvtColor(srcImg,srcImgGry,CV_RGB2GRAY);
// Canny和Harris期望灰度(8位)输入。
//哈里斯图像的输出必须是32位浮点数。
IplImage * harrisImg = cvCreateImage(cvGetSize(srcImg),IPL_DEPTH_32F,1);
IplImage * cannyImg = cvCreateImage(cvGetSize(srcImg),IPL_DEPTH_8U,1);
////使用Harris-corner角度检测
cvCornerHarris(srcImgGry,harrisImg,5,5,0.04);
cvCanny(srcImgGry,cannyImg,50,100,3);
//(5)显示结果
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);
我可以得到 cvCanny
( cannyImg
),但输出图像 cvCornerHarris
( harrisImg
)是一个黑色的图像,没有什么。
请帮助解释如何使用此函数 cvCornerHarris
。非常感谢!
这是关于参数!人们往往相信有魔法参数适用于所有类型的图像和场景。不幸的是,这在现实世界中不会发生。
用于处理一个图像的参数在应用于其他类型的图像时可能不会产生相同的结果水平。现在,请考虑以下代码:
IplImage * colored = cvLoadImage(house.jpg,CV_LOAD_IMAGE_UNCHANGED);
if(!colored)
{
printf(无法打开图像文件.\\\
);
return -1;
}
IplImage * grey = cvCreateImage(cvGetSize(colored),IPL_DEPTH_8U,1);
cvCvtColor(colored,gray,CV_RGB2GRAY);
IplImage * harris = cvCreateImage(cvGetSize(colored),IPL_DEPTH_32F,1);
cvCornerHarris(grey,harris,3,11,0.07);
cvNamedWindow(Harris,CV_WINDOW_AUTOSIZE);
cvShowImage(Harris,harris);
如下图所示,这些参数产生了一个体面的结果我的观点)。但是,请记住,他们不会为你工作。错误的参数将产生一个黑色图像(即将不会检测到任何东西),因为你在测试中观察到。
答案是:看看文档,参数意味着它们如何影响结果。
输入图片:
com / KoWfR.pngalt =harry's_house>
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!
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).\n");
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:
Output:
这篇关于不能得到输出结果使用cvCornerHarris()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!