我正在尝试编写一个 Python (2.7.12) 和 OpenCV (3.2.0) 脚本,它将打开左右侧摄像头 Ximea xiQ 摄像头,设置一些参数,开始采集,每秒从每个摄像头拍摄一张照片,将它们写入文件,然后重复直到 KeyboardInterrupt。一切似乎都在工作,但是当我查看 .png 图像时,它们是空白的(或者在预览中显示为黑色,但它们都是 ~2.5 MB,所以不是空的)。
我是 OpenCV 和这些相机/API 的新手,还不能流利地使用 Python(但我可以绕过)。你们都看看我的脚本,让我知道你认为问题可能是什么吗?这是我正在尝试的代码(这只是包含的 Python 示例的更详细版本:
from ximea import xiapi
import cv2
increment_val = 0
##which_camera = 'Left\\'
save_location = 'C:\\Users\\mah_usernames\\Desktop\\camera_test\\'
image_file_name = 'image'
image_file_type = '.png'
##saved_image_full_path = save_location + which_camera + image_file_name + image_file_type
saved_image_full_path = save_location + image_file_name + image_file_type
#create instance for first connected camera
left_cam = xiapi.Camera(dev_id=0)
right_cam = xiapi.Camera(dev_id=1)
#start communication
#to open specific device, use:
#cam.open_device_by_SN('41305651')
#(open by serial number)
print('Opening left side camera...')
left_cam.open_device()
print('Opening right side camera...')
right_cam.open_device()
#settings
##left_cam.enable_aeag() #enable auto exposure, auto gain
##right_cam.enable_aeag()
##left_cam.set_acq_timing_mode('XI_ACQ_TIMING_MODE_FREE_RUN')
##right_cam.set_acq_timing_mode('XI_ACQ_TIMING_MODE_FREE_RUN')
left_cam.set_led_selector('XI_LED_SEL3') #choose bottom LED
left_cam.set_led_mode('XI_LED_ON') #always on, power indicator
left_cam.set_led_selector('XI_LED_SEL2') #choose middle LED
left_cam.set_led_mode('XI_LED_FRAME_ACTIVE') #blinks when frame is active
left_cam.set_led_selector('XI_LED_SEL1') #choose top LED
left_cam.set_led_mode('XI_LED_ACQUISITION') #blinks when data is transferred
##
right_cam.set_led_selector('XI_LED_SEL3') #choose bottom LED
right_cam.set_led_mode('XI_LED_ON') #always on, power indicator
right_cam.set_led_selector('XI_LED_SEL2') #choose middle LED
right_cam.set_led_mode('XI_LED_FRAME_ACTIVE') #blinks when frame is active
right_cam.set_led_selector('XI_LED_SEL1') #choose top LED
right_cam.set_led_mode('XI_LED_ACQUISITION') #blinks when data is transferred
left_cam.set_trigger_source('XI_TRG_EDGE_RISING')
right_cam.set_trigger_source('XI_TRG_EDGE_RISING')
left_cam.set_trigger_selector('XI_TRG_SEL_EXPOSURE_ACTIVE')
right_cam.set_trigger_selector('XI_TRG_SEL_EXPOSURE_ACTIVE')
left_cam.set_gpi_mode('XI_GPI_TRIGGER')
right_cam.set_gpi_mode('XI_GPI_TRIGGER')
left_cam.set_exposure(5000)
right_cam.set_exposure(5000)
print('Left Camera exposure was set to %i us' %left_cam.get_exposure())
print('Right Camera exposure was set to %i us' %right_cam.get_exposure())
left_cam.set_imgdataformat('XI_RGB32')
right_cam.set_imgdataformat('XI_RGB32')
#create instance of Image to store image data and metadata
##img = xiapi.Image()
img_left = xiapi.Image()
img_right = xiapi.Image()
#start data acquisition
print('Starting data acquisition...')
left_cam.start_acquisition()
right_cam.start_acquisition()
try:
while True:
print str(increment_val)
#get data and pass them from camera to img
## left_cam.get_image(img_left)
## right_cam.get_image(img_right)
left_cam.get_image(img_left)
right_cam.get_image(img_right)
#get raw data from camera
#for Python2.x function returns string
#for Python3.x function returns bytes
left_cam_data = img_left.get_image_data_numpy()
right_cam_data = img_right.get_image_data_numpy()
## #transform data to list
## data = list(data_raw)
##
## #print image data and metadata
## print('Image number: ' + str(i))
## print('Image width (pixels): ' + str(img_left.width))
## print('Image height (pixels): ' + str(img_left.height))
## print('First 10 pixels: ' + str(left_cam_data[:10]))
## print('\n')
## #show acquired image
## print('Drawing image...')
## cv2.imshow('Left side camera', left_cam_data)
## cv2.imshow('Right side camera', right_cam_data)
## cv2.waitKey(0)
## cv2.destroyAllWindows()
#save acquired image
print('Saving image...')
cv2.imwrite((save_location + 'Left\\' + image_file_name + str(increment_val) + image_file_type), left_cam_data)
cv2.imwrite((save_location + 'Right\\' + image_file_name + str(increment_val) + image_file_type), right_cam_data)
increment_val += 1
print str(increment_val)
except:
KeyboardInterrupt
#stop data acquisition
print ('Stopping acquisition...')
left_cam.stop_acquisition()
right_cam.stop_acquisition()
#stop communication
left_cam.close_device()
right_cam.close_device()
print ('All finished!')
在此之前,我一直在使用罐头示例的稍微修改较少的版本......
from ximea import xiapi
import cv2
##increment_val = 0
##which_camera = 'Left\\'
save_location = 'C:\\Users\\mah_usernames\\Desktop\\camera_test\\'
image_file_name = 'image'
image_file_type = '.png'
##saved_image_full_path = save_location + which_camera + image_file_name + image_file_type
saved_image_full_path = save_location + image_file_name + image_file_type
#create instance for first connected camera
left_cam = xiapi.Camera(dev_id=0)
right_cam = xiapi.Camera(dev_id=1)
#start communication
#to open specific device, use:
#cam.open_device_by_SN('41305651')
#(open by serial number)
print('Opening left side camera...')
left_cam.open_device()
print('Opening right side camera...')
right_cam.open_device()
#settings
left_cam.set_exposure(10000)
right_cam.set_exposure(8000)
print('Left Camera exposure was set to %i us' %left_cam.get_exposure())
print('Right Camera exposure was set to %i us' %right_cam.get_exposure())
left_cam.set_imgdataformat('XI_RGB24')
right_cam.set_imgdataformat('XI_RGB24')
#create instance of Image to store image data and metadata
##img = xiapi.Image()
img_left = xiapi.Image()
img_right = xiapi.Image()
#start data acquisition
print('Starting data acquisition...')
left_cam.start_acquisition()
right_cam.start_acquisition()
for i in range(10):
increment_val = i
print str(increment_val)
#get data and pass them from camera to img
## left_cam.get_image(img_left)
## right_cam.get_image(img_right)
left_cam.get_image(img_left)
right_cam.get_image(img_right)
#get raw data from camera
#for Python2.x function returns string
#for Python3.x function returns bytes
left_cam_data = img_left.get_image_data_numpy()
right_cam_data = img_right.get_image_data_numpy()
## #transform data to list
## data = list(data_raw)
##
## #print image data and metadata
## print('Image number: ' + str(i))
## print('Image width (pixels): ' + str(img_left.width))
## print('Image height (pixels): ' + str(img_left.height))
## print('First 10 pixels: ' + str(left_cam_data[:10]))
## print('\n')
## #show acquired image
## print('Drawing image...')
## cv2.imshow('Left side camera', left_cam_data)
## cv2.imshow('Right side camera', right_cam_data)
## cv2.waitKey(0)
## cv2.destroyAllWindows()
#save acquired image
print('Saving image...')
cv2.imwrite((save_location + 'Left\\' + image_file_name + str(increment_val) + image_file_type), left_cam_data)
## which_camera = 'Right\\'
cv2.imwrite((save_location + 'Right\\' + image_file_name + str(increment_val) + image_file_type), right_cam_data)
#stop data acquisition
print('Stopping acquisition...')
left_cam.stop_acquisition()
right_cam.stop_acquisition()
#stop communication
left_cam.close_device()
right_cam.close_device()
print('Done.')
...并且那个工作正常,即从每台相机拍摄了一张图像,并且所有图像都正确显示。所以,我认为这与我必须触发设置的方式有关。仅供参考,我使用来自 GPS 接收器的 1 PPS 信号作为 GPI 引脚的触发输入。
无论如何,如果您对此问题有任何建议或重构脚本以便更好地利用 OpenCV API,请告诉我。
谢谢!