threshold operation on image in matlab

Thresholding is a common image processing technique used to separate an image into foreground and background regions. In MATLAB, you can perform thresholding on graylevel images to create binary images. The following code demonstrates how to perform thresholding using the built-in im2bw function:

main.m
% Read in graylevel image
I_gray = imread('image.jpg');

% Set threshold value
threshold = 128;

% Perform thresholding to create binary image
I_binary = im2bw(I_gray, threshold/255);

% Display original and binary image side-by-side
subplot(1,2,1), imshow(I_gray), title('Original Image');
subplot(1,2,2), imshow(I_binary), title('Binary Image');
348 chars
13 lines

In the above code, imread is used to read in the graylevel image. A threshold value is then set (in this case, a value of 128 is used). im2bw is then used to perform the thresholding operation on the image, converting it to a binary image. Finally, the original and binary images are displayed side-by-side using the subplot and imshow functions.

Note that the threshold value is scaled to be between 0 and 1 by dividing by 255, which is the maximum pixel value for an 8-bit grayscale image. If your image has a different bit depth, you will need to scale the threshold value accordingly.

gistlibby LogSnag