2014-01-28 76 views
1

當我試圖將彩色圖像轉換爲灰度時,我遇到了一個問題。錯誤是:「在cvGetSize」中的錯誤參數(數組應該是CvMat或IplImage),但是當我評論所有與灰度相關的行時,我可以設法加載原始的彩色圖像並顯示它。我該如何解決這個錯誤?在OpenCV中將彩色圖像轉換爲灰度問題

#include <opencv\cv.h> 
#include <opencv\highgui.h> 
#include <iostream> 
#include <stdio.h> 


int main(int argc, char** argv) 
{ 
    //Loading the color image 
    IplImage* frame = cvLoadImage("lena.jpg"); 
    //Converting the color image to grayscale 
    IplImage* grayframe = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1); 
    cvCvtColor(frame, grayframe, CV_RGB2GRAY); 

    //Creating a window for color image 
    cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE); 
    //Creating a window for grayscale image 
    cvNamedWindow("Example2", CV_WINDOW_AUTOSIZE); 
    // Showing the color image 
    cvShowImage("Example1", frame); 
    // Showing the grayscale image 
    cvShowImage("Example2", grayframe); 
    //Showeing for X seconds 
    cvWaitKey(2000); 
    cvReleaseImage(&frame); 
    cvDestroyWindow("Example1"); 
    cvReleaseImage(&grayframe); 
    cvDestroyWindow("Example2"); 
    return 0; 
} 
+1

兄弟,用CV ::墊用imread獲取圖像。也花了幾個小時[在這裏](http://docs.opencv.org/doc/tutorials/tutorials.html)。 – William

回答

2

爲什麼Iplimage?

嘗試使用Mat,如果您想在未來擴展示例。

Mat image = imread("lena.jpg"); 
Mat gray; 
cvtColor(image,gray,CV_BGR2GRAY); 

這會很容易,會給你灰度圖像。

但是,如果是使用C API一個具體的理由,那麼, 問題是在

IplImage* grayframe = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1); 

我不知道該具體原因,但是,我可以給你一個替代運行你的代碼。

int x= frame->width, y=frame->height; 
IplImage* grayframe = cvCreateImage(cvSize(x,y), IPL_DEPTH_8U, 1); 

酪氨酸,它可能爲你工作

+0

即使我被困在最近2天的同樣的錯誤,不知道該怎麼辦 –