工程筆記本 / 科技觀點/快訊/報導

科技觀點/快訊/報導

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

CAVEDU 阿吉 - 雜工📅 2022-02-02👁 43

前言

本文為 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。 [pastacode lang="python" manual="%23%20Create%20a%20ZED%20camera%0Azed%20%3D%20sl.Camera()%0Ainit_params%20%3D%20sl.InitParameters()%0Ainit_params.sdk_verbose%20%3D%20True%20%23%20Enable%20verbose%20logging%0Ainit_params.depth_mode%20%3D%20sl.DEPTH_MODE.PERFORMANCE%20%23%20Set%20the%20depth%20mode%20to%20performance%20(fastest)%0Ainit_params.coordinate_units%20%3D%20sl.UNIT.MILLIMETER%20%20%23%20Use%20millimeter%20units%0A%0A%23%20Open%20the%20camera%0Aerr%20%3D%20zed.open(init_params)%0Aif%20err%20!%3D%20sl.ERROR_CODE.SUCCESS%3A%0A%20%20%20%20print(%22Error%20%7B%7D%2C%20exit%20program%22.format(err))%20%23%20Display%20the%20error%0A%20%20%20%20exit()" message="open the camera - python" highlight="" provider="manual"/]
注意:深度感知預設是在 ULTRA 模式。如果您是使用這個模式的話,應該不必在 InitParameters 中設定深度模式。

擷取影像與深度

ZED 開啟之後就可以擷取影像與深度了,在此程式會持續執行,直到成功擷取 50 張影像(畫面)為止。取得深度地圖的作法與取得影像相當類似,後者作法請回顧 範例2
  • 建立 sl.Mat() 來儲存深度地圖
  • 呼叫 retrieveMeasure() 來取得深度地圖
[pastacode lang="python" manual="%23%20Capture%2050%20images%20and%20depth%2C%20then%20stop%0Ai%20%3D%200%0Aimage%20%3D%20sl.Mat()%0Adepth%20%3D%20sl.Mat()%0Apoint_cloud%20%3D%20sl.Mat()%0Aruntime_parameters%20%3D%20sl.RuntimeParameters()%0Awhile%20i%20%3C%2050%3A%0A%20%20%20%20%23%20Grab%20an%20image%0A%20%20%20%20if%20zed.grab(runtime_parameters)%20%3D%3D%20sl.ERROR_CODE.SUCCESS%3A%0A%20%20%20%20%20%20%20%20%23%20A%20new%20image%20is%20available%20if%20grab()%20returns%20sl.ERROR_CODE.SUCCESS%0A%20%20%20%20%20%20%20%20zed.retrieve_image(image%2C%20sl.VIEW.LEFT)%20%23%20Get%20the%20left%20image%0A%20%20%20%20%20%20%20%20zed.retrieve_measure(depth%2C%20sl.MEASURE.DEPTH)%20%23%20Retrieve%20depth%20matrix.%20Depth%20is%20aligned%20on%20the%20left%20RGB%20image%0A%20%20%20%20%20%20%20%20zed.retrieve_measure(point_cloud%2C%20sl.MEASURE.XYZRGBA)%20%23%20Retrieve%20colored%20point%20cloud%0A%20%20%20%20%20%20%20%20i%20%3D%20i%20%2B%201" message="capture image and depth - python" highlight="" provider="manual"/] 更多關於深度與點雲參數請參考 Using the Depth API

在點雲中測量距離

取得點雲之後,就可取得特定像素的深度(距離)了。本範例會取得畫面中心(寬/2, 高/2)像素的距離資訊。 [pastacode lang="python" manual="%23%20Get%20and%20print%20distance%20value%20in%20mm%20at%20the%20center%20of%20the%20image%0A%23%20We%20measure%20the%20distance%20camera%20-%20object%20using%20Euclidean%20distance%0Ax%20%3D%20round(image.get_width()%20%2F%202)%0Ay%20%3D%20round(image.get_height()%20%2F%202)%0Aerr%2C%20point_cloud_value%20%3D%20point_cloud.get_value(x%2C%20y)%0Adistance%20%3D%20math.sqrt(point_cloud_value%5B0%5D%20*%20point_cloud_value%5B0%5D%20%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20point_cloud_value%5B1%5D%20*%20point_cloud_value%5B1%5D%20%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20point_cloud_value%5B2%5D%20*%20point_cloud_value%5B2%5D)%0Aprint(%22Distance%20to%20Camera%20at%20(%7B0%7D%2C%20%7B1%7D)%3A%20%7B2%7D%20mm%22.format(x%2C%20y%2C%20distance)%2C%20end%3D%22%5Cr%22)" message="measure distance in point cloud - python" highlight="" provider="manual"/] 不太習慣點雲的話,也可直接使用深度地圖來取得指定像素的深度值。 [pastacode lang="python" manual="x%20%3D%20round(image.get_width()%20%2F%202)%0Ay%20%3D%20round(image.get_height()%20%2F%202)%0Aerr%2C%20depth_value%20%3D%20depth.get_value(x%2C%20y)%0Aprint(%22Distance%20to%20Camera%20at%20(%7B0%7D%2C%20%7B1%7D)%3A%20%7B2%7D%20mm%22.format(x%2C%20y%2C%20depth_value)%2C%20end%3D%22%5Cr%22)" message="use the depth map - python" highlight="" provider="manual"/]
關閉攝影機
擷取50張影像之後,關閉攝影機並退出程式。 [pastacode lang="python" manual="%23%20Close%20the%20camera%0Azed.close()" message="close the camera - python" highlight="" provider="manual"/]

更多範例

想學習如何由相機取得/顯示即時點雲,並調整 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/

相關文章 📎

留言 💬 (0)

還沒有留言,來當第一個。