Table of contents
1.
Introduction
2.
What is cv2.imshow() Method?
3.
Syntax and Parameters
4.
Working of OpenCV cv2.imshow() Method 
5.
Example 1: Display an image 
6.
Example 2: Applying the Colour Effect
7.
Example 3: Multiple Images in a Single Window
8.
Exceptions in cv2.imshow()
9.
Frequently Asked Questions
9.1.
What are the parameters of the OpenCV cv2.imshow() Method?
9.2.
Name some of the exceptions thrown by the OpenCV cv2.imshow() Method. 
9.3.
How to fix errors thrown by the OpenCV cv2.imshow() Method?
9.4.
How to view an image in cv2?
10.
Conclusion
Last Updated: Jun 12, 2024
Easy

OpenCV cv2.imshow() Method

Author Dhruv Rawat
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

OpenCV cv2.imshow() Method


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
Run Code

 

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 

  1. The cv2.imshow() method takes two parameters: the name of the window and the image to be displayed.
     
  2. 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.
     
  3. The method then displays the image in the window until any key is pressed on the keyboard using the cv2.waitkey() method.
     
  4. The window displaying the image can be easily destroyed using the destroyAllWindows() method in the OpenCV module.
     
  5. 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:

import cv2

img = cv2.imread('type5.png')
cv2.imshow('image',img)

cv2.waitKey(0)
cv2.destroyAllWindows()
You can also try this code with Online Python Compiler
Run Code

 

Output:

display an image


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:

import cv2
import numpy as np

image_path = "type5.png"
image = cv2.imread(image_path)

CN_image = np.copy(image)
gray = cv2.cvtColor(CN_image, cv2.COLOR_RGB2GRAY)
cv2.imshow('BWimage', gray)

cv2.waitKey(0)
cv2.destroyAllWindows()
You can also try this code with Online Python Compiler
Run Code

 

Image Before:

image before applying the colour effect

 

Image After:

image after colour effect

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: 

import cv2
import numpy as np

image1 = cv2.imread('type5.png')
image2 = cv2.imread('menu4.png')
image3 = cv2.imread('menu6.jpeg')

max_width = max(image1.shape[1], image2.shape[1], image3.shape[1])
image1 = cv2.resize(image1, (max_width, image1.shape[0]))
image2 = cv2.resize(image2, (max_width, image2.shape[0]))
image3 = cv2.resize(image3, (max_width, image3.shape[0]))

concatenated_image = np.concatenate((image1, image2, image3), axis=0)

cv2.imshow('Concatenated Images', concatenated_image)

cv2.waitKey(0)
cv2.destroyAllWindows()
You can also try this code with Online Python Compiler
Run Code

 

Output:

multiple images in 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 as a parameter. Finally, the code will display the concatenated image.

Also Read - Image Sampling

Exceptions in cv2.imshow()

See the below table for exceptions with their descriptions in the cv2.imshow() method:

Exception 

Description

cv2.errorexception 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.

Here are some more related articles:


Check out The Interview Guide for Product Based Companies and some famous Interview Problems from Top Companies, like AmazonAdobeGoogle, etc., on CodeStudio.

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test SeriesInterview Bundles, and some Interview Experiences curated by top Industry Experts only on CodeStudio.

We hope you liked this article.

"Have fun coding!”

Live masterclass