我试图编写一个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,请告诉我。
谢谢!
答案 0 :(得分:0)
我在第一次评论中提出的解决方案是一个可行的答案,因此足以将其结束。我会继续看看为什么我看不到RGBA文件,但我认为这更多地与Windows处理该文件类型的方式有关,而不是Python / Open CV脚本的问题。
由于Ximea的文档缺乏,我将把那些需要触发的两个Ximea相机解决方案的人与OpenCV放在一起。
祝你好运!