How to Read and Display Images Using OpenCV-Python Part-2
In the previous post we saw how to read an image in a computer with the help of the OpenCV library in Python. We also tried printing the variable in which we stored the image and visualized how actually the colored and grayscale images are stored as 3D and 2D matrices in a computer respectively.
Moving further, in this post we will see how to display an image that we have read using the imread() method of the OpenCV module.
For displaying the image we will have to make use of the imshow() method provided by the OpenCV module. The cv2.imshow() method creates a window in which the image is displayed. It accepts two parameters -
- Name of the window in which we want to display the image.
- The variable in which the image to be displayed is stored as a matrix.
import cv2
img=cv2.imread("c:/Users/HP/Desktop/harshad.jpg",1)
cv2.imshow("Harshad",img)
Next, we will have to make use of the waitKey() method which specifies the amount of time in milliseconds for which the image window should appear on the screen of the user. If we provide 0 as an argument to the waitKey() method then, it will hold the image window on to the screen until the user presses any key on the keyboard.
import cv2
img=cv2.imread("c:/Users/HP/Desktop/harshad.jpg",1)
cv2.imshow("Harshad",img)
cv2.waitKey(0)
#cv2.waitKey(5000) this statement will hold the image window for 5 seconds
Next, in order to destroy the image window either automatically or on pressing a key (based on the parameter provided in the waitKey() method) we will have to make a call to the destroyAllWindows() method, this method whenever invoked will destroy the window displaying the image.
import cv2
img=cv2.imread("c:/Users/HP/Desktop/harshad.jpg",1)
cv2.imshow("Harshad",img)
cv2.waitKey(0)
#cv2.waitKey(5000) this statement will hold the image window for 5 seconds
cv2.destroyAllWindows()
Comments
Post a Comment