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

212 views (last 30 days)
Christine Ak on 19 Nov 2013
Commented: burçin temur on 28 May 2020
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 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
Christine Ak on 19 Nov 2013
Thxxxx :d

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;
Christine Ak on 19 Nov 2013
Thxxx Alot

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.
##### 2 CommentsShowHide 1 older comment
Christine Ak on 19 Nov 2013
Thxxx alot

burçin temur on 28 May 2020
how can I change the red band with the green band in image?
##### 2 CommentsShowHide 1 older comment
burçin temur on 28 May 2020
thanks