How can i use threshold to convert a gray-scaled image into binary image ?
287 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
More Answers (4)
Simon
on 19 Nov 2013
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;
Image Analyst
on 19 Nov 2013
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.
Ali nafaa
on 29 Nov 2022
Edited: Image Analyst
on 29 Nov 2022
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);
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!