Updated April 19, 2023
Introduction to OpenCV cvtColor
The colors present in a given image is represented by color spaces in OpenCV and there are several color spaces each having its own importance and some of the color spaces are RGB, CMYK, etc. and whenever there is a need to convert a given image from one color space to another color space in OpenCV, then we make use of a function called cvtColor() function and there are more than 150 color space conversions codes available in OpenCV, some of them are cv2.COLOR_BGR2GRAY, cv2.COLOR_BGR2HSV etc. and color space conversion using cvtColor() function is very useful to solve problems in the area of computer vision.
The syntax to define cvtColor() function in OpenCV is as follows:
cvtColor(image, code)
where the image is the image whose color space is to be converted,
code is the color space conversion code.
How to the working of cvtColor() Function in OpenCV?
Working of cvtColor() function in OpenCV is as follows:
- While dealing with the problems in the area of computer vision, sometimes it is necessary to convert the color space of a given to another color space.
- Whenever there is a need to convert the color space of a given image to another color space, we make use of a function called cvtColor() function in OpenCV.
- The cvtColor() function in OpenCV takes two parameters namely image and code where the image is the image whose color space is to be converted to different color space and the code represents the color conversion code.
- There are more than 150 color space conversion codes available in OpenCV.
- Some of the color space conversion codes are cv2.COLOR_BGR2GRAY, cv2.COLOR_BGR2HSV etc.
- The cvtColor() function returns the image with changed color space.
Examples
Let’s discuss examples of OpenCV cvtColor.
Example #1
OpenCV program in python to demonstrate cvtColor() function using which we are going to change the color space of a given image to different color space and display the resulting image with changed color space as the output on the screen:
Code:
#importing the module cv2
import cv2
#reading the image which is to be converted to HSV color space
image1 = cv2.imread('C:/Users/admin/Desktop/logo.png')
#converting the image to HSV color space using cvtColor function and rewriting the resulting image to a new image
imageresult = cv2.cvtColor(image1, cv2.COLOR_BGR2HSV)
cv2.imwrite("C:/Users/admin/Desktop/logo1.png", imageresult)
The output of the above program is shown in the snapshot below:
In the above program, we are importing the cv2 module. Then we are reading the image whose color space is to be converted using imread() function. Then we are using cvtColor() function by specifying the color space conversion code to convert the color space of the given image to a different color space. The resulting image is displayed as the output on the screen. The output is shown in the snapshot above.
Example #2
OpenCV program in python to demonstrate cvtColor() function using which we are going to change the color space of a given image to different color space and display the resulting image with changed color space as the output on the screen:
Code:
#importing the module cv2
import cv2
#reading the image which is to be converted to Gray color space
image1 = cv2.imread('C:/Users/admin/Desktop/logo.png')
#converting the image to Gray color space using cvtColor function and rewriting the resulting image to a new image
imageresult = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
cv2.imwrite("C:/Users/admin/Desktop/logo1.png", imageresult)
The output of the above program is shown in the snapshot below:
In the above program, we are importing the cv2 module. Then we are reading the image whose color space is to be converted using the imread() function. Then we are using the cvtColor() function by specifying the color space conversion code to convert the color space of the given image to a different color space. The resulting image is displayed as the output on the screen. The output is shown in the snapshot above.
Example #3
OpenCV program in python to demonstrate cvtColor() function using which we are going to change the color space of a given image to different color space and display the resulting image with changed color space as the output on the screen:
Code:
#importing the module cv2
import cv2
#reading the image which is to be converted to Gray color space
image1 = cv2.imread('C:/Users/admin/Desktop/tree.jpg')
#converting the image to Gray color space using cvtColor function and rewriting the resulting image to a new image
imageresult = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
cv2.imwrite("C:/Users/admin/Desktop/logo1.jpg", imageresult)
The output of the above program is shown in the snapshot below:
In the above program, we are importing the cv2 module. Then we are reading the image whose color space is to be converted using the imread() function. Then we are using the cvtColor() function by specifying the color space conversion code to convert the color space of the given image to a different color space. The resulting image is displayed as the output on the screen. The output is shown in the snapshot above.
Example #4
OpenCV program in python to demonstrate cvtColor() function using which we are going to change the color space of a given image to different color space and display the resulting image with changed color space as the output on the screen:
Code:
#importing the module cv2
import cv2
#reading the image which is to be converted to HSV color space
image1 = cv2.imread('C:/Users/admin/Desktop/plane.jpg')
#converting the image to HSV color space using cvtColor function and rewriting the resulting image to a new image
imageresult = cv2.cvtColor(image1, cv2.COLOR_BGR2HSV)
cv2.imwrite("C:/Users/admin/Desktop/lo.jpg", imageresult)
The output of the above program is shown in the snapshot below:
In the above program, we are importing the cv2 module. Then we are reading the image whose color space is to be converted using the imread() function. Then we are using the cvtColor() function by specifying the color space conversion code to convert the color space of the given image to a different color space. The resulting image is displayed as the output on the screen. The output is shown in the snapshot above.
Conclusion
In this article, we have learned the concept of cvtColor() function through definition, syntax, and working of cvtColor() function with corresponding programming examples and their outputs to demonstrate them.
Recommended Articles
We hope that this EDUCBA information on “OpenCV cvtColor” was beneficial to you. You can view EDUCBA’s recommended articles for more information.