In this tutorial, we will see how to change the color of an image from one color space to another using python open-cv, which exists as cv2 (computer vision) library.
Table of Contents
cvtColor()
method of cv2
library to convert the color of an image from one color space to another. To use cv2 library, you need to import cv2 library using import statement
.
There are more than 150 shading space transformation techniques
accessible in OpenCV. In any case, we will investigate just two which are most broadly utilized ones, BGR Gray
and BGR HSV.
BGR –> Blue Green Red
HSV –> Hue Saturation ValuesNote :
1) For BGR, Blue,Green,Red value range is [0,255] 2) For HSV, Hue range is [0,179], Saturation range is [0,255] and Value range is [0,255].
Now let’s see the syntax and return value of cv2 cvtColor()
the method first, then we will move on to the examples.
Syntax
1 2 3 |
cv2.cvtColor(image, code, dst, dstCn) |
Parameters
You can pass four parameters to cvtColor() method. Among the four parameters, the first 2(image and code) are mandatory; rest(dst and dstCn) are optional.
-
- Necessary parameters are:
image
: Source/Input image of n-dimensional array.code
: Conversion code for color space.
Optional parameters are:
dst:
Output image of the same size and depth as source.dstCn:
The number of channels in the destination image. If you put parameter as 0, the number of the channels is obtained automatically from image and code.
Return Value :
It returns the Coverted color space image.
The default color format in OpenCV is often referred to as RGB, but it is actually BGR (the bytes are reversed). So the first byte in a standard (24-bit) color image will be an 8-bit Blue component, the second byte will be Green, and the third byte will be Red. The fourth, fifth, and sixth bytes would then be the second pixel (Blue, then Green, then Red), and so on.
cv2 cvtColor() Method examples
Now Let’s see the Python code :
Example 1: Convert image from BGR color space to GRAY color space.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# import computer vision library(cv2) in this code import cv2 # main code if __name__ == "__main__" : # mentioning absolute path of the image img_path = "C:\\Users\\user\\Desktop\\flower.jpg" # read/load an image image = cv2.imread(img_path) # show the input image on the newly created window cv2.imshow('input image',image) # convert image from BGR color space to GRAY color space convert_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY ) # show the output image on the newly created window cv2.imshow('output image',convert_image) |
Example 2: Convert image from BGR color space to HSV color space.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# import computer vision library(cv2) in this code import cv2 # main code if __name__ == "__main__" : # mentioning absolute path of the image img_path = "C:\\Users\\user\\Desktop\\flower.jpg" # read/load an image image = cv2.imread(img_path) # show the input image on the newly created window cv2.imshow('input image',image) # convert image from BGR color space to HSV color space convert_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) # show the output image on the newly created window cv2.imshow('output image',convert_image) |
Source
https://docs.opencv.org/2.4/modules/imgproc/doc/miscellaneous_transformations.html
That’s all about Python cv2 cvtColor() Method.