imbinarize not working on given image

So I have attached the image of coins I am using. I need to make this image black and white but it's just showing an area whole filled with white. When I run on coins.tif file provided in MATLAB, my code works fine but not on this image. Please tell me where I am going wrong. TIA
Image = imread('Coin.jpg');
[rows, columns, numberOfColorBands] = size(Image);
if numberOfColorBands > 1
grayImage = rgb2gray(Image);
else
grayImage = Image;
end
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
binaryImage = imbinarize(grayImage);
subplot(3, 1, 1);
imshow(Image, []);
subplot(3, 1, 2);
imshow(grayImage, []);
subplot(3, 1, 3);
imshow(binaryImage, []);
The best binary image I could get is by using this line
binaryImage = imbinarize(grayImage,'adaptive','ForegroundPolarity','bright','Sensitivity',0.13);
But still I am getting numerous white dots here and there in the black area like this.

 採用された回答

drummer
drummer 2020 年 11 月 1 日
編集済み: drummer 2020 年 11 月 1 日

0 投票

by using size, you're obtaining the dimension information of the image. So your 3rd coordinate (numberOfColorBands) would be the position in the stack (of a volumetric image), and not the # channels of your image.
Try using
info = imfinfo('yourImage')
% Then, use info in your if condition.
Cheers

5 件のコメント

SURBHI GOEL
SURBHI GOEL 2020 年 11 月 1 日
Thanks but can you please tell which field of info to use in if condition? Is it NumberOfSamples?
drummer
drummer 2020 年 11 月 1 日
Well,
info.BitDepth
didn't work?
Check the linked documentation in FYI. There's plenty of information there. Perhaps it might help you.
Cheers
SURBHI GOEL
SURBHI GOEL 2020 年 11 月 1 日
Also anyway I can directly do grayImage = rgb2gray(Image); without using the if condition, so that's not the problem. Problem is how to binarize.
drummer
drummer 2020 年 11 月 1 日
Well, I see you could get your binary image.
To improve the noise in the black background, you can dilate/erode your B&W output.
SURBHI GOEL
SURBHI GOEL 2020 年 11 月 1 日
Thanks a lot. Erosion worked.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeImage Processing and Computer Vision についてさらに検索

質問済み:

2020 年 11 月 1 日

コメント済み:

2020 年 11 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by