Example Analysis of basic Image Operation of OpenCV
This article is to share with you the content of an example analysis of the basic operation of OpenCV images. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Overview
OpenCV is a cross-platform computer vision library that supports multiple languages and has powerful functions.
Intercept image
Example:
# intercepted image img = cv2.imread ("picture.jpg") img = img [200 img 600, 400 picture.jpg 1000] cv2.imshow ("cut", img) cv2.waitKey (0) cv2.destroyAllWindows ()
Output result:
Get color channel
Cv2.split can help us get different color channels.
Format:
Cv2.split (img)
Example:
# get color channel img = cv2.imread ("picture.jpg") # read image b, g, r = cv2.split (img) # split color channel print (r.shape, g.shape, b.shape) # debug output
Output result:
(1263, 1920) (1263, 1920) (1263, 1920)
Single channel display
# single channel display img = cv2.imread ("picture.jpg") # read picture cur_img = img.copy () # Deep copy cur_img [:,:, 0] = 0 # B channel set to 0cur_img [:,:, 1] = 0 # G channel set to 0cv2.imshow ("B channel", cur_img) # picture display cv2.waitKey (0) cv2.destroyAllWindows ()
Output result:
Read video
Cv2.VideoCapture can help us read videos. Use numbers to control different devices, such as 0,1. Or specify the video file path.
Format:
Cv2.VideoCapture (video_path or device_index)
Parameters:
Video_path: video file path
Device_index: video recording device (camera) index, such as 0,1
Example:
# read video vc = cv2.VideoCapture ("video.mp4") # Loop while (True): # read a frame, ret is bool type, indicating whether to read this frame ret successfully, frame = vc.read () # convert grayscale image gray = cv2.cvtColor (frame, cv2.COLOR_BGR2GRAY) # display cv2.imshow ('frame', gray) # if you don't press Q key Then every 1 millisecond displays a frame if cv2.waitKey (1) & 0xFF = = ord ('q'): break# releases vc.release () cv2.destroyAllWindows () Thank you for reading! This is the end of the article on "sample Analysis of the basic Operation of OpenCV Images". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!