Updated April 18, 2023
Introduction to OpenCV imshow
When we are trying to solve the problems related to computer vision, many times it becomes necessary to display the resulting image from the program in a window, in such cases to be able to display a given image in a window, we make use of a function called imshow() function using which an image can be displayed in a window by specifying a name for the window and the duration of time can be specified using waitkey() function and if 0 is passed as a parameter to the waitkey() function, the display of the window continues till any key is pressed and the window can be destroyed using destroyAllWindows() function. In this topic, we are going to learn about OpenCV imshow.
The syntax to define imshow() function in OpenCV
imshow(window_name, image)
where window_name is the name of the window within which the given image must be displayed and
image is the image to be displayed in a window.
Working of imshow() function in OpenCV
- When we are trying to solve the problems related to computer vision, many times it becomes necessary to display the resulting image from the program in a window.
- To be able to display an image by creating a window, we make use of a function called imshow() function in OpenCV.
- The imshow() function takes two parameters namely window_name and image.
- The parameter window_name is the name of the window within which the given image must be displayed.
- The parameter image is the image to be displayed in a window.
- The window created using the imshow() function to display an image is displayed until any key is pressed on the keyboard using the waitkey() function.
- The window created using the imshow() function to display an image can be destroyed using the destroyAllWindows() function.
- The imshow() function displays the given image in a window and doesn’t return anything.
Examples of OpenCV imshow
Here we discuss the following examples mention below
Example #1
OpenCV program in python to demonstrate imshow() function to read an image using imread() function and then display the same image using imshow() function by creating a window and specifying the name for the window and display it as the output on the screen:
#importing the module cv2
import cv2
#reading the image from a given path on which is to be displayed in the window
imageread = cv2.imread('C:/Users/admin/Desktop/educba.jpg')
#creating a window by using namedWindow() function
cv2.namedWindow('imageread', cv2.WINDOW_NORMAL)
#displaying the image in the window by specifying a name for it and the display continues until any key is pressed on the keyboard
cv2.imshow('EDUCBA',imageread)
cv2.waitKey(0)
cv2.destroyAllWindows()
The output of the given program is shown in the snapshot below:
In the above program, we are importing the module cv2. Then we are reading the image on which is to be displayed in the window using the imread() function. Then we made use of the namedwindow() function, we are naming the window in which the image must be displayed. Then we are making use imshow() function to display the image in the window as the output on the screen.
Example #2
OpenCV program in python to demonstrate imshow() function to read an image using imread() function and then display the same image using imshow() function by creating a window and specifying the name for the window and display it as the output on the screen:
#importing the module cv2
import cv2
#reading the image from a given path on which is to be displayed in the window
imageread = cv2.imread('C:/Users/admin/Desktop/plane.jpg')
#creating a window by using namedWindow() function
cv2.namedWindow('imageread', cv2.WINDOW_NORMAL)
#displaying the image in the window by specifying a name for it and the display continues until any key is pressed on the keyboard
cv2.imshow('Aeroplane',imageread)
cv2.waitKey(0)
cv2.destroyAllWindows()
The output of the given program is shown in the snapshot below:
In the above program, we are importing the module cv2. Then we are reading the image on which is to be displayed in the window using the imread() function. Then we made use of the namedwindow() function, we are naming the window in which the image must be displayed. Then we are making use imshow() function to display the image in the window as the output on the screen.
Example #3
OpenCV program in python to demonstrate imshow() function to read an image using imread() function and then display the same image using imshow() function by creating a window and specifying the name for the window and display it as the output on the screen:
#importing the module cv2
import cv2
#reading the image from a given path on which is to be displayed in the window
imageread = cv2.imread('C:/Users/admin/Desktop/logo.png')
#creating a window by using namedWindow() function
cv2.namedWindow('imageread', cv2.WINDOW_NORMAL)
#displaying the image in the window by specifying a name for it and the display continues until any key is pressed on the keyboard
cv2.imshow('EDUCBA_logo',imageread)
cv2.waitKey(0)
cv2.destroyAllWindows()
The output of the given program is shown in the snapshot below:
In the above program, we are importing the module cv2. Then we are reading the image on which is to be displayed in the window using the imread() function. Then we made use of the namedwindow() function, we are naming the window in which the image must be displayed. Then we are making use imshow() function to display the image in the window as the output on the screen.
Example #4
OpenCV program in python to demonstrate imshow() function to read an image using imread() function and then display the same image using imshow() function by creating a window and specifying the name for the window and display it as the output on the screen:
#importing the module cv2
import cv2
#reading the image from a given path on which is to be displayed in the window
imageread = cv2.imread('C:/Users/admin/Desktop/tree.jpg')
#creating a window by using namedWindow() function
cv2.namedWindow('imageread', cv2.WINDOW_NORMAL)
#displaying the image in the window by specifying a name for it and the display continues until any key is pressed on the keyboard
cv2.imshow('Tree',imageread)
cv2.waitKey(0)
cv2.destroyAllWindows()
The output of the given program is shown in the snapshot below:
In the above program, we are importing the module cv2. Then we are reading the image on which is to be displayed in the window using the imread() function. Then we made use of the namedwindow() function, we are naming the window in which the image must be displayed. Then we are making use imshow() function to display the image in the window as the output on the screen.
Conclusion
In this article, we have learned the concept of imshow() function in OpenCV through definition, syntax, and working of imshow() function in OpenCV with corresponding programming examples and their outputs to demonstrate them.
Recommended Articles
We hope that this EDUCBA information on “OpenCV imshow” was beneficial to you. You can view EDUCBA’s recommended articles for more information.