Life2Coding
How to Display a Matplotlib RGB Image using OpenCV in Python

This post will be helpful in learning OpenCV using Python programming. Here I will show how to implement OpenCV functions and apply them in various aspects using some great examples. Then the output will be visualized along with the comparisons.

We will also discuss the basic of image processing and provide the detail explanation related to the OpenCV functions.

Requirements:

First, you need to setup your Python Environment with OpenCV. You can easily do it by following Life2Coding’s tutorial on YouTube: Linking OpenCV with Python 3

Goals:

The goal is to make you understand how to display opencv BGR image as Matplotlib plot images.

Documentation:

imread()

retval=cv.imread(filename[, flags])

Loads an image from a file.

Parameters
filename Name of file to be loaded.
flags Flag that can take values of cv::ImreadModes

split()

mv=cv.split(m[, mv])
mv=cv.split(m[, mv])

Divides a multi-channel array into several single-channel arrays.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters
src input multi-channel array.
mvbegin output array; the number of arrays must match src.channels(); the arrays themselves are reallocated, if needed.
Parameters
m input multi-channel array.
mv output vector of arrays; the arrays themselves are reallocated, if needed.

merge()

dst=cv.merge(mv[, dst])
dst=cv.merge(mv[, dst])

Creates one multi-channel array out of several single-channel ones.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters
mv input array of matrices to be merged; all the matrices in mv must have the same size and the same depth.
count number of input matrices when mv is a plain C array; it must be greater than zero.
dst output array of the same size and the same depth as mv[0]; The number of channels will be equal to the parameter count.
Parameters
mv input vector of matrices to be merged; all the matrices in mv must have the same size and the same depth.
dst output array of the same size and the same depth as mv[0]; The number of channels will be the total number of channels in the matrix array.

Steps:

  • Load the Original image using cv2.imread()
  • Then we need to split B, G, R channels of the image using cv2.split()
  • After that we will merge the image agian in R,G,B format using cv2.merge()
  • Make matplotlib subplot windows
  • Display the images using plt.show()

Example Code:

import cv2
import matplotlib.pyplot as plt
img=cv2.imread('hanif.jpg')

b,g,r=cv2.split(img)
img_matplotlib=cv2.merge([r,g,b])

plt.subplot(121)
plt.title("BGR")
plt.imshow(img)
plt.subplot(122)
plt.title("RGB")
plt.imshow(img_matplotlib)
plt.show()

Output:

5cd8b937031ca How to Display a Matplotlib RGB Image using OpenCV in Python
life2coding_icon [] How to Display a Matplotlib RGB Image using OpenCV in Python

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.