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.
Pkg.add(["LinearAlgebra", "TestImages"])
using Images, TestImages, LinearAlgebra
Let's upload a test image.
rgb_img = testimage("lighthouse")
HSV (English Hue, Saturation, Value – tone, saturation, value) is a color model in which the color coordinates are:
- Hue – a color tone, for example, red, green or blue;
- Saturation – saturation, and the higher this parameter, the "cleaner" the color.;
- Value – the color value.
Let's convert the original RGB image to the HSV representation.
hsv_img = HSV.(rgb_img);
Let's select the channels of the HSV image.
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.
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)
The resulting binary image can be used as a mask for the original RGB image.
segmented_img = mask .* rgb_img
hcat(rgb_img, binary_img, segmented_img)
Conclusion
In this demo, we have analyzed the RGB to HSV conversion and the use of this representation to facilitate image segmentation processes.


