OpenCV is the best library for computer vision. You can manipulate images and videos through it. In the previous post, you know how to save or write an image file using the cv2.imread() method. In this entire tutorial, you will implement the cv2.imshow function in python. But before that let’s know the syntax of this method.
This method has two main parameters. One is the window name and the other is your image file. The window name is of string type and represents the name of the window where you are seeing the image. Obviously, the image_file is the file name of the image. In the next section, you will learn the steps to implement the cv2.imshow() method.
Steps to Implement cv2.imshow() in python
Step 1: Import all necessary libraries.
In this entire tutorial, I am using two main python modules. One is NumPy for black and white image creation. And the other is the OpenCV(cv2) module. Let’s import them.
import numpy as np import cv2
Step 2: Examples on cv2.imshow() method
Below are the examples to implement the imshow() method.
Example 1: Displaying Black Image using cv2 imshow()
In this example Firstly, I will make a black image using the NumPy array. Then use the imshow() method to display the image.
import numpy as np import cv2 black= np.zeros([200,250,1],dtype="uint8") cv2.imshow("Black Image",black) cv2.waitKey(0) cv2.destroyAllWindows()
Explanation of the code
Here I am first creating a NumPy array of type uint8(8-bit image type) with width 200, height 250, and of one channel. After that, I am passing the “Black Image” string as a window name and black NumPy array to the imshow() method to display the image.
waitKey() is useful for displaying the image in the windows as long as you don’t close the window. The method destroyAllWindows() will close all opened windows opened in OpenCV. Below is the output.
Example 2: Displaying White Image using cv2 imshow()
Now let’s display a white image using the imshow() method. Here I will create a NumPy array with each element value of 255. Execute the lines of code given below.
import numpy as np import cv2 white = np.ones([200,250,1],dtype="uint8") white = white*(2**8-1) cv2.imshow("White Image",white) cv2.waitKey(0) cv2.destroyAllWindows()
Explanation of the code
Firstly I am creating a NumPy array with each element of value 1 using np. ones() of type “unit8“. After that multiplying each element with 2**8 -1(255). It will create a white image. Below is the output of the above code.
Example 3: Displaying RGB Image using imshow()
In this last example, I will show the image in the window that I have saved on the disk. To do so I will first read the image using the cv2.imread() method. After that pass the image inside the imshow() method. Execute the lines of code and see the output.
import cv2 img = cv2.imread("flowers.jpg") cv2.imshow("Flowers",img) cv2.waitKey(0) cv2.destroyAllWindows()
These are the examples I have aggregated for you on the cv2 imshow() method. You will often use this method to display the image for different purposes. Hope these examples have cleared your understanding of using this method. Even if you have any doubt then you can contact us for getting more help.
Similar CV2 Utilities :
Data Science Learner Team
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.