To perform color thresholding in HSV space using MATLAB

To perform color thresholding in HSV space using MATLAB, follow these steps. HSV (Hue, Saturation, Value) is often preferred over RGB for color segmentation because it separates color information (hue) from brightness (value) and saturation, making it easier to isolate specific colors.


Step-by-Step Guide

1. Convert RGB Image to HSV

matlab
Copy
% Read the RGB image
rgbImage = imread('your_image.jpg');

% Convert to HSV (values normalized to [0,1])
hsvImage = rgb2hsv(rgbImage);

2. Define Thresholds for Hue, Saturation, and Value

Adjust these ranges to target your desired color:

matlab
Copy
% Example: Thresholds for "green" color
hueThreshold = [0.25, 0.45];       % Hue range (green is ~0.33 in normalized [0,1])
saturationThreshold = [0.4, 1];    % Saturation range (higher = purer color)
valueThreshold = [0.3, 1];         % Value range (avoid dark/black regions)

3. Create a Mask Based on Thresholds

Use logical indexing to combine thresholds:

matlab
Copy
% Extract H, S, V channels
H = hsvImage(:,:,1);
S = hsvImage(:,:,2);
V = hsvImage(:,:,3);

% Create a binary mask
mask = (H >= hueThreshold(1) & H <= hueThreshold(2)) & ...
       (S >= saturationThreshold(1) & S <= saturationThreshold(2)) & ...
       (V >= valueThreshold(1) & V <= valueThreshold(2));

4. Apply the Mask to the Original Image

matlab
Copy
% Use the mask to extract the segmented color
segmentedImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));

% Display results
figure;
subplot(1,2,1); imshow(rgbImage); title('Original Image');
subplot(1,2,2); imshow(segmentedImage); title('Segmented Color');

Handling Colors with Wrapped Hue (e.g., Red)

For colors like red (hue wraps around 0° and 360°), define two threshold ranges:

matlab
Copy
% Example: Red has hues near 0° and 360° (normalized to [0,1])
hueThreshold1 = [0, 0.05];    % 0° to ~18°
hueThreshold2 = [0.95, 1];    % 342° to 360°

mask = ( (H >= hueThreshold1(1) & H <= hueThreshold1(2)) | ...
         (H >= hueThreshold2(1) & H <= hueThreshold2(2)) ) & ...
       (S >= saturationThreshold(1) & S <= saturationThreshold(2)) & ...
       (V >= valueThreshold(1) & V <= valueThreshold(2));

Interactive Thresholding with colorThresholder App

For intuitive threshold selection, use MATLAB’s built-in app:

matlab
Copy
colorThresholder(rgbImage); % Opens an interactive tool
  1. Select HSV color space.
  2. Adjust sliders for H, S, and V to refine the mask.
  3. Export the code for reuse.

Post-Processing (Optional)

Clean up the mask using morphological operations:

matlab
Copy
mask = imopen(mask, strel('disk', 3));  % Remove small noise
mask = imfill(mask, 'holes');           % Fill gaps

Example Workflow for Green Objects

matlab
Copy
% Read image and convert to HSV
rgbImage = imread('fruits.jpg');
hsvImage = rgb2hsv(rgbImage);

% Define green thresholds (adjust as needed)
hueThreshold = [0.25, 0.45];
saturationThreshold = [0.4, 1];
valueThreshold = [0.3, 1];

% Create mask
H = hsvImage(:,:,1);
S = hsvImage(:,:,2);
V = hsvImage(:,:,3);
mask = (H >= hueThreshold(1) & H <= hueThreshold(2)) & ...
       (S >= saturationThreshold(1) & S <= saturationThreshold(2)) & ...
       (V >= valueThreshold(1) & V <= valueThreshold(2));

% Apply mask and display
segmentedImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
imshowpair(rgbImage, segmentedImage, 'montage');

Key Notes

  1. Normalization: MATLAB’s rgb2hsv returns values in [0,1], not [0,360] for hue.
  2. Threshold Tuning: Use the colorThresholder app for interactive adjustments.
  3. Performance: Vectorized operations (e.g., H > threshold) are faster than loops.

By following these steps, you can robustly segment colors in HSV space for tasks like object detection, robotics, or image analysis.