Trouble using threshold() function in opencv

asked 2016-06-17 03:07:25 -0600

Akash Garg gravatar image

I am trying to segment input image using watershed method. In that i am using threshold(), however my program gets aborted with the following error on the terminal unsupported format or combination of formats () in cv::threshold The format of the function used is threshold(bw, bw, 40, 255, THRESH_BINARY); where bw is the binarised image in grayscale form.

edit retag flag offensive close merge delete

Comments

"where bw is the binarised image in grayscale form."

maybe your Mat isn't grayscale, or empty ? please check: cerr << bw.type() << " " << bw.channels() << " " << bw.size() << endl;

berak gravatar imageberak ( 2016-06-17 03:27:05 -0600 )edit

the o/p of the above command comes out to be 22 3 [259 x 194]

Akash Garg gravatar imageAkash Garg ( 2016-06-17 03:47:47 -0600 )edit

bw.convertTo(bw, CV_8UC1); gives output 16 3 [259 x 194]. Why is that the channel even now remain 3?

Akash Garg gravatar imageAkash Garg ( 2016-06-17 03:53:30 -0600 )edit
1

Try to use cvtColor().

Missing gravatar imageMissing ( 2016-06-17 03:56:14 -0600 )edit

convertTo() does not change the channel count, only the depth. (so, yes, cvtColor(src,dst,COLOR_BGR2GRAY))

berak gravatar imageberak ( 2016-06-17 04:10:06 -0600 )edit

okk thanks alot. But is there any way i can threshold a 3 channel image? Converting BGR 2 Gray before using threshold() and then converting it back to BGR, will it work?

Akash Garg gravatar imageAkash Garg ( 2016-06-18 02:33:06 -0600 )edit

no, you can't get back color from a grayscale image ..

what are you trying to achieve ?

berak gravatar imageberak ( 2016-06-18 07:05:25 -0600 )edit

Thank u so much. Got it now. GRAY2BGR will only convert single channel image to 3 channel. Earlier I thought it wud convert gray to color image!!!

Akash Garg gravatar imageAkash Garg ( 2016-06-18 07:55:49 -0600 )edit

I am basically writing an image segmentation code & my code will work only if i can make the background black objects's color other than black(then i am using watershed()). Can u suggest any way to make background black?

Akash Garg gravatar imageAkash Garg ( 2016-06-18 08:00:35 -0600 )edit