How can i use threshold to convert a gray-scaled image into binary image ?

44 ビュー (過去 30 日間)
Christine Ak
Christine Ak 2013 年 11 月 19 日
コメント済み: DGM 2023 年 8 月 25 日
How can I use threshold to convert a gray-scaled image into binary image , I mean to get the image just black and white ?
Does Binarization help ??
Thx

採用された回答

Jan
Jan 2013 年 11 月 19 日
編集済み: Jan 2013 年 11 月 19 日
This is straightforward:
A = imread('cameraman.tif'); % example grayscale image
threshold = 120; % custom threshold value
A_bw = A > threshold;
=======
edit: removed factor of 255 as Image Analyst pointed out

その他の回答 (4 件)

Simon
Simon 2013 年 11 月 19 日
Hi!
If you load an image into matlab, you get a matrix A (for example) of size (XxYx3) with X and Y being the number of pixels in x- and y-direction. Usually this matrix is for RGB images. Look at imread
If it is a grayscale image the values for all three colors are the same, they range between 0 and 255. You may now apply a threshold
% threshold
t = 128;
% find values below
ind_below = (A < t);
% find values above
ind_above = (A >= t);
% set values below to black
A(ind_below) = 0;
% set values above to white
A(ind_above) = 255;
  4 件のコメント
Image Analyst
Image Analyst 2023 年 3 月 23 日
@Nayana again (see my answer) there is no need to do all that and make A a double matrix of 0 and 255. You can simply do
mask = A >= t;
And of course he messes up the size of A. It's not "of size (XxYx3)". It's of size YxXx3 which is rows x columns x 3.
DGM
DGM 2023 年 8 月 25 日
(i don't remember why i parked my browser on this page, but I'll bite anyway)
One thing to start with:
% find values below
ind_below = (A < t);
% find values above
ind_above = (A >= t);
This whole baloney is unnecessary. Do the comparison once. If you need, negate the logical result, that way you know the union of masks spans the entire input. Comparing it twice just wastes time and invites room for error. That said, there's no need to do so at all.
A logical image as would result from a comparison operator (e.g. >=) is a properly-scaled image itself. It can be rescaled for viewing or saving without problem. Creating a floating-point image in uint8-scale, or otherwise blindly presuming that the input is uint8 is to create an improperly-scaled image which won't display or save correctly without intervention. Learn how images are scaled according to their numeric class.
There's no reason to create two masks. There's no reason to overwrite the input. There's no reason to need two operations to do so. Stop creating problems for yourself. As IA and Jan have posted, this whole thing reduces to a single line of code, and that single line of code is more robust than this answer.

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


Image Analyst
Image Analyst 2013 年 11 月 19 日
Yes you binarize the image by thresholding:
binaryImage = grayImage > thresholdValue;
There is no need to ever multiply by 255 that I've ever encountered. Displaying the binary (logical) image will show it as black and white even without multiplying by, or directly setting to, a value of 255.
  2 件のコメント
Jan
Jan 2013 年 11 月 19 日
Right, in general (and especially for binarized images) the scaling is obsolete...
Christine Ak
Christine Ak 2013 年 11 月 19 日
Thxxx alot

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


burçin temur
burçin temur 2020 年 5 月 28 日
how can I change the red band with the green band in image?
  3 件のコメント
burçin temur
burçin temur 2020 年 5 月 28 日
thanks
DGM
DGM 2023 年 2 月 12 日
Or just:
rgbImage = rgbImage(:,:,[2 1 3]);

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


Ali nafaa
Ali nafaa 2022 年 11 月 29 日
編集済み: Image Analyst 2022 年 11 月 29 日
x = imread('cameraman.tif');
figure,imshow(x);
[r,c] = size (x);
output=zeros(r,c);
for i = 1 : r
for j = 1 : c
if x(i,j) > 128
output(i,j)=1;
else
output(i,j)=0;
end
end
end
figure,imshow(output);
  4 件のコメント
Image Analyst
Image Analyst 2022 年 11 月 29 日
Sorry, I changed it to x and output like you used. I find it's easier for people to understand the code if you use descriptively named variables. Usually people think of x as like in an x-y graph, not a binary image. So I'd rather use "mask" or "binaryImage" rather than "output", and "grayImage" rather than "x".
Ali nafaa
Ali nafaa 2022 年 11 月 29 日
yes thank you

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

Community Treasure Hunt

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

Start Hunting!

Translated by