日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

利用opencv从USB摄像头获取图片 获得摄像头编号

發布時間:2024/3/13 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 利用opencv从USB摄像头获取图片 获得摄像头编号 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

  • 安裝opencv
    安裝的步驟可以按照之前這個文章操作。如果在測試的時候:
  • cam = cv2.VideoCapture(0) print cam.isOpend()

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

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

  • 設置攝像頭參數
    設置可以在腳本中用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=128contrast (int) : min=0 max=255 step=1 default=57343 value=128saturation (int) : min=0 max=255 step=1 default=57343 value=128white_balance_temperature_auto (bool) : default=1 value=1gain (int) : min=0 max=255 step=1 default=57343 value=0power_line_frequency (menu) : min=0 max=2 default=2 value=2white_balance_temperature (int) : min=2000 max=6500 step=1 default=57343 value=4000 flags=inactivesharpness (int) : min=0 max=255 step=1 default=57343 value=128backlight_compensation (int) : min=0 max=1 step=1 default=57343 value=0exposure_auto (menu) : min=0 max=3 default=0 value=3exposure_absolute (int) : min=3 max=2047 step=1 default=250 value=250 flags=inactiveexposure_auto_priority (bool) : default=0 value=1pan_absolute (int) : min=-36000 max=36000 step=3600 default=0 value=0tilt_absolute (int) : min=-36000 max=36000 step=3600 default=0 value=0focus_absolute (int) : min=0 max=250 step=5 default=8189 value=0 flags=inactivefocus_auto (bool) : default=1 value=1zoom_absolute (int) : min=100 max=500 step=1 default=57343 value=100

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

    v4l2-ctl --set-ctrl=zoom_absolute=200 #放大兩倍
    4. opencv獲取圖片
    這個就很簡單了,這里就說明下用waitKey參數來用鍵盤輸入控制視頻流:

    import cv2cam = cv2.VideoCapture(0) img_counter = 0 while cam.isOpened():ret, frame = cam.read()cv2.imshow("test", frame)if not ret:breakkey = cv2.waitKey(1) & 0xFFif key == 27: # press ESC to escape (ESC ASCII value: 27)print("Escape hit, closing...")breakelif 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 += 1else:pass cam.release() cv2.destroyAllWindows()

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

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

    總結

    以上是生活随笔為你收集整理的利用opencv从USB摄像头获取图片 获得摄像头编号的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。