1. Data reading - image
cv. The imread() function reads the image
1.1 several ways to read images:
cv.IMREAD_COLOR: loading color images
cv.IMREAD_GRAYSCALE: load grayscale image
cv.IMREAD_UNCHANGED: loads images, including alpha channels
import numpy as np import cv2 as cv #Load color grayscale image img = cv.imread('C:\Users\Dell\Pictures\Camera Roll\crown.png',0)
The operation results are as follows:
2. Display image
cv.imshow() window automatically fits image size
cv.imshow('C:\Users\Dell\Pictures\Camera Roll\crown.png',img) cv.waitKey(0) cv.destroyAllWindows()
The operation results are as follows:
Note: where, CV Waitkey() is a keyboard specified function that waits for the specified milliseconds for any keyboard event. If you press any key during this time, the program will continue to run. If * * 0 * * is passed, it will wait indefinitely for a keystroke. It can also be set to detect specific keys, CV Destroyallwindows() will only destroy all the windows we created. If you want to destroy any particular window, use the function cv Destroywindow() passes the exact window name as a parameter.
3. Write image
cv.imwrite file name, image to be saved), for example: CV imwrite('messigray.png',img)
import numpy as np import cv2 as cv #Read image img = cv.imread('messi5.jpg',0) #Display image cv.imshow('image',img) #wait for k = cv.waitKey(0) if k == 27: # Wait for ESC to exit cv.destroyAllWindows() elif k == ord('s'): # Wait for keywords, save and exit cv.imwrite('messigray.png',img) cv.destroyAllWindows()
4. Read video from camera
Capture video: create VideoCapture object (parameters: device index / video file)
import numpy as np import cv2 as cv cap = cv.VideoCapture(0) if not cap.isOpened(): print("Cannot open camera") exit() while True: # Frame by frame capture ret, frame = cap.read() # ret is True if the frame is read correctly if not ret: print("Can't receive frame (stream end?). Exiting ...") break # That's all for our operations on the framework gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) # Display result frame e cv.imshow('frame', gray) if cv.waitKey(1) == ord('q'): break # Release the catcher after all operations are completed cap.release() cv.destroyAllWindows()
cap.read() returns a Boolean value (True / False). If the frame is read correctly, it will be True. Therefore, you can check the end of the video by checking this return value.
Sometimes, the cap may not have initialized the capture. In this case, this code displays an error. You can use * * cap The isepened * * () method checks whether it is initialized. If True, then OK. Otherwise, use * * cap Open * * () open it.
You can also use cap The get (propId) method accesses some functions of the video, where propId is a number between 0 and 18. Each number represents the properties of the video (if applicable to the video), and the complete details can be displayed here: cv::VideoCapture::get(). Some of these values can use cap Set (propId, value). Value is the new value you want.
For example, I can use cap Get (CV. Cap_prop_frame_width) and cap Get (CV. Cap_prop_frame_height) check the width and height of the frame. By default, its resolution is 640x480. But I want to change it to 320x240. Just use and. ret = cap.set(cv.CAP_PROP_FRAME_WIDTH,320) and ret = cap.set(cv.CAP_PROP_FRAME_HEIGHT,240).