Engee documentation
Notebook

RGB in HSV and threshold processing

This example illustrates how converting RGB to HSV and using this representation helps simplify segmentation processes.

The implementation of this feature begins with the connection of libraries.

In [ ]:
Pkg.add(["LinearAlgebra", "TestImages"])
In [ ]:
using Images, TestImages, LinearAlgebra

Let's upload a test image.

In [ ]:
rgb_img = testimage("lighthouse")
Out[0]:
No description has been provided for this image

HSV (English Hue, Saturation, Value – tone, saturation, value) is a color model in which the color coordinates are:

  1. Hue – a color tone, for example, red, green or blue;
  2. Saturation – saturation, and the higher this parameter, the "cleaner" the color.;
  3. Value – the color value.

Let's convert the original RGB image to the HSV representation.

In [ ]:
hsv_img = HSV.(rgb_img);

Let's select the channels of the HSV image.

In [ ]:
channels = channelview(float.(hsv_img));

hue_img = channels[1,:,:];
value_img = channels[3,:,:];
saturation_img = channels[2,:,:];

Then, a simple segmentation of the image can be efficiently performed by determining the threshold of the HSV channels.

In [ ]:
mask = zeros(size(hue_img))
h, s, v = 80, 150, 150
for ind in eachindex(hue_img)
    if hue_img[ind] <= h && saturation_img[ind] <= s/255 && value_img[ind] <= v/255
        mask[ind] = 1
    end
end
binary_img = colorview(Gray, mask)
Out[0]:
No description has been provided for this image

The resulting binary image can be used as a mask for the original RGB image.

In [ ]:
segmented_img = mask .* rgb_img
hcat(rgb_img, binary_img, segmented_img)
Out[0]:
No description has been provided for this image

Conclusion

In this demo, we have analyzed the RGB to HSV conversion and the use of this representation to facilitate image segmentation processes.