Files
yehsin b8f84264fb Merge pull request #30144 from Yehsin0815:fix-odometry-python-sample
samples: fix Python odometry sample for 5.x bindings - #30144

Fixes #30143

`samples/python/odometry.py` fails on 5.x:

- It uses `cv.DEPTH`, `cv.RGB` and `cv.RGB_DEPTH`, which the Python bindings don't export. The enum values are exposed as `cv.OdometryType_*`, the same names `modules/ptcloud/misc/python/test_odometry.py` uses. Every mode fails with `AttributeError`.
- RGB mode calls `compute(rgb1, rgb2, Rt)`, and that overload always raises "This odometry algorithm requires depth and rgb data simultaneously" (`modules/ptcloud/src/odometry.cpp`). The sample now loads the depth frames in RGB mode too and uses the 5-argument overload.

Tested with opencv-python 5.0.0.93 on Windows 11 and Linux: `--algo DEPTH`, `RGB`, `RGB_DEPTH` and the default (all modes) each run and print `Rt`.

This change was prepared with the help of an AI coding tool; I reviewed it and reproduced the bug and the fix locally.

### Pull Request Readiness Checklist

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
- [ ] The feature is well documented and sample code can be built with the project CMake
2026-10-05 14:34:13 +03:00

68 lines
2.2 KiB
Python

#!/usr/bin/env python
import numpy as np
import cv2 as cv
import argparse
import sys
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'--algo',
help="""DEPTH - works with depth,
RGB - works with images (depth frames are required too),
RGB_DEPTH - works with all,
default - runs all algos""",
default="")
parser.add_argument(
'-src_d',
'--source_depth_frame',
default="")
parser.add_argument(
'-dst_d',
'--destination_depth_frame',
default="")
parser.add_argument(
'-src_rgb',
'--source_rgb_frame',
default="")
parser.add_argument(
'-dst_rgb',
'--destination_rgb_frame',
default="")
args = parser.parse_args()
if args.algo in ("RGB_DEPTH", "DEPTH", "RGB", ""):
source_depth_frame = cv.samples.findFile(args.source_depth_frame)
destination_depth_frame = cv.samples.findFile(args.destination_depth_frame)
depth1 = cv.imread(source_depth_frame, cv.IMREAD_ANYDEPTH).astype(np.float32)
depth2 = cv.imread(destination_depth_frame, cv.IMREAD_ANYDEPTH).astype(np.float32)
if args.algo == "RGB_DEPTH" or args.algo == "RGB" or args.algo == "":
source_rgb_frame = cv.samples.findFile(args.source_rgb_frame)
destination_rgb_frame = cv.samples.findFile(args.destination_rgb_frame)
rgb1 = cv.imread(source_rgb_frame, cv.IMREAD_COLOR)
rgb2 = cv.imread(destination_rgb_frame, cv.IMREAD_COLOR)
if args.algo == "DEPTH" or args.algo == "":
odometry = cv.Odometry(cv.OdometryType_DEPTH)
Rt = np.zeros((4, 4))
odometry.compute(depth1, depth2, Rt)
print("Rt:\n {}".format(Rt))
if args.algo == "RGB" or args.algo == "":
odometry = cv.Odometry(cv.OdometryType_RGB)
Rt = np.zeros((4, 4))
odometry.compute(depth1, rgb1, depth2, rgb2, Rt)
print("Rt:\n {}".format(Rt))
if args.algo == "RGB_DEPTH" or args.algo == "":
odometry = cv.Odometry(cv.OdometryType_RGB_DEPTH)
Rt = np.zeros((4, 4))
odometry.compute(depth1, rgb1, depth2, rgb2, Rt)
print("Rt:\n {}".format(Rt))
if __name__ == '__main__':
main()