前言
本文為 ZED 景深攝影機原廠範例,共有八個範例,請根據 ZED 的範例程式頁面,取得 C++ / Python / C# 等範例原始碼。說明如下:
- Hello ZED: 入門範例,說明如何連接 ZED 攝影機,並於終端機中顯示裝置序列編號。
- Image Capture: 開啟 ZED 攝影機,取得影像,並於終端機中顯示時間戳記、影像尺寸。
- Depth Perception: 取得畫面的深度資料與點雲,並於終端機中顯示指定點的距離。
- Camera Tracking: 啟用位置追蹤,可即時更新攝影機的位置與指向。
- Spatial Mapping: 啟用地圖繪製,可擷取環境的網格或融合點雲。
- 3D Object Detection: 偵測畫面中的物體,並進行 3D 定位 (只適用於 ZED 2 機型)。
- Using Sensors: 取得攝影機的 IMU、氣壓計與磁力感測器資料。
- Body Tracking: 追蹤人體骨架
範例04 – Camera Tracking
本範例說明如何取得即時的相機的位置與方位,程式會在成功擷取 1000 筆位置資訊後結束。在此假設您已完成先前的範例。
註:原廠頁面每段都提供了 C++ / Python / C# 的範例程式,在此只列出 Python 範例
前置作業
- 下載最新版的 ZED SDK (請點我)
- 下載 Image Capture 範例程式,提供 C++, Python 與 C# 等版本
- 在 Windows 或 Linux OS上,建置 C++ 環境(請點我) 或執行 Python 範例,本系列文章將使用 Python (教學請點我)
程式總覽
開啟攝影機
如先前所有範例,在此會建立、設定並開啟ZED攝影機。
[pastacode lang=”python” manual=”%23%20Create%20a%20ZED%20camera%20object%0Azed%20%3D%20sl.Camera()%0A%0A%23%20Set%20configuration%20parameters%0Ainit_params%20%3D%20sl.InitParameters()%0Ainit_params.camera_resolution%20%3D%20sl.RESOLUTION.HD720%20%20%23%20Use%20HD720%20video%20mode%20(default%20fps%3A%2060)%0A%23%20Use%20a%20right-handed%20Y-up%20coordinate%20system%0Ainit_params.coordinate_system%20%3D%20sl.COORDINATE_SYSTEM.RIGHT_HANDED_Y_UP%0Ainit_params.coordinate_units%20%3D%20sl.UNIT.METER%20%20%23%20Set%20units%20in%20meters%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%20exit(1)” message=”open the camera” highlight=”” provider=”manual”/]啟用位置追蹤
攝影機開啟之後,還需要透過 enablePositionalTracking() 來啟用位置追蹤模組,這樣才能取得 ZED 攝影機的位置與方位。
先前的範例只使用了ZED SDK預設的追蹤參數。完整參數清單請參考 Tracking API。
擷取姿勢資料
現在動作追蹤已經啟用了,在此透過一個迴圈來擷取攝影機位置。攝影機的位置是由 Pose 類別所給定,該類別已包含了攝影機的位移與方位狀態,以及影像的時間戳記與追蹤信心指數。
每個位置都會聯於某個參考坐標系,ZED SDK 提供了兩種參考坐標系:REFERENCE_FRAME::WORLD 與 REFERENCE_FRAME::CAMERA。更多資訊請參考Coordinate Frames 文件。ZED 的三軸方位示意如下:
![]()
本範例會在 World Frame 中取得攝影機位置。
[pastacode lang=”python” manual=”%23%20Track%20the%20camera%20position%20during%201000%20frames%0Ai%20%3D%200%0Azed_pose%20%3D%20sl.Pose()%0Aruntime_parameters%20%3D%20sl.RuntimeParameters()%0Awhile%20i%20%3C%201000%3A%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%20Get%20the%20pose%20of%20the%20left%20eye%20of%20the%20camera%20with%20reference%20to%20the%20world%20frame%0A%20%20%20%20%20%20%20%20zed.get_position(zed_pose%2C%20sl.REFERENCE_FRAME.WORLD)%0A%0A%20%20%20%20%20%20%20%20%23%20Display%20the%20translation%20and%20timestamp%0A%20%20%20%20%20%20%20%20py_translation%20%3D%20sl.Translation()%0A%20%20%20%20%20%20%20%20tx%20%3D%20round(zed_pose.get_translation(py_translation).get()%5B0%5D%2C%203)%0A%20%20%20%20%20%20%20%20ty%20%3D%20round(zed_pose.get_translation(py_translation).get()%5B1%5D%2C%203)%0A%20%20%20%20%20%20%20%20tz%20%3D%20round(zed_pose.get_translation(py_translation).get()%5B2%5D%2C%203)%0A%20%20%20%20%20%20%20%20print(%22Translation%3A%20Tx%3A%20%7B0%7D%2C%20Ty%3A%20%7B1%7D%2C%20Tz%20%7B2%7D%2C%20Timestamp%3A%20%7B3%7D%5Cn%22.format(tx%2C%20ty%2C%20tz%2C%20zed_pose.timestamp.get_milliseconds()))%0A%0A%20%20%20%20%20%20%20%20%23%20Display%20the%20orientation%20quaternion%0A%20%20%20%20%20%20%20%20py_orientation%20%3D%20sl.Orientation()%0A%20%20%20%20%20%20%20%20ox%20%3D%20round(zed_pose.get_orientation(py_orientation).get()%5B0%5D%2C%203)%0A%20%20%20%20%20%20%20%20oy%20%3D%20round(zed_pose.get_orientation(py_orientation).get()%5B1%5D%2C%203)%0A%20%20%20%20%20%20%20%20oz%20%3D%20round(zed_pose.get_orientation(py_orientation).get()%5B2%5D%2C%203)%0A%20%20%20%20%20%20%20%20ow%20%3D%20round(zed_pose.get_orientation(py_orientation).get()%5B3%5D%2C%203)%0A%20%20%20%20%20%20%20%20print(%22Orientation%3A%20Ox%3A%20%7B0%7D%2C%20Oy%3A%20%7B1%7D%2C%20Oz%20%7B2%7D%2C%20Ow%3A%20%7B3%7D%5Cn%22.format(ox%2C%20oy%2C%20oz%2C%20ow))” message=”retrieve camera position” highlight=”” provider=”manual”/]慣性資料
如果您使用的型號包含了IMU (例如 ZED 2 與 ZED Mini),位置追蹤模組會融合視覺與慣性資料來提供改良後的位置追蹤。
存取 IMU 感測器資料的程式碼如下:
[pastacode lang=”python” manual=”sensors_data%20%3D%20sl.SensorsData()%0Azed.get_sensors_data(sensors_data%2C%20sl.TIME_REFERENCE.IMAGE)%0Azed_imu%20%3D%20zed_sensors.get_imu_data()%0A%23%20Get%20IMU%20orientation%0Azed_imu_pose%20%3D%20sl.Transform()%0Aox%20%3D%20round(zed_imu.get_pose(zed_imu_pose).get_orientation().get()%5B0%5D%2C%203)%0Aoy%20%3D%20round(zed_imu.get_pose(zed_imu_pose).get_orientation().get()%5B1%5D%2C%203)%0Aoz%20%3D%20round(zed_imu.get_pose(zed_imu_pose).get_orientation().get()%5B2%5D%2C%203)%0Aow%20%3D%20round(zed_imu.get_pose(zed_imu_pose).get_orientation().get()%5B3%5D%2C%203)%0Aprint(%22IMU%20Orientation%3A%20Ox%3A%20%7B0%7D%2C%20Oy%3A%20%7B1%7D%2C%20Oz%20%7B2%7D%2C%20Ow%3A%20%7B3%7D%5Cn%22.format(ox%2C%20oy%2C%20oz%2C%20ow))%0A%23%20Get%20IMU%20acceleration%0Aacceleration%20%3D%20%5B0%2C0%2C0%5D%0Azed_imu.get_linear_acceleration(acceleration)%0Aax%20%3D%20round(acceleration%5B0%5D%2C%203)%0Aay%20%3D%20round(acceleration%5B1%5D%2C%203)%0Aaz%20%3D%20round(acceleration%5B2%5D%2C%203)%0Aprint(%22IMU%20Acceleration%3A%20Ax%3A%20%7B0%7D%2C%20Ay%3A%20%7B1%7D%2C%20Az%20%7B2%7D%5Cn%22.format(ax%2C%20ay%2C%20az))” message=”access IMU sensor data” highlight=”” provider=”manual”/]更多關於 Camera-IMU 與其他板載感測器請參考 Sensors section.
關閉攝影機
追蹤 ZED 攝影機中的姿勢狀態 1000 個畫面(幀)之後,停用追蹤模組並關閉攝影機。
[pastacode lang=”python” manual=”%23%20Disable%20positional%20tracking%20and%20close%20the%20camera%0Azed.disable_positional_tracking()%3B%0Azed.close()” message=”close the camera” highlight=”” provider=”manual”/]如果您想知道如何在3D視窗中取得與顯示攝影機的即時位置與方位、位置資料轉換以及如何改變坐標系與坐標單位,請參考 Motion Tracking 範例程式。
下一步
請接續閱讀[範例05 Spatial Mapping]與 [範例06 Object Detection] 來深入學習。
註:本文經授權之後翻譯自 https://www.stereolabs.com/docs/tutorials/positional-tracking/





