user824624
user824624

Reputation: 8080

OpenCV Error: Bad number of channels (Source image must have 1, 3 or 4 channels)

I am using opencv to find the contours, and show each contour on the image frame, I had seen the example using converting contour into rectangle like

`boundingRect( Mat(contours_poly[i]) );`

so I think it might be ok to work cv::imshow("parking2", Mat(contours[i]));

cv::findContours(img_resized,contours,CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);
cout<<"contour size   "<<contours.size()<<endl;
for(int i = 0;i <contours.size();i++){
  cv::imshow("parking2", Mat(contours[i]));
}

But ultimately, it doesn't work , giving me the error - OpenCV Error: Bad number of channels (Source image must have 1, 3 or 4 channels)

Note: the original image is a grey image not RGB image.

Upvotes: 0

Views: 5417

Answers (1)

berak
berak

Reputation: 39816

sorry for being lazy / unclear the other day, but even if you put your contours-pointlist into a Mat, that does not make it an image. it should probably be:

Rect br = boundingRect( contours_poly[i] ); // the rect containing all points of the contour

Mat cropped(img_resized, br);               // the cropped part of the original image 

imshow( "parking2", cropped );              // show cropped part only

also, if you repeatedly call imshow with the same windowname in a loop, you'll only see the last image, as one call will overwrite the other

Upvotes: 0

Related Questions