Welcome! In this post I am going to describ how to read image in OpenCV, in last post I have described how to install the open source computer vision library - OpenCV in windows, do read it if your are new to openCV.
To read a image in OpenCV, write a python script as,
# importing OpenCV library
import cv2
# reading a image in path
img = cv2.imread("path-to/image.png")
# showing the image
cv2.imshow("Image", img)
# waiting for key event continuously
cv2.waitKey(0)
# destroying all generated windows
cv2.destroyAllWindows()
The import cv2 code imports the OpenCV library in the program, so that you can use its function.
In next instruction, we are reading an image using cv2.imread() function and storing it in img variable.
In next instruction, we are showing the readed image using cv2.imshow() function, in which the first parameter is the name of the window in which the image will be shown and second parameter is variable name in which the image is stored.
The cv2.waitKey() is a function in OpenCV to listen the keyboard events and the parameter to passed is the time of interval after which it would be checked. Here we have passed 0 as parameter, so it will be waiting indefinetly.
As soon as the keyboard event happens the cv2.destroyAllWindows() will get executed and it will destroy all the windows generated by the script.
So in this post, we have learned how to read a image in OpenCV, also learned about the cv2.imread(), cv2.imshow(), cv2.waitKey() and cv2.destroyAllWindows() functions.