ZED使用指南(三)Image Capture

 2、捕获图像

如何打开相机,捕获图像并在终端打印其时间戳和图像大小。

捕获ZED相机的左图像,循环直至捕获50张图像。

(1)创建配置并打开相机

# Create a ZED camera object
zed = sl.Camera()

# Set configuration parameters
init_params = =sl.InitParameters()
init_params.camera_resolution = sl.RESOLUTION.HD1080 # Use HD1080 video mode
init_params.camera_fps = 30 # Set fps at 30

# Open the camera
err = zed.open(init_params)
if (err!= sl.ERROR_CODE.SUCCESS):
    exit(-1)

(2)捕获图像并获取数据

捕获图像并对其进行处理,需要调用Camera.grab()函数。该函数接受运行时参数,在本程序中保持默认值。

每次需要新图像时都需要调用这个函数。如果grab()返回SUCCESS,则表示捕获一个新图像;否则检查grab()的状态,会显示是否没有新的帧可用或者发生了错误。

# Grab an image
if (zed.grab() == sl.ERROR_CODE.SUCCESS):
    # A new image is available if grab() returns SUCCESS

抓取完成后可以获得ZED SDK提供的所有数据,如图像(Camera.retrieve_image())和时间戳(Camera.get_timestamp())。

zed.retrieve_image(image, sl.VIEW.LEFT) #Get the left image
timestamp = zed.get_current_timestamp(sl.TIME_REFERENCE.IMAGE) # Get the timestamp of the image
print("Image resolution: ", image.get_width(), "x", image.get_height(), "|| Image timestamp: ", timestamp.get_millistamps())

retrieve_image()接受sl.Mat和VIEW模式作为参数。

在开始循环前,首先需要创建Mat。

创建Mat并不会分配内存,因此第一个retrieve_image()将自动分配内存。

在捕获50张图片后停止循环,因此在捕获成功时增加计数器。

# Capture 50 frames and stop
i = 0
image = sl.Mat()
while (i < 50):
    # Grab an image
    if (zed.grab() == sl.ERROR_CODE.SUCCESS):
        # A new image is available if grab() returns SUCCESS
        zed.retrieve_image(image, sl.VIEW.LEFT)# Get the left image
        timestamp = zed.get_timewstamp(sl.TIME_REFERENCE.IMAGE)# Get the timestamp at the time the image was captured
        print("Image resolution: ", image.get_width(), "x: ", image.get_height(), "|| Image timestamp: ", timestamp.get_milliseconds())
        i = i + 1 

图像时间戳的单位是纳秒。如果没有帧丢失,两个grab()之间的时间戳应该接近帧率时间。

(3)关闭相机并退出程序

# Close the camera
zed.close()
return 0

 完整程序 

import pyzed.sl as sl


def main():
    # Create a Camera object
    zed = sl.Camera()

    # Create a InitParameters object and set configuration parameters
    init_params = sl.InitParameters()
    init_params.camera_resolution = sl.RESOLUTION.HD1080  # Use HD1080 video mode
    init_params.camera_fps = 30  # Set fps at 30

    # Open the camera
    err = zed.open(init_params)
    if err != sl.ERROR_CODE.SUCCESS:
        exit(1)

    # Capture 50 frames and stop
    i = 0
    image = sl.Mat()
    runtime_parameters = sl.RuntimeParameters()
    while i < 50:
        # Grab an image, a RuntimeParameters object must be given to grab()
        if zed.grab(runtime_parameters) == sl.ERROR_CODE.SUCCESS:
            # A new image is available if grab() returns SUCCESS
            zed.retrieve_image(image, sl.VIEW.LEFT)
            timestamp = zed.get_timestamp(sl.TIME_REFERENCE.CURRENT)  # Get the timestamp at the time the image was captured
            print("Image resolution: {0} x {1} || Image timestamp: {2}\n".format(image.get_width(), image.get_height(),
                  timestamp.get_milliseconds()))
            i = i + 1

    # Close the camera
    zed.close()

if __name__ == "__main__":
    main()

猜你喜欢

转载自blog.csdn.net/kchenlyee/article/details/130680815