Search code examples
copencvcodeblockswatershed

cvWatershed unsupported format or combination of formats


I'm working with OpenCV 2.4.11 in C on Code::Blocks, in particular through the O'Reilly book Learning OpenCV. The section on the watershed algorithm was a bit short, so I thought I'd play with it a bit to see how exactly it works. However, every time I call the function I get the following error:

OpenCV Error: Unsupported format or combination of formats (Only 32-bit, 1-chann el output images are supported) in cvWatershed

My program so far is very simple:

int main(int arg, int arg2) {
//open windows
cvNamedWindow("Input", 1 );
cvNamedWindow("Markings", 1 );

//load images
IplImage* input = cvLoadImage("ActualDoorPhoto.jpg", CV_LOAD_IMAGE_COLOR);
assert(input != NULL);

IplImage* markingstemp = cvLoadImage("ActualMarkingTest.jpg", CV_LOAD_IMAGE_COLOR);
assert(markingstemp != NULL);

//prepare markings
IplImage* markings = cvCreateImage(cvGetSize(markingstemp), 32, 1);
CvMat* markmat = cvCreateMat(input->width, input->height, CV_32FC1);

cvWatershed(input, markmat);

cvShowImage("Input", input);
cvShowImage("Markings", markings);
cvWaitKey(0);

return 0;
}

I have tried putting both markings and markmat as the second argument for cvWatershed, as well as several other things (notably markings with the contours of markingstemp drawn onto it), but every time I get the same error. Can anyone tell me what I'm doing wrong?


Solution

  • You're inverting the dimensions of the output matrix. It should be:

    CvMat* markmat = cvCreateMat(input->height, input->width, CV_32FC1);
    

    The format should also probably be changed to CV_32SC1.