Working with image files
OpenCV provides a very simple way to load images, using imread()
:
import cv2 image = cv2.imread('test.jpg')
To show the image, you can use imshow()
, which accepts two parameters:
- The name to write on the caption of the window that will show the image
- The image to be shown
Unfortunately, its behavior is counterintuitive, as it will not show an image unless it is followed by a call to waitKey()
:
cv2.imshow("Image", image)cv2.waitKey(0)
The call to waitKey()
after imshow()
will have two effects:
- It will actually allow OpenCV to show the image provided to
imshow()
. - It will wait for the specified amount of milliseconds, or until a key is pressed if the amount of milliseconds passed is
<=0
. It will wait indefinitely.
An image can be saved on disk using the imwrite()
method, which accepts three parameters:
- The name of the file
- The image
- An optional format-dependent parameter: ...