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.m348 chars13 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