1

I tried several methods to open an image and split the channels. I just want 3 Matrix to work with. I don't know whats wrong. Here my code:

    IplImage* img = cvLoadImage( "C:\\foo.jpg" ); 
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE ); 
cvShowImage( "Example1", img );
std::cout << "Hight: " << img->height << " Width: " << img->width;

CvMat* imgR= cvCreateMat(img->width,img->height,CV_8UC1);
CvMat* imgG= cvCreateMat(img->width,img->height,CV_8UC1);
CvMat* imgB= cvCreateMat(img->width,img->height,CV_8UC1);

cvSplit(&img, imgB, imgG, imgR, NULL);

cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "Example1" );
return 0;

The problem is the line cvSplit(&img, imgB, imgG, imgR, NULL);. The program always crash and I don't know why.

Edit1: Exception:

error -(206) Unrecognized or unsupported array type

Edit2: If i use img instead of &img I get this exception:

An error occurred.
..\..\..\..\ocv\opencv\src\cxcore\cxconvert.cpp:877: error: (-215) dvec[j].size(
) == src.size() && dvec[j].depth() == src.depth() && dvec[j].channels() == 1 &&
i < src.channels()

Solution: I was a not aware of the differnce between cv::Mat, cvMat and IplImage. This is the solution:

    IplImage *r = cvCreateImage(cvGetSize(img), img->depth, 1);
    IplImage *g = cvCreateImage(cvGetSize(img), img->depth, 1);
    IplImage *b = cvCreateImage(cvGetSize(img), img->depth, 1);

    cvSplit(img, b, g, r, NULL);
4

1 回答 1

2

如果 cvloadImage 返回一个指向 iplImage 的指针,那么你不需要 img 上的 '&' 它已经是一个指针

cvSplit 需要 cvMat* 和 iplImage* 不一样,您需要将其转换为 cvmat 参见http://opencv.willowgarage.com/documentation/cpp/c++_cheatsheet.html

于 2011-08-06T23:55:00.280 回答