visual-studio-2010 - 如何找到单 channel 图像来运行 openCV 的方法 cvCanny

标签 visual-studio-2010 visual-c++ opencv image-processing

假设我有这个代码:

#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因为它将我提供的所有图像识别为非单 channel 图像并输入if(in->nChannels != 1)并运行代码 return(0);我已经尝试了在我的计算机中找到的所有灰度图像,并在网上搜索了单 channel 图像,但它们都有这个问题。我已经为我提供的所有图像设置了一个断点并逐步尝试了代码??!!!!!!
你能告诉我什么样的图像被称为 openCV 库的单 channel 图像,并给我一个链接到可以与上面的代码一起使用的这种图像吗?

最佳答案

查看文档 here .

用于加载图像的函数的定义是: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 会简单地转换它。

关于visual-studio-2010 - 如何找到单 channel 图像来运行 openCV 的方法 cvCanny,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17527479/

相关文章:

c# - 如何完全禁用已签名的程序集?

visual-studio-2010 - Visual Studio Productivity Power Tools 创建的 .sln.docstates 文件是什么?

c++ - 我怎么知道 C++ 编译器是否生成线程安全的静态目标代码?

c++ - 简单 for 循环的意外无意义优化尝试(在 Visual C++ 2017 中)

python - OpenCV/ python : multi-threading for live facial recognition

android - OpenCv4Android 和 C++ 数据类型之间的混淆

c++ - 从 sprintf 获取 STR16

asp.net - 我应该在网站项目中使用 aspnet_compiler 吗?

c++ - C++中的内存扫描

windows - Affectiva emotion SDK - 无法使用 VideoDetector 处理 webm 文件