Search code examples
pointersopencvvisual-c++-2010

OpenCV Visual-C++ cvSplit Problem


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);

Solution

  • if cvloadImage is returning a pointer to an iplImage then you don't need the '&' on img it's already a pointer

    cvSplit takes cvMat* a iplImage* isn't the same, you need to convert it to a cvmat see http://opencv.willowgarage.com/documentation/cpp/c++_cheatsheet.html