Home » Python

Convert an RGB format Image in an HSV format Image using OpenCV in Python

In this article, we are going to learn how to convert an RGB format image Into an HSV format image using OpenCV in Python?
Submitted by Ankit Rai, on May 03, 2019

An HSV is another type of color space in which H stands for Hue, S stands for Saturation and V stands for Value.

A Hue represents color. It is an angle from 0 degrees to 360 degrees.

Angle Color
0-60 Red
60-120 Yellow
120-180 Green
180-240 Cyan
240-300 Blue
300-360 Magenta

Saturation: It indicates the range of grey in the color space. It ranges from 0 to 100%. Sometimes the value is calculated from 0 to 1. When the value is '0,' the color is grey and when the value is '1,' the color is a primary color.

Value is the brightness of the color and varies with color saturation. It ranges from 0 to 100%. When the value is '0' the color space will be totally black. With the increase in the value, the color space brightness up and shows various colors.

In this program, we will be using three functions of OpenCV-python (cv2) module. let's see their syntax and descriptions first:

1) imread():
It takes an absolute path/relative path of your image file as an argument and returns its corresponding image matrix.

2) imshow():
It takes window name and image matrix as an argument in order to display an image in a display window with a specified window name.

3) cv2.cvtColor():
It takes image matrix and a flag for changing color-space from one color space to another(in this case we are using BGR2HSVcolor-space conversion) and returns the newly converted image matrix.

Python program to convert an RGB format Image in an HSV format Image

# open-cv library is installed as cv2 in python
# import cv2 library into this program
import cv2

# read an image using imread() function of cv2
# we have to  pass only the path of the image
img = cv2.imread(r'C:/Users/user/Desktop/pic1.jpg')

# displaying the image using imshow() function of cv2
# In this : 1st argument is name of the frame
# 2nd argument is the image matrix
cv2.imshow('original image',img)

# converting the colourfull image into HSV format image
# using cv2.COLOR_BGR2HSV argument of
# the cvtColor() function of cv2
# in this :
# ist argument is the image matrix
# 2nd argument is the attribute
HSV_img = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)

# displaying the Hsv format image
cv2.imshow('HSV format image',HSV_img)

Output

Convert an RGB format Image in an HSV format Image in Python - output




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.