MATLAB: How to make the binarized image clearer

binaryimage processing

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.

Best Answer

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.