一个摄像头插入后会出现两个video且都有同样设备名,比如video0,video1。只有video0有数据。另一个video1用opencv是读不到数据的。(而且往往是编号较小的那个video*是可以读的)
两个usb相机同时打开可能会出现问题。通过一个usb口进入主板的两个相机会出现第一个相机占用端口的问题,导致第二个相机无法打开。必须要插两个独立的usb口。
OpenCV 3.4.10不支持更改exposure曝光参数,偶然间我使用自己台式机上opencv-python 4.2.0的cv2支持更改参数。知道是OpenCV版本问题,4.2.0修复了bug。
如果需要ros话题,可以直接用OpenCV 4.2.0cv2转发成rostopic
#!/usr/bin/env python
# coding: utf-8
import cv2
print(cv2.__version__)
cap_ir_mono = cv2.VideoCapture('/dev/video_ir_mono')
# cap_color_mono = cv2.VideoCapture('/dev/video0')
# cap_color_mono = cv2.VideoCapture(0)
## 设置曝光参数
cap_ir_mono.set(cv2.CAP_PROP_AUTO_EXPOSURE, 1) # manual mode
cap_ir_mono.set(cv2.CAP_PROP_EXPOSURE, 10) # value form 1 ~ 30 is ok
while cap_ir_mono.isOpened():
ret_ir, frame_ir = cap_ir_mono.read()
print('ir:', ret_ir)
if ret_ir:
print('ir:', len(frame_ir))
cv2.imshow("cap_ir", frame_ir)
cv2.waitKey(1)
'''
下面是多线程打开多个相机,经常找设备失败。不好用
'''
# import cv2
# import threading
# import time
#
# class camThread(threading.Thread):
# def __init__(self, previewName, camID):
# threading.Thread.__init__(self)
# self.previewName = previewName
# self.camID = camID
# def run(self):
# print("Starting " + self.previewName)
# print("self.camID " + self.camID)
# self.camPreview(self.camID)
#
# def camPreview(self, type):
# if type == 'color':
# print('type color: ', type)
# cam = cv2.VideoCapture(5)
# while cam.isOpened():
# rval, frame = cam.read()
# print('rval: ', rval)
# if rval:
# cv2.imshow(self.previewName, frame)
# key = cv2.waitKey(1)
# if key == 27: # exit on ESC
# break
# cv2.destroyWindow(self.previewName)
#
# elif type == 'ir':
# print('type ir: ', type)
# cam = cv2.VideoCapture(2)
# while cam.isOpened():
# rval, frame = cam.read()
# print('rval: ', rval)
# if rval:
# cv2.imshow(self.previewName, frame)
# key = cv2.waitKey(1)
# if key == 27: # exit on ESC
# break
# cv2.destroyWindow(self.previewName)
#
# if __name__ == "__main__":
# # Create threads as follows
# thread1 = camThread("color_mono", 'color')
# thread2 = camThread("ir_mono", 'ir')
#
# thread1.start()
# time.sleep(3)
# thread2.start()
# print("Active threads", threading.activeCount())