ZED 景深攝影機範例#3/8 – Depth Perception

前言

本文為 ZED 景深攝影機原廠範例,共有八個範例,請根據 ZED 的範例程式頁面,取得 C++ / Python / C# 等範例原始碼。說明如下:

  1. Hello ZED: 入門範例,說明如何連接 ZED 攝影機,並於終端機中顯示裝置序列編號。
  2. Image Capture: 開啟 ZED 攝影機,取得影像,並於終端機中顯示時間戳記、影像尺寸。
  3. Depth Perception: 取得畫面的深度資料與點雲,並於終端機中顯示指定點的距離。
  4. Camera Tracking: 啟用位置追蹤,可即時更新攝影機的位置與指向。
  5. Spatial Mapping: 啟用地圖繪製,可擷取環境的網格或融合點雲。
  6. 3D Object Detection: 偵測畫面中的物體,並進行 3D 定位 (只適用於 ZED 2 機型)。
  7. Using Sensors: 取得攝影機的 IMU、氣壓計與磁力感測器資料。
  8. Body Tracking: 追蹤人體骨架

範例03 – 深度感知

本範例將說明如何從立體影像與點雲取得深度資訊,並可在終端機中顯示指定像素的距離。程式會擷取 50 張畫面之後結束。在此假設您已完成前兩個範例:Hello ZEDImage Capture

註:原廠頁面每段都提供了 C++ / Python / C# 的範例程式,在此只列出 Python 範例

前置作業

範例總覽

開啟攝影機

如先前範例,首先要建立、設定與開啟 ZED 攝影機。在此把攝影機設定為 HD720 / 60fps 並在 PERFORMANCE 模式中啟用深度偵測,更多資訊請參考 Depth Modes

open the camera - python
# Create a ZED camera
zed = sl.Camera()
init_params = sl.InitParameters()
init_params.sdk_verbose = True # Enable verbose logging
init_params.depth_mode = sl.DEPTH_MODE.PERFORMANCE # Set the depth mode to performance (fastest)
init_params.coordinate_units = sl.UNIT.MILLIMETER # Use millimeter units

# Open the camera
err = zed.open(init_params)
if err != sl.ERROR_CODE.SUCCESS:
print("Error {}, exit program".format(err)) # Display the error
exit()
注意:深度感知預設是在 ULTRA 模式。如果您是使用這個模式的話,應該不必在 InitParameters 中設定深度模式。

擷取影像與深度

ZED 開啟之後就可以擷取影像與深度了,在此程式會持續執行,直到成功擷取 50 張影像(畫面)為止。取得深度地圖的作法與取得影像相當類似,後者作法請回顧 範例2

  • 建立 sl.Mat() 來儲存深度地圖
  • 呼叫 retrieveMeasure() 來取得深度地圖
capture image and depth - python
# Capture 50 images and depth, then stop
i = 0
image = sl.Mat()
depth = sl.Mat()
point_cloud = sl.Mat()
runtime_parameters = sl.RuntimeParameters()
while i < 50:
# Grab an image
if zed.grab(runtime_parameters) == sl.ERROR_CODE.SUCCESS:
# A new image is available if grab() returns sl.ERROR_CODE.SUCCESS
zed.retrieve_image(image, sl.VIEW.LEFT) # Get the left image
zed.retrieve_measure(depth, sl.MEASURE.DEPTH) # Retrieve depth matrix. Depth is aligned on the left RGB image
zed.retrieve_measure(point_cloud, sl.MEASURE.XYZRGBA) # Retrieve colored point cloud
i = i + 1

更多關於深度與點雲參數請參考 Using the Depth API

在點雲中測量距離

取得點雲之後,就可取得特定像素的深度(距離)了。本範例會取得畫面中心(寬/2, 高/2)像素的距離資訊。

measure distance in point cloud - python
# Get and print distance value in mm at the center of the image
# We measure the distance camera - object using Euclidean distance
x = round(image.get_width() / 2)
y = round(image.get_height() / 2)
err, point_cloud_value = point_cloud.get_value(x, y)
distance = math.sqrt(point_cloud_value[0] * point_cloud_value[0] +
point_cloud_value[1] * point_cloud_value[1] +
point_cloud_value[2] * point_cloud_value[2])
print("Distance to Camera at ({0}, {1}): {2} mm".format(x, y, distance), end="\r")

不太習慣點雲的話,也可直接使用深度地圖來取得指定像素的深度值。

use the depth map - python
x = round(image.get_width() / 2)
y = round(image.get_height() / 2)
err, depth_value = depth.get_value(x, y)
print("Distance to Camera at ({0}, {1}): {2} mm".format(x, y, depth_value), end="\r")
關閉攝影機

擷取50張影像之後,關閉攝影機並退出程式。

close the camera - python
# Close the camera
zed.close()

更多範例

想學習如何由相機取得/顯示即時點雲,並調整 depth confidence filters, check the Point Cloud Viewer 範例。

下一步

您現在已經知道如何從 ZED 立體攝影機取得影像與深度資料了。根據您的應用,可參考一下文章:

  • 範例07 Sensor – 取得裝置上的感測器資料,例如 IMU、氣壓與磁力感測器等。
  • 範例04 Camera Tracking – 於3D空間中追蹤攝影機的動作。
  • 範例06 Object Detection – 於3D空間中偵測與追蹤人體。

註:本文經授權之後翻譯自:https://www.stereolabs.com/docs/tutorials/depth-sensing/

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *