Convert BGR and RGB with Python – OpenCV
OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human. In this article, we will convert a BGR image to RGB with python and OpenCV.
OpenCV uses BGR image format. So, when we read an image using cv2.imread() it interprets in BGR format by default.
We can use cvtColor() method to convert a BGR image to RGB and vice-versa.
- cv2.COLOR_BGR2RGB – BGR image is converted to RGB.
- cv2.COLOR_RGB2BGR – RGB image is converted to BGR.
Converting a BGR image to RGB and vice versa can have several reasons, one of them being that several image processing libraries have different pixel orderings.
- Import module
- Read image
- Convert it using cvtColor()
- Add wait key
- Add destroy window mechanism
Image used: Apple
First, we will display the image as it is imported which means in BGR format.
Now to convert BGR to RGB implementation is given below.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course