How to Color Slicing Using HSV Color Space in MATLAB?


In digital image processing, the process of extracting a specific color or color range from an image with all other colors are discarded is referred to as color slicing. In an image, the color slicing is performed by creating a binary mask that selects all the pixels in the image that lies in the range of the specified color.

Coloring slicing is generally used in specific color spaces such as RGB (Red−Green−Blue) color space or HSV (Hue−Saturation−Value) color space, etc.

In this article, we will learn how to perform color slicing in HSV color space using MATLAB programming.

In the HSV color space, the color slicing is performed by considering the hue components of the image. Where, the term ‘hue’ represents the color tone. The range from 0 to 1 represents the entire color spectrum of the hue value in the HSV color space.

The color slicing is widely used in image processing for performing various tasks like removing background, image segmentation, object recognition, and more.

Now, let us understand the process of color slicing in the HSV color space with the help of MATLAB example programs.

Perform Color Slicing from Red Color to Green Color in an Image

Example

% MATLAB program to perform color slicing to green using HSV color space
% Read the input image
in_img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425236.jpg');

% Convert the image from RGB format to HSV format
hsv = rgb2hsv(in_img);

% Extract the Hue channel of the HSV image
h = hsv(:, :, 1);

% Change all hue values within the red color range to green color
red_range = [0, 0.1]; % Red color range in HSV
h(h >= red_range(1) | h <= red_range(2)) = 0.3333; % Green color Hue value is 0.3333

% Replace the original Hue channel in the HSV image with the modified Hue channel
hsv(: , :, 1) = h;

% Convert the modified HSV image back to RGB image
out_img = hsv2rgb(hsv);

% Display the orignal image and the color-sliced image in green
figure;
subplot(1, 2, 1); imshow(in_img); title('Original Image');
subplot(1, 2, 2); imshow(out_img); title('Color Sliced to Green');

Output

Code Explanation

In this MATLAB code, we start by reading an RGB image by using the ‘imread’ function and store it in the variable ‘in_img’. Next, we convert the input image from RGB color space to HSV color space by using the ‘rgb2hsv’ function and store in a variable ‘hsv’. Then, we extract the hue channel of the HSV image and store it in a variable ‘h’.

After that we specify the red color range ‘red_range’ in HSV color space. Then, we set all hue values in this range to 0.3333 that is the hue value for green color in HSV space.

Next, we replace the original Hue channel ‘hsv’ in the HSV image with the modified Hue channel ‘h’ to apply color slicing. Then, we convert the modified HSV image back to the RGB image by using the ‘hsv2rgb’ function to obtain the color−sliced RGB image and store the result in the variable ‘out_img’.

Finally, we use the ‘imshow’ function to display the original image and the color−sliced to green image.

Perform Color Slicing from Red to Violet Color in an Image

Example

% MATLAB program to perform color slicing using HSV color space
% Read the input image
in_img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425236.jpg');

% Convert the image from RGB format to HSV format
hsv = rgb2hsv(in_img);

% Extract the Hue channel of the HSV image
h = hsv(:, :, 1);

% Change all hue values within the red color range to violet color
red_range = [0, 0.1]; % Red color range in HSV
h(h >= red_range(1) | h <= red_range(2)) = 0.8; % Violet color code in HSV is 0.8

% Replace the original Hue channel in the HSV image with the modified Hue channel
hsv(:, :, 1) = h;

% Convert the modified HSV image back to RGB iimage
out_img = hsv2rgb(hsv);

% Display the orignal image and the color-sliced image in violet
figure;
subplot(1, 2, 1); imshow(in_img); title('Original Image');
subplot(1, 2, 2); imshow(out_img); title('Color Sliced to Violet');

Output

Code Explanation

In this MATLAB program, the code implementation and execution are the same as that of the MATLAB program (1). The only difference is that in this MATLAB program, the color slicing is performed for red to violet color, where the hue value for violet color specified is 0.8 in HSV color space.

Hence, in this article, we have explained the color slicing of image in HSV color using MATLAB programming.

Updated on: 07-Aug-2023

111 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements