Updated April 7, 2023
Introduction to OpenCV namedWindow
While working with problems related to computer vision, we may have to display images or videos after processing the images or videos, so a window is necessary to display the images or videos. In order to create the window to display the images or videos, we make use of a function called namedWindow() function in OpenCV using which a window with appropriate name and size can be created to display the images or videos and this window is made visible for a certain period of time using waitKey() function and this window can be destroyed using destroywindow() function in OpenCV.
The syntax to define namedWindow() function in OpenCV is as follows:
namedWindow(window_name, flag)
where window_name represents the name of the window that displays the image or video,
The flag represents if the window size is manually adjustable or automatically set.
Working of namedWindow() function in OpenCV
- The images and videos must be displayed after processing of images and videos in OpenCV.
- A window is necessary to display the processed images or videos.
- We have a function called namedWindow() function to create a window to display the images or videos.
- The namedWindow() function takes the image to be displayed in the window as the input along with the flag to specify the window size.
- There are two flags that can be passed to the namedWindow() function namely WINDOW_NORMAL and WINDOW_AUTOSIZE.
- Specifying the flag as WINDOW_NORMAL allows us to adjust the size of the window manually.
- Specifying the flag as WINDOW_AUTOSIZE automatically sets the size of the window.
- The namedWindow() function displays the image or video in the window having a specified name.
Examples of OpenCV namedWindow
Here are the follwoing examples mention below
Example #1
OpenCV program in python to demonstrate namedWindow() function to read the input image and then display the image in a window by using namedWindow() function as the output on the screen:
#importing all the required modules
import cv2 as cv
#reading the image that is to be displayed in a window using imread() function
imageread = cv.imread('C:/Users/admin/Desktop/images/educba.jpg')
#using namedWindow() function to display the image in a window as the output on the screen
cv.namedWindow("Display_image", cv.WINDOW_AUTOSIZE)
cv.imshow('Display_image', imageread)
cv.waitKey(0)
cv.destroyAllWindows()
The output of the given program is shown in the snapshot below:
In the above program, we are importing the required modules. Then we are reading the image that is to be displayed in a window using the imread() function. Then we are using the namedWindow() function to display the image as the output on the screen.
Example #2
OpenCV program in python to demonstrate namedWindow() function to read the input image and then display the image in a window by using namedWindow() function as the output on the screen:
#importing all the required modules
import cv2 as cv
#reading the image that is to be displayed in a window using imread() function
imageread = cv.imread('C:/Users/admin/Desktop/images/plane.jpg')
#using namedWindow() function to display the image in a window as the output on the screen
cv.namedWindow("Display_image", cv.WINDOW_AUTOSIZE)
cv.imshow('Display_image', imageread)
cv.waitKey(0)
cv.destroyAllWindows()
The output of the given program is shown in the snapshot below:
In the above program, we are importing the required modules. Then we are reading the image that is to be displayed in a window using the imread() function. Then we are using the namedWindow() function to display the image as the output on the screen.
Example #3
OpenCV program in python to demonstrate namedWindow() function to read the input image and then display the image in a window by using namedWindow() function as the output on the screen:
#importing all the required modules
import cv2 as cv
#reading the image that is to be displayed in a window using imread() function
imageread = cv.imread('C:/Users/admin/Desktop/images/car.jpg')
#using namedWindow() function to display the image in a window as the output on the screen
cv.namedWindow("Display_image", cv.WINDOW_AUTOSIZE)
cv.imshow('Display_image', imageread)
cv.waitKey(0)
cv.destroyAllWindows()
The output of the given program is shown in the snapshot below:
In the above program, we are importing the required modules. Then we are reading the image that is to be displayed in a window using the imread() function. Then we are using the namedWindow() function to display the image as the output on the screen.
Example #4
OpenCV program in python to demonstrate namedWindow() function to read the input image and then display the image in a window by using namedWindow() function as the output on the screen:
#importing all the required modules
import cv2 as cv
#reading the image that is to be displayed in a window using imread() function
imageread = cv.imread('C:/Users/admin/Desktop/images/tree.jpg')
#using namedWindow() function to display the image in a window as the output on the screen
cv.namedWindow("Display_image", cv.WINDOW_AUTOSIZE)
cv.imshow('Display_image', imageread)
cv.waitKey(0)
cv.destroyAllWindows()
The output of the given program is shown in the snapshot below:
In the above program, we are importing the required modules. Then we are reading the image that is to be displayed in a window using the imread() function. Then we are using the namedWindow() function to display the image as the output on the screen.
Example #5
OpenCV program in python to demonstrate namedWindow() function to read the input image and then display the image in a window by using namedWindow() function as the output on the screen:
#importing all the required modules
import cv2 as cv
#reading the image that is to be displayed in a window using imread() function
imageread = cv.imread('C:/Users/admin/Desktop/images/logo.png')
#using namedWindow() function to display the image in a window as the output on the screen
cv.namedWindow("Display_image", cv.WINDOW_AUTOSIZE)
cv.imshow('Display_image', imageread)
cv.waitKey(0)
cv.destroyAllWindows()
The output of the given program is shown in the snapshot below:
In the above program, we are importing the required modules. Then we are reading the image that is to be displayed in a window using the imread() function. Then we are using the namedWindow() function to display the image as the output on the screen.
Recommended Articles
This is a guide to OpenCV namedWindow. Here we discuss the concept of creating a window to display the images using the namedWindow() function with corresponding programming examples and their outputs to demonstrate them. You may also look at the following article to learn more –