To convert a grayscale image to binary in MATLAB, you typically use the im2bw
function, which performs thresholding based on a specified level.
Using the im2bw
Function
The most common and direct way to convert a grayscale image to a binary image in MATLAB is by using the im2bw
function. This function requires a threshold level to distinguish between black and white pixels.
As described in the reference, the syntax is:
BW = im2bw( I , level )
I
: This is the input grayscale image.level
: This is the threshold value. Pixels inI
with a luminance greater thanlevel
are converted to the value 1 (white) in the output binary imageBW
. All other pixels (those with luminance less than or equal tolevel
) are converted to the value 0 (black). The range of thelevel
is relative to the signal levels possible for the image's class (e.g., for auint8
image, the range is typically [0, 1], where 0 corresponds to 0 and 1 corresponds to 255).BW
: This is the resulting binary image, which will have logical values (true for 1, false for 0).
Understanding the Threshold level
Choosing the correct level
is crucial for effective binarization.
- Manual Thresholding: You can manually pick a value based on visual inspection or prior knowledge.
- Automatic Thresholding: MATLAB provides functions like
graythresh
that automatically compute a global threshold using Otsu's method, which is often a good starting point.
Step-by-Step Conversion Process
Here's a typical workflow to convert a grayscale image to binary using im2bw
:
- Load the Image: Read the image into MATLAB. Ensure it's a grayscale image. If it's color, you'll need to convert it first (e.g., using
rgb2gray
). - Determine the Threshold: Decide on the threshold level (
level
). You can calculate it automatically or set it manually. - Apply
im2bw
: Use theim2bw
function with your grayscale image and the chosen threshold. - Display the Result: Show the original grayscale image and the resulting binary image.
Practical Examples
Here are a couple of common scenarios:
Example 1: Using Automatic Thresholding (graythresh
)
This is a common approach when you don't have a predefined threshold.
% 1. Load the image (replace 'your_image.jpg' with your file)
img = imread('your_image.jpg');
% Convert to grayscale if it's a color image
if size(img, 3) == 3
gray_img = rgb2gray(img);
else
gray_img = img; % It's already grayscale
end
% 2. Determine the threshold automatically using Otsu's method
% graythresh returns a normalized threshold value [0, 1]
level = graythresh(gray_img);
% 3. Apply im2bw using the calculated level
% BW = im2bw( I , level )
binary_img = im2bw(gray_img, level);
% 4. Display the results
figure;
subplot(1, 2, 1);
imshow(gray_img);
title('Original Grayscale Image');
subplot(1, 2, 2);
imshow(binary_img);
title('Binary Image (Automatic Threshold)');
Example 2: Using a Manual Threshold
If you know a suitable threshold value, you can set it directly. Remember that the level
value depends on the image's data type (e.g., for uint8
, a level
of 0.5 corresponds to a pixel value of 127.5).
% 1. Load the image
img = imread('your_image.jpg');
% Convert to grayscale if necessary
if size(img, 3) == 3
gray_img = rgb2gray(img);
else
gray_img = img;
end
% 2. Set a manual threshold level (adjust this value as needed)
% For a uint8 image, a level of 0.5 means threshold at pixel value 127.5
manual_level = 0.5; % Example: threshold at the midpoint
% 3. Apply im2bw using the manual level
% BW = im2bw( I , level )
binary_img_manual = im2bw(gray_img, manual_level);
% 4. Display the results
figure;
subplot(1, 2, 1);
imshow(gray_img);
title('Original Grayscale Image');
subplot(1, 2, 2);
imshow(binary_img_manual);
title('Binary Image (Manual Threshold)');
Key Concepts
- Binary Image: An image where each pixel can only have one of two values, typically 0 (black) or 1 (white).
- Thresholding: The process of converting a grayscale image into a binary image by choosing a cutoff value (the threshold). Pixels above the threshold are assigned one value (e.g., 1), and pixels at or below the threshold are assigned the other value (e.g., 0).
By using the im2bw
function with an appropriate threshold level, you can effectively convert a grayscale image into a binary format in MATLAB.