PYTHON+OPENCV Open the camera, save the video, the implementation of the photo function

Source: Internet
Author: User
Tags ord

In this section, the main learning is through

. Videocapture ()

Call the camera to read the image data, and use the

Cap.set (propid, value)  
Cap.get (propid)

Gets or changes the video properties.
Where the value of Propid is 0-18, 19 values are not each can be modified, each value corresponding to the properties and functions as follows:

Parameters value function/meaning
Cv_cap_prop_pos_msec 0 The current position of the video file (in milliseconds) or the video capture timestamp.
Cv_cap_prop_pos_frames 1 A 0-based index will be decoded/captured next frame.
Cv_cap_prop_pos_avi_ratio 2 The relative position of the video file: 0-The beginning of the movie, the movie's 1-end.
Cv_cap_prop_frame_width 3 The width of each frame of the video.
Cv_cap_prop_frame_height 4 The height of each frame of the video.
Cv_cap_prop_fps 5 The frame rate of the video.
Cv_cap_prop_fourcc 6 A 4-character Video encoder format.
Cv_cap_prop_frame_count 7 The number of frames in the video.
Cv_cap_prop_format 8 Byretrieve () returns the format of the Mat object.
Cv_cap_prop_mode 9 Indicates the backend-specific value of the current capture mode.
Cv_cap_prop_brightness 10 The brightness of the image (for the camera only).
Cv_cap_prop_contrast 11 Image contrast (for camera only).
Cv_cap_prop_saturation 12 The saturation of the image (for camera only).
Cv_cap_prop_hue 13 The hue of the image (for camera only).
Cv_cap_prop_gain 14 The gain of the image (for camera only).
Cv_cap_prop_exposure 15 Exposure (for cameras only).
Cv_cap_prop_convert_rgb 16 A Boolean flag that indicates whether the image should be converted to RGB.
Cv_cap_prop_white_balance 17 Currently does not support
Cv_cap_prop_rectification 18 Rectification mark for stereoscopic cameras (note: Only current support DC1394 v 2.x back end)

code as follows:

#coding: Utf-8import cv2import NumPy as NP "" "Function name: Cv2. Videocapture () function: Capture real-time image data return value via camera: Parameter one: Camera code, 0 is the default camera, notebook built-in camera is generally 0 or fill in the video name directly loaded local video file "" "Cap = Cv2. Videocapture (0) #创建一个 Videocapture object "" "Function Name: Cap.set (propid, value) function: Set video parameters. Setting the video's width and height values and the camera can only be set to 1280*720 and 640*480 when using the laptop built-in camera, even if the set value is different automatically corrected, and still return true return value: Boolean parameter one: need to set the parameters of the video parameter two: set the parameter Value "" "Ca P.set (3,480) cap.set (4,320) flag = 1; #设置一个标志, used to output video information "" "Function Name: cv2.isopened () Function: Returns a Boolean value (True/false), checks if initialization succeeded, returns successfully True return value: Boolean "" "while (Cap.isopened ()): #循环读取每一帧" "" Function Name: Cap.read () function: Returns two values first returns a Boolean value if the video read is correct, true if           Error, or false, can also be used to determine whether to the end of the video and return a value for each frame of the image, the value is a three-dimensional matrix universal Receive method is: Ret,frame = Cap.read ();  So ret store Boolean value, frame storage image If you use a variable to receive two values, such as frame = Cap.read () frame is a tuple, the original use of frame to change FRAME[1] return value: R1: Boolean R2: three-dimensional Matrix "" "Ret_flag of the image, Vshow = Cap.read () #gray = Cv2.cvtcolor (vshow,cv2.col Or_bgr2gray) #cv2. Imshow ("Gray", GRAY) cv2.imshow ("Capture_test", Vshow) #窗口显示, display named Capture_test k = Cv2.waitkey (1) & 0xFF #每帧数据延时 1ms, delay cannot be 0, otherwise read        The result will be a static frame if k = = Ord (' s '): #若检测到按键 ' s ', print the string "" "Function Name: Cap.get (propid) function: Query video parameter return value: None        Parameter one: Query the video parameters, some of which can be modified with cap.set () "" "Print (Cap.get (3));    Print (Cap.get (4)); elif k = = Ord (' q '): #若检测到按键 ' Q ', Exit Breakcap.release () #释放摄像头cv2. Destroyallwindows () #删除建立的全部窗口

PYTHON+OPENCV Open the camera, save the video, the implementation of the photo function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.