首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用触发器的Ximea Python脚本导致空白图像

使用触发器的Ximea Python脚本导致空白图像
EN

Stack Overflow用户
提问于 2017-05-11 00:33:16
回答 1查看 888关注 0票数 2

我正在尝试写一个Python (2.7.12)和OpenCV (3.2.0)脚本,它将打开一个左右两侧的相机Ximea xiQ相机,设置一些参数,开始采集,每秒从每个相机拍摄一张照片,将它们写入文件,然后重复直到KeyboardInterrupt。一切似乎都正常,但当我查看.png图像时,它们是空白的(或者在预览中显示为黑色,但它们都大约为2.5MB,所以不是空的)。

我刚接触OpenCV和这些相机/应用编程接口,对Python语言还不是很流利(但我可以应付)。你们能不能看一下我的脚本,让我知道你们认为问题可能是什么?下面是我正在尝试的代码(这只是所包含的Python示例的一个更精细的版本:

代码语言:javascript
复制
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!')

在此之前,我一直在使用修改较少的预制示例版本……

代码语言:javascript
复制
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.')

...and认为其中一种工作正常,即从每个摄像头拍摄一张图像,并且所有图像都正确显示。所以,我认为这与我触发设置的方式有关。仅供参考,我使用来自GPS接收器的1pps信号作为GPI引脚的触发器输入。

无论如何,如果您对这个问题或重构脚本有任何建议,请让我知道,以便更好地使用OpenCV应用程序接口。

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2017-05-16 23:33:07

我在上面的第一条评论中提出的解决方案是一个可行的答案,因此足以结束这一点。我将继续研究为什么我看不到RGBA文件,但我认为这更多地与Windows处理该文件类型的方式有关,而不是Python/Open CV脚本的问题。

我将把这篇文章留给那些需要一个触发的,两个Ximea摄像头的Python和OpenCV解决方案的人,因为Ximea的文档是缺乏的。

祝好运!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43898171

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档