How to make the binarized image clearer

2 ビュー (過去 30 日間)
Alphonse Do
Alphonse Do 2019 年 9 月 20 日
回答済み: Johannes Fischer 2019 年 9 月 20 日
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.

採用された回答

Johannes Fischer
Johannes Fischer 2019 年 9 月 20 日
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.

その他の回答 (1 件)

darova
darova 2019 年 9 月 20 日
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

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by