2013-07-08 27 views
0

的方法cvCanny假設我有這樣的代碼:我怎樣才能找到一個單一的通道圖像運行的OpenCV

#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庫,並給我一個鏈接到這樣的圖像,將與上面的代碼工作?

+3

嘗試'IplImage * in = cvLoadImage(a,CV_LOAD_IMAGE_GRAYSCALE);' – William

回答

3

看看文檔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); 

總之,要告訴O​​penCV的,如果你想它加載圖像爲彩色圖像或灰度圖像。如果您要求它將彩色圖像作爲灰度圖像打開,則OpenCV將簡單地轉換它。

相關問題