Read/Display/Write Image
- Reading, displaying and writing images are the basic to image processing and computer vision task.
- In OpenCV, three inbuilt functions are used to load, display and write the images. The inbuilt functions are:
- imread() - helps to load image from the disk
- imshow() - helps to display image in a window
- imwrite() - helps to write image in the directory
Read/Display an Image
For reading the image, imread() is used. The functions takes two parameters:
imread(image, flags)
- image: first argument is the name of the image/path.
- flags: is an optional flag that lets you to specify how you want to load the image. It offers three constants:
- IMREAD_COLOR: Loads the image in the BGR format (default)
- IMREAD_UNCHANGED: Loads the image as (including the alpha channel if present)
- IMREAD_GRAYSCALE : Loads the image as an grayscale format
For display the image, imshow() function is used. The function takes two parameters:
imshow(name of the window, image)
Python Code
Line 2: Import the OpenCV package
Line 4: Loads "lena.jpg" from the disk and assign to img variable
Line 6: Display the image in the window using imshow() function
Line 8: Waits for the use to press any key
Output
Writing an Image
To write the image, imwrite() function is used. The function takes two parameters:imwrite(filename, image)
- filename: the first argument is the filename, which must include filename extension
- image: Image that you want to save
Python Code
cv2.imwrite('lena.jpg',img)

No comments:
Post a Comment