Display OpenCV Image In Jupyter Notebook

In this context, we will learn about how to display “OpenCV Image In Jupyter Notebook”, how to install the “Jupyter Notebook” and how to use various libraries and methods in the code, which is required for displaying the image.

Introduction

  • Firstly, you need to have Jupyter Notebook on your device.
  • It would help if you open your command prompt(open it as administrator) and then type the command “pip install jupyter” to install the jupyter notebook.
  • Secondly, you need to create a separate folder open the folder, and click on the title bar, you can see the command prompt as cmd click on it, and there you type the command “jupyter notebook” to open the jupyter notebook.
  • You may wonder what is “Jupyter notebook” ?. It is built to train machine learning and data science algorithms, where you can type the code run it, and can get the output.
  • Next, you need to have an image in the folder you created, this is how it appears https://drive.google.com/folderview?id=1pDBxFBYQG8dL0s3P2DJT2Jtls3LZvTye
  •  To create a new notebook for you to write and run the code, open the kernel as the image shows. https://drive.google.com/file/d/1pMW53RDsIpVnKKYc7v1CjB0ZGpxoDcxS/view?usp=drivesdk

Source Code:

https://drive.google.com/file/d/1pN9uJ9UKIf1EkVuJ696DJMFxrgXf2So_/view?usp=drivesdk

Let’s focus on the above code-

import cv2
  • Here, We are simply importing the OpenCV library, which consists of functions used for displaying the image in Jupyter Notebook.
pic = cv2.imread(r"C:\Users\akula\OneDrive\Desktop\OpenCV Image\image.jpg")
  • Here, create a variable (like pic) and  cv2.imread() used for reading the image from your folder and inside the function you need to specify the path of your image.
from matplotlib import pyplot as plt
  • Matplotlib is used for visualization and we are importing pyplot from it, which is used for plotting the area of the image and also displaying the patterns and labels for the image.
pic = cv2.cvtColor(pic,cv2.COLOR_BGR2RGB)
  • Here, the method cv2.cvtColor() for converting the color space of an image from one to another.
  • At first the cv2 reads the image stores it as BGR(Blue, Green, Red), but due to importing the matplotlib it will consider the image as RGB(Red, Green, Blue) format. So, in order to display the image correctly without any changes in the colors of the image we need to use the above function.
  • Inside the function along withe the variable name , the method for changing the format of the image is also written which is cv2.COLOR_BGR2RGB.
plt.imshow(pic)
plt.title('My Image')
plt.show()
  • Here, imshow() is used to display the given data as image.
  • title() used display the image along with name.
  • show() is used for displaying the whole image.

Output:

Here comes the results of the above code-

https://drive.google.com/file/d/1pUteIIg190nM2FOqlnUBNs2NW0CdogUI/view?usp=drivesdk

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top