利用opencv從USB攝像頭獲取圖片


由於opencv自帶的VideoCapture函數直接從usb攝像頭獲取視頻數據,所以用這個來作為實時的圖像來源用於實體檢測識別是很方便的。

1. 安裝opencv

安裝的步驟可以按照之前這個文章操作。如果在測試的時候:

cam = cv2.VideoCapture(0)
print cam.isOpend()

返回了False,很有可能是在安裝的時候cmake的配置沒有設置后,可以make uninstall之后重新cmake。

2. 安裝usb攝像頭驅動(這個一般都不需要)

如果系統沒有預裝usb攝像頭的驅動,那么根據所用的攝像頭安裝相應的驅動即可。安裝完之后可以用lsusb或者v4l2-ctl --list-device命令查看當前鏈接的usb設備來確認。這里我們使用的攝像頭是羅技c930e。

3. 設置攝像頭參數

設置可以在腳本中用opencv或者在命令行用v4l2-ctl命令設置:
1). opencv

"""
0. CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds.
1. CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
3. CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file
4. CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
5. CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
6. CV_CAP_PROP_FPS Frame rate.
7. CV_CAP_PROP_FOURCC 4-character code of codec.
8. CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
9. CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
10. CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
11. CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
12. CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
13. CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
14. CV_CAP_PROP_HUE Hue of the image (only for cameras).
15. CV_CAP_PROP_GAIN Gain of the image (only for cameras).
16. CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
17. CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
18. CV_CAP_PROP_WHITE_BALANCE Currently unsupported
19. CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)
"""

# set camera properties
cam.set(4, 1280)     # img width,第一個數字對應上述屬性
cam.set(5, 640)     # img height
cam.set(6, 24)      # video FPS

2). v4l2-ctl
首先用v4l2-ctl --list-device確定usb攝像頭的device編號(一般為/dev/video0),然后查看該設備可以設置的參數:

v4l2-ctl -d /dev/video0 --list-ctrls

羅技c930e攝像頭的參數如下:

                     brightness (int)    : min=0 max=255 step=1 default=-8193 value=128
                       contrast (int)    : min=0 max=255 step=1 default=57343 value=128
                     saturation (int)    : min=0 max=255 step=1 default=57343 value=128
 white_balance_temperature_auto (bool)   : default=1 value=1
                           gain (int)    : min=0 max=255 step=1 default=57343 value=0
           power_line_frequency (menu)   : min=0 max=2 default=2 value=2
      white_balance_temperature (int)    : min=2000 max=6500 step=1 default=57343 value=4000 flags=inactive
                      sharpness (int)    : min=0 max=255 step=1 default=57343 value=128
         backlight_compensation (int)    : min=0 max=1 step=1 default=57343 value=0
                  exposure_auto (menu)   : min=0 max=3 default=0 value=3
              exposure_absolute (int)    : min=3 max=2047 step=1 default=250 value=250 flags=inactive
         exposure_auto_priority (bool)   : default=0 value=1
                   pan_absolute (int)    : min=-36000 max=36000 step=3600 default=0 value=0
                  tilt_absolute (int)    : min=-36000 max=36000 step=3600 default=0 value=0
                 focus_absolute (int)    : min=0 max=250 step=5 default=8189 value=0 flags=inactive
                     focus_auto (bool)   : default=1 value=1
                  zoom_absolute (int)    : min=100 max=500 step=1 default=57343 value=100

最后可以可以設置參數了:

v4l2-ctl --set-ctrl=zoom_absolute=200    #放大兩倍

4. opencv獲取圖片

這個就很簡單了,這里就說明下用waitKey參數來用鍵盤輸入控制視頻流:

import cv2

cam = cv2.VideoCapture(0)
img_counter = 0
while cam.isOpened():
    ret, frame = cam.read()
    cv2.imshow("test", frame)
    if not ret:
        break
    key = cv2.waitKey(1) & 0xFF

    if key == 27: 
        # press ESC to escape (ESC ASCII value: 27)
        print("Escape hit, closing...")
        break
    elif key == 32:
        # press Space to capture image (Space ASCII value: 32)
        img_name = "opencv_frame_{}.png".format(img_counter)
        cv2.imwrite(img_name, frame)
        print("{} written!".format(img_name))
        img_counter += 1
    else:
        pass
cam.release()
cv2.destroyAllWindows()

PS:waitKey(1) & 0xFF獲取當前按的鍵的ascii碼,如果要用其他鍵來控制,用相應鍵的ascii碼替換即可。(ascii碼查詢)。

參考

  1. http://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture
  2. http://stackoverflow.com/questions/11420748/setting-camera-parameters-in-opencv-python
  3. http://kurokesu.com/main/2016/01/16/manual-usb-camera-settings-in-linux/
  4. http://stackoverflow.com/questions/34588464/python-how-to-capture-image-from-webcam-on-click-using-opencv
  5. http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000520.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM