0

假设我有这个代码:

#include "stdafx.h"
#include "opencv\highgui.h"
#include "opencv2\imgproc\imgproc.hpp"
#include "opencv2\core\core.hpp"
#include "opencv\cv.h"

using namespace cv;


IplImage* doPyrDown(IplImage*,int );
IplImage* doCanny(IplImage*,double,double,double);

int main()
{
    const char*a ="D:\\s.jpg" ;
    IplImage* in = cvLoadImage(a);
    IplImage* img1 = doPyrDown( in, IPL_GAUSSIAN_5x5 );
    IplImage* img2 = doPyrDown( img1, IPL_GAUSSIAN_5x5 );
    IplImage* img3 = doCanny( img2, 10, 100, 3 );
    cvNamedWindow("in",CV_WINDOW_AUTOSIZE);
    cvNamedWindow("img1",CV_WINDOW_AUTOSIZE);
    cvNamedWindow("img2",CV_WINDOW_AUTOSIZE);
    cvNamedWindow("img3",CV_WINDOW_AUTOSIZE);
    cvShowImage("in",in);
    cvShowImage("img1",img1);
    cvShowImage("img2",img2);
    cvShowImage("img3",img3);
    cvWaitKey(0);
    cvReleaseImage( &in );
    cvReleaseImage( &img1 );
    cvReleaseImage( &img2 );
    cvReleaseImage( &img3 );
    cvDestroyWindow("in");
    cvDestroyWindow("img1");
    cvDestroyWindow("img2");
    cvDestroyWindow("img3");
    return 0;
}




//Using cvPyrDown() to create a new image that is half the width and height of the input
//image
IplImage* doPyrDown(IplImage* in,int filter = CV_GAUSSIAN_5x5)
{
// Best to make sure input image is divisible by two.
    //
    assert( in->width%2 == 0 && in->height%2 == 0 );
    IplImage* out = cvCreateImage(cvSize( in->width/2, in->height/2 ),in->depth,in->nChannels);
    cvPyrDown( in, out );
    return( out );
}

 //The Canny edge detector writes its output to a single channel (grayscale) image
 IplImage* doCanny(IplImage* in,double lowThresh,double highThresh,double aperture)
 {
    if(in->nChannels != 1)
        return(0); //Canny only handles gray scale images
    IplImage* out = cvCreateImage(cvGetSize(in),IPL_DEPTH_8U,1);
    cvCanny( in, out, lowThresh, highThresh, aperture );
    return( out );
  }  

此代码没有任何编译错误。问题是它不会在窗口中显示任何图像,img3因为它会识别我作为非单通道图像提供的所有图像并输入if(in->nChannels != 1)并运行代码return(0);
我已经尝试了在我的计算机中找到的所有灰度图像并搜索了网络对于单通道图像,但它们都有这个问题。我已经为我提供的所有图像设置了一个断点并逐步尝试了代码??!!!!!!
你能告诉我什么样的图像被称为 openCV 库的单通道图像,并给我一个链接到可以与上面的代码一起使用的这种图像吗?

4

1 回答 1

3

看看这里的文档。

您用来加载图像的函数的定义是: IplImage* cvLoadImage(const char* filename, int iscolor=CV_LOAD_IMAGE_COLOR).

这意味着CV_LOAD_IMAGE_COLOR默认情况下使用它。如果您想以灰度加载图像,请CV_LOAD_IMAGE_GRAYSCALE按照评论中的建议使用 :

IplImage* in = cvLoadImage(a,CV_LOAD_IMAGE_GRAYSCALE);

总而言之,必须告诉 OpenCV 是否希望将图像加载为彩色图像或灰度图像。如果你要求它打开一个彩色图像作为灰度图像,那么 OpenCV 会简单地转换它。

于 2013-07-08T14:12:06.517 回答