Python OpenCV
About Lesson

In this Python OpenCV lesson we want to learn about Python OpenCV Reading Image in Matplotlib, so Matplotlib is a plotting library for the Python programming language and its numerical mathematics extension NumPy. It provides an object-oriented API for embedding plots into applications using general-purpose GUI toolkits like Tkinter, wxPython, Qt, or GTK+.

 

First of all we need to install matplotlib, you can use pip for the installation.

 

 

This is the complete code for this lesson.

 

 

Rest of the code is familiar for you, but you can see that at the end we have used plt.show(), and we are using matplotlib for showing of the image in opencv.

 

 

If you run the code this will be the result.

Python OpenCV Reading Image Matplotlib
Python OpenCV Reading Image Matplotlib

 

 

We have our image, but you can see a color difference in our image, it is because opencv renders the image in order of GBR, the reverse of the usual RBG format. this is the reason the colors get messed up from OpenCV to matplotlib. The matplotlib module displays images according to the more conventional RGB method. Therefore, in order to correctly render images from OpenCV in matplotlib, what we need to do is to convert the GBR image into a RGB image.

 

 

 

You can see the we have used cv2.cvtColor() for converting BGR to RGB, we will talk about the OpenCV color spaces in the later part of the course.

 

 

Run the code and this time you can see that we have a RGB color image in matplotlib.

Python OpenCV Matplotlib RGB Color
Python OpenCV Matplotlib RGB Color