ZED ユーザーガイド (3) 画像キャプチャ

 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

キャプチャが完了すると、画像 (Camera.retrieve_image()) やタイムスタンプ (Camera.get_timestamp()) など、ZED SDK によって提供されるすべてのデータを取得できます。

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 の作成ではメモリが割り当てられないため、最初の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 

画像のタイムスタンプはナノ秒単位です。フレームがドロップされない場合、2 つの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