Open In App

How to Color Slicing Using HSV Color Space in MATLAB?

Last Updated : 08 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Color slicing is a technique in image processing, which lets us separate certain objects from their surroundings. Furthermore, it works by filtering through only a certain band of the color spectrum, essentially erasing all other colors beyond it. In this article, you will learn how to perform color slicing in HSV color space using MATLAB.

It is advised to have an understanding of HSV color space before continuing in this article. 

The HSV (Hue Saturation Value) scale provides a numerical readout of an image that corresponds to the color names contained therein. Hue is measured in degrees from 0 to 360. For instance, cyan falls between 180–240 degrees, and magenta falls between 301–360 degrees. The value and saturation of color are both analyzed on a scale of 0 to 100 percent. Most digital color pickers are based on the HSV scale, and HSV color models are particularly useful for selecting precise colors for art, color swatches, and digital graphics. You can learn more about the HSV color space over here

Example 1:

Matlab




% MATLAB program for Color slicing
% using HSV color space in MATLAB?
 img = imread('test.jpg');
 
% Converting from RGB color mode to HSV
 HSV = rgb2hsv(img);
 
% Obtaining Hue channel/matrix of the HSV color space
 H = HSV(:,:,1);
 
% Converting All hue values lower than the mean
% value of Hue channel to .7(blue)
 H( H > mean2(H) ) = .7;
 
% Replacing the original HSV channel by the current one
 HSV(:,:,1) = H;
 
% Converting the HSV channel to RGB for displaying
 C = hsv2rgb(HSV);
 figure,imshow(C);title('Color Sliced to Blue');


Input images:

 

 

Output:

 

 

Explanation:

The image is firstly read and the pixel data is stored in variable img. Then the color space of the image was converted from RGB to HSV using the rgb2hsv function. The hue channel is obtained by HSV(:,:,1) (similar 2 in the ending argument is for saturation, and 3 for Value) . After which all the values of the channel are passed through a constraint that sets pixel values in the channel which are lower than the mean to .7. Where .7 is used to represent blue (.7 * 360 = 252, which corresponds to the blue color). After passing the channel matrix through the constraint we replace the original Hue channel of the Image with the one which is produced conditionally. Then the image is converted back to RGB color space. In the end, the image is displayed. 

NOTE: Certain things to keep in mind while using the above code:

  • The value provided on the right-hand side of the constraint must be less than 1 and greater than 0, in the case of hue. All the possible values in the Hue channel could be represented by the following formula:
RHS = (Hue in Degrees)/360

 This RHS is placed in

H( H > mean2(H) ) = RHS;
  • Color slicing could also be performed on Saturation and Value channels. But the effect produced is more abrupt than in the case of Hue. 
  • The effect that a particular channel has on the image could be viewed by displaying the channel as an image. This would give us a grayscale image where the intensity of the greys represents the effect that particular channel has on the image. Ex. adding the following statement
figure,imshow(H);

 After the third statement of the code will display the following image:

 

 

which is a grayscale image where the regions which have higher intensity, represent that the channel (Hue in this case) has a high effect in the region and vice versa. This is the reason why changing the hue had a distinguishable effect in those regions, then the rest of the image. 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads