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

287 views (last 30 days)
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 ??

Accepted Answer

Jan on 19 Nov 2013
Edited: Jan on 19 Nov 2013
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

More Answers (4)

Simon on 19 Nov 2013
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
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.

burçin temur
burçin temur on 28 May 2020
how can I change the red band with the green band in image?

Ali nafaa
Ali nafaa on 29 Nov 2022
Edited: Image Analyst on 29 Nov 2022
x = imread('cameraman.tif');
[r,c] = size (x);
for i = 1 : r
for j = 1 : c
if x(i,j) > 128

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!