Hello Guys,

In this tutorial, I will discuss how to display an image using OpenCV. 

OpenCV comes with the function named imshow which is used to display images. 

imshow function takes two arguments
 
 1. Window name
 2. Image object

We can show show any number of windows . But the name of each window must be different.

# Import cv2 module
import cv2 as cv

#reading an using imread function without passing any parameter
#by default it loads image in color format (BGR format)
colored_image= cv.imread("nature.jpg") 

# loads image in gray format
gray_image= cv.imread("nature.jpg",cv.IMREAD_GRAYSCALE)

#Code to display single image
cv.imshow("Colored Image",colored_image)

#Passing zero means it will open till you press any key 
#to close the window. It takes time in milliseconds . So if 500 will be #passed then it will be opened for 500 ms and then it will be closed #automatically.

cv.waitKey(0) 

# This is used to close all opened windows

cv.destroyAllWindows() 

#code to display multiple windows. If you want to show multiple images #then you have to pass different window name for each window

cv.imshow("Colored Image",colored_image)
cv.imshow("GrayScale Image",gray_image)
cv.waitKey(0)
cv.destroyAllWindows() # This is used to close all opened windows

Also don’t forget to check my previous OpenCV tutorials. 

http://techievaibhav.in/category/programming/opencv/

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments