Basic Operations on Images - ( Image Basics with OpenCV )
Heading h2
Syntax
cv2.imread(filename, flags)
cv2.imshow(window_name, image)
cv2.imwrite(filename, image)
cv2.waitKey(delay)
cv2.destroyAllWindows()
Example
import cv2
img = cv2.imread('cat.jpg', 1)
cv2.imshow('cat image', img)
k = cv2.waitKey(0)
if k == 27: # wait for ESC key to exit
cv2.destroyAllWindows()
elif k == ord('s'): # wait for 's' key to save and exit
cv2.imwrite('cat_gray.png', img)
cv2.destroyAllWindows()
Output
The image is displayed on a window with the given window name. Users can press 's' to save the image and exit the window or press ESC to exit without saving.
Explanation
Basic image operations are the foundational operations for manipulating images in OpenCV. The cv2.imread()
function is used to load an image from a given filename. The cv2.imshow()
function is used to display the image on a window with a given window name. After displaying the image, cv2.waitKey()
is used to wait for a keyboard event, which can be used to perform further operations on the image. If the user presses 's', cv2.imwrite()
is used to save the image to a file with the given filename. Finally, cv2.destroyAllWindows()
is used to destroy the window.
Use
Basic image operations in OpenCV are useful in a wide range of computer vision applications, such as object detection, image segmentation, and face recognition. These operations form the building blocks for more complex image processing tasks.
Important Points
cv2.imread()
is used to load an image from a given filenamecv2.imshow()
is used to display the image on a window with a given window namecv2.waitKey()
is used to wait for a keyboard event before performing further operations on the imagecv2.imwrite()
is used to save the image to a file with the given filenamecv2.destroyAllWindows()
is used to destroy the window
Summary
In conclusion, basic image operations form the foundational operations for manipulating images in OpenCV. These operations, such as loading, displaying, saving, and destroying images, are useful in a wide range of computer vision applications. Understanding basic image operations is essential for building more complex image processing tasks using OpenCV.