Thresholding Gray to B&W

 avatar
unknown
matlab
5 months ago
671 B
6
Indexable
% Reading an image : I stores the pixel wise colour values of the image
I = imread("cameraman.tif"); % tif is used for grayscale images
[m,n] = size(I); % reading the dimensions of the image

J = zeros(m,n); % creating a blank matrix filled with zeros


T = 150; % threshold value

for i = 1:m % row wise matrix traversal
    for j = 1:n % column wise matrix traversal
        if( I(i,j) <  T) 
            J(i,j) = 0; %black
        else
            J(i,j) = 255; %white
        
        end; % ending of if condition
    end; % ending of inner for loop
end; % ending of outer for loop

figure, subplot (1,2,1), imshow(I);
subplot(1,2,2), imshow(J);
Editor is loading...
Leave a Comment