Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Do you want to see images in real-time, or maybe you want to showcase your pics from the latest vacations to family and friends? If that is the case, the cv2.imshow() method is the perfect way to display your images in OpenCV.
Continuing reading the article, we will explore the OpenCV cv2.imshow() method in detail with its syntax, parameters and working examples.
What is cv2.imshow() Method?
The OpenCV cv2.imshow() Method is a very powerful and useful method which is used to display images in a window. The window automatically fits according to the image size, so we don't need to worry about resizing. Also, the cv2.imshow() method is non-blocking, which means that we can continue to run the code while the image is displayed.
The cv2.imshow() method does not return any value as it is only capable of displaying the image. It only displays the image.
Syntax and Parameters
The syntax of the OpenCV cv2.imshow() method is as follows:
cv2.imshow(window_name, image);
You can also try this code with Online Python Compiler
The parameters of the OpenCV cv2.imshow() method are:
window_name: It is a user-defined name for the window in which the image will be displayed
image: The image is a NumPy array containing the pixel values of the image, and it will be displayed in the window
Working of OpenCV cv2.imshow() Method
The cv2.imshow() method takes two parameters: the name of the window and the image to be displayed.
The Method first checks if a window with a specified name already exists; if it doesn't exist, it will create a new window of that size, and an image will be displayed.
The method then displays the image in the window until any key is pressed on the keyboard using the cv2.waitkey() method.
The window displaying the image can be easily destroyed using the destroyAllWindows() method in the OpenCV module.
The method returns immediately, so the rest of the code can continue to run while the image is displayed.
Now let's see some examples of how the OpenCV cv2.imshow() method is implemented using Python.
Example 1: Display an image
The following code will show how to use the cv2.imshow() method to display an image in a window:
The above code will read the image from the given path and display it in a window. The window will automatically fit the image and wait for the key from the user before closing the window.
Example 2: Applying the Colour Effect
The following code will show how to black and white the image using the cv2.imshow() method:
In the above code, we first take an image, read it from the given path, perform a black and white colour change operation, and then display it in a window. The window will automatically fit the image.
Example 3: Multiple Images in a Single Window
The following code will display three images in a single window:
The above code will read three images and then resize the images to the max width. It will then concatenate the images vertically because we passed axis=0 asaparameter. Finally, the code will display the concatenated image.
See the below table for exceptions with their descriptions in the cv2.imshow() method:
Exception
Description
cv2.error
exception is thrown if an image is invalid or bugs in the OpenCV library.
cv2.error: OpenCV(4.5.3) error: (-215 failed) size.width>0 && size.height>0 in function 'cv::imshow'
exception thrown if the image size is zero or invalid.
cv2.error: OpenCV(4.5.3) error: (-215 failed) src.dims <= 2 in function 'cv::imshow'
exception is thrown if the image is not a 2D image.
cv2.error: OpenCV(4.5.3) error: (-215 failed) channel == CV_MAT_CN(dtype) in function 'cv::cvtColor'
exception is thrown if the image is not in supported colour space or format.
cv2.error: OpenCV(4.5.3) error: (-215 failed) depth == CV_8U | | depth == CV_16U || depth == CV_32F in function 'cv::imshow':
exception is thrown on displaying an image with an unsupported data type.
Frequently Asked Questions
What are the parameters of the OpenCV cv2.imshow() Method?
There are only two parameters in the imshow() method, which are window and image. The window in which the image will be displayed and the image is the image to be displayed.
Name some of the exceptions thrown by the OpenCV cv2.imshow() Method.
Some of the most common exceptions that can be thrown are cv2.error, cv2.error: OpenCV(4.5.3) error: (-215 failed) size.width>0 && size.height>0, cv2.error: OpenCV(4.5.3) error: (-215 failed) src.dims <= 2 in function 'cv::imshow' in function 'cv::imshow', etc
How to fix errors thrown by the OpenCV cv2.imshow() Method?
You can check for valid parameters and ensure that the image file used is readable and valid. Also, check if the hardware of the computer is compatible with OpenCV and update the OpenCV to the latest version.
How to view an image in cv2?
To view an image using OpenCV (cv2) in Python, read the image with cv2.imread(), display it using cv2.imshow(), and wait for a key press with cv2.waitKey(). Finally, close the window with cv2.destroyAllWindows(). This sequence loads, displays, and closes the image efficiently.
Conclusion
Congratulations, you did a fantastic job!!. This article covered the OpenCV cv2.imshow() method in detail and explained its syntax, parameters and working examples. At last, some frequently asked questions have been discussed.