How to make the binarized image clearer

3 views (last 30 days)
my_image = imread('image026.gif');
my_image = my_image(:,:,1);
BW = imadjust(my_image);
BW = imbinarize(my_image);
imtool(BW)
I am using the above code to change the 'image026.gif' to binary, however it comes out as 'binary.bmp' which is due to the threshold of the imbinarize() function. I was wondering how can I change the threshold to make it more clear.

Accepted Answer

Johannes Fischer
Johannes Fischer on 20 Sep 2019
The problem why your output looks like this is not necessarily to do with the imbinarize function. The problem lies in the way that '*.gif' files work. The GIF data format does not specify an RGB triplet for each pixel but only one value between 0 and 255. This value is used with a look-up-table - also stored in the file - which maps it to an RGB triplet. That also means, that each '*.gif' cannot contain more than 256 different colors.
There is no rule on how this Look-up-table has to be ordered. That means adjacent pixels with a similar but different color, might have completely different values stored ad that position. Here is where i think one of your problems lies: The data that is given to imbinarize is not the brightness variation you see when opening the image, its the index map. In order to fix this, you need to apply the look-up-table (or map) to the data:
[my_image, map] = imread('image026.gif');
my_image_rgb = ind2rgb(my_image, map);
Now you can either decide to use the red, green or blue channel an hand it to imbinarize,
% use the green channel to binarize with a threshold of 0.5
BW = imbinarize(my_image_rgb(:, :, 2), 0.5);
or you calculate the lightness in each pixel by transforming to the L*a*b colorspace.
my_image_lab = rgb2lab(my_image_rgb);
lightness = my_image_lab(:, :, 1);
and use that in combination with imbinarize.

More Answers (1)

darova
darova on 20 Sep 2019
Pay attention that you don't extract colormap
This only extracts indexed image (see workspace, I is of size [369x574x1])
I = imread('image026.gif');
imshow(I)
And this how it looks originally
img2.png
Try
[I,map] = imread('image026.gif');
I0 = ind2rgb(I,map); % convert indexed to rgb
thresh = graythresh(I0);
I1 = im2bw(I0,thresh); % binarize image
conn = 4;
I2 = bwareaopen(~I1,5,conn); % remove areas with more than 5 pixels
% I3 = imdilate(I2,ones(3)); % can be dilated a bit
subplot(211)
imshow(I0)
subplot(212)
imshow(~I2)
Result
img12.png

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!