Python's OpenCV (v)---Grab camera video images

Source: Internet
Author: User
Tags ord

OpenCV can open the camera via the head videocapture () method

Camera variable = cv2. Videocapture (n) n is an integer, the built-in camera is 0, if there are other cameras are 1,2,3,4, ...

Cap = Cv2. Videocapture (0)

Whether the camera is open can be judged by the isopened () method

Camera variables. isopened ()

Returns True if Open returns false anyway

Cap.isopened ()

Boolean variable, image variable = camera variable. Read () reads the image through the read () method

Boolean variable is true, indicates read succeeded, False indicates read failed

Ret,img = Cap.read ()

Release the camera

Camera variables. Release ()

Cap.release ()

The key variable = Cv2.waitkey (n) waitkey (n) method can obtain the user input, and can get the ASCLL code value of the key.

code example:

Import cv2# Basic Drawing # import Numpy#cv2.namedwindow ("image") #创建窗口 # grab webcam video Image cap = Cv2. Videocapture (0)  #创建内置摄像头变量while (cap.isopened ()):  #isOpened ()  detects if the camera is open    ret,img = Cap.read ()  #把摄像头获取的图像信息保存之img变量    if ret = = True:       #如果摄像头读取图像成功        cv2.imshow (' Image ', img)        k = Cv2.waitkey (100)        if k = = Ord (' a ') or k = = Ord (' A '):            cv2.imwrite (' test.jpg ', img)            breakcap.release ()  # Close camera cv2.waitkey (0) Cv2.destroyallwindow ()

  

Python's OpenCV (v)---Grab camera video images

Related Article

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.