Thresholding of a color image (Three-channels RGB)

317 views
Skip to first unread message

h.ben...@gmail.com

unread,
Sep 9, 2015, 8:36:13 AM9/9/15
to scikit-image
Hi,

I have a very basic question about thresholding in Scikit-image (or maybe it's actually a numpy question).
I'm performing a simple otsu thresholding on an image, that I've read previously:


img
= io.imread('flood.jpg')
print img.shape

# otsu thresholding of the image
cell2
= fig.add_subplot(1, 2, 2)
threshold
= threshold_otsu(img)
thresholded_img
= img > threshold

Everything is working, however I've a question concerning the thresholding on a multi-dimensional of the image read.
I noticed that image has been loaded as three-channels RGB; How is the thresholded RGB image drawn on the screen?

Knowing that the thresholded_img object is a boolean numpy array still having three dimensions (the three color axes). I mean, if it had only one channel, it would be easy True=255 and False=0; but in the case of a 3-channels image, is a logical-AND performed on the RGB-image to flatten it to a single-channel image?

Thanks.

Emmanuelle Gouillart

unread,
Sep 9, 2015, 8:41:14 AM9/9/15
to scikit...@googlegroups.com
Hi,

thanks for your question.

The first line of the threshold_otsu function is
hist, bin_centers = histogram(image.ravel(), nbins)
meaning that all channels are combined together with ravel (in other
words, the algorithm behaves as if the color image were a 3-D image).

I think that this is a bug and that the function should behave in a
different way.

An option for you is to convert the image to grayscale before using
threshold_otsu

Cheers,
Emmanuelle

h.ben...@gmail.com

unread,
Sep 9, 2015, 9:23:52 AM9/9/15
to scikit-image
Hi,

Thanks for your reponse.
You're right, the image needs to be converted to gray scale (single channel) before being given as input to threshold_otsu.
Indeed, in the present case, the threshold is computed for the flatten 1D array of all three channels, inside the otsu_threshold function.

However, I'm getting better results with the previous code than with the thresholding of the gray scale image for this image: http://www.e-geos.it/gallery/featured/flooding_veneto/CSKS1_SCS_B_HI_08_HH_RD_SF_20101103172214_20101103172222_orto_hh_slc_geo_subset.jpg. Maybe, it's because the threshold is computed from the accumulated intensities of all the channels.

 Maybe I could obtain similar results if I perform the otsu thresholding on all three channels and try to stack them (OR operator).

Emmanuelle Gouillart

unread,
Sep 9, 2015, 9:53:51 AM9/9/15
to scikit...@googlegroups.com
The image you link to is almost grayscale already, meaning that the three
channels have comparable values. With an image where one of the channels
is dominant (like the coffee image of our data module), I think the
results would be very different.

Nice image by the way! Out of curiosity, what is your application?

Emma

h.ben...@gmail.com

unread,
Sep 9, 2015, 12:35:30 PM9/9/15
to scikit-image
Correct, the weird effect I was getting in the display was because I was using Matplotlib to display only 1-channel for both the original image and the thresholded image. Because of the 1-channel image having just 2-dimensions (array[x, y]) it was affecting synthetic colors to it, and maybe that's why the result was different than with the RGB one. I need to check if Scikit-image allows to show both images at the same time.

I'm working on Flood mapping using Satellite radar images, so I will be using images 10x larger than this one
Reply all
Reply to author
Forward
0 new messages