Skip to content

viz: fix BGR/RGB channel swap in readCloud PLY, add PLY I/O and traje…#4119

Open
rtkartista wants to merge 1 commit into
opencv:4.xfrom
rtkartista:feature/viz-ply-io-tests-may5
Open

viz: fix BGR/RGB channel swap in readCloud PLY, add PLY I/O and traje…#4119
rtkartista wants to merge 1 commit into
opencv:4.xfrom
rtkartista:feature/viz-ply-io-tests-may5

Conversation

@rtkartista
Copy link
Copy Markdown

@rtkartista rtkartista commented May 6, 2026

Summary

Fix a long-standing BGR/RGB channel swap bug in cv::viz::readCloud() for PLY files,
and add a test suite for PLY cloud I/O and trajectory read/write roundtrips.

Bug fixed

readCloud() returns colors in RGB order (as stored in PLY spec) rather than
BGR (OpenCV convention). writeCloud() correctly converts BGR→RGB on write,
but the inverse conversion was missing on read. Every color channel 0 and 2
were silently swapped for all callers.

Fix: swap channels 0↔2 in-place after sink->Write() in vizcore.cpp.

Tests added (modules/viz/test/test_ply_io.cpp)

PLY cloud I/O

  • ASCII roundtrip — positions preserved within 1e-4 tolerance
  • Binary roundtrip — positions bit-exact for float32
  • Binary roundtrip with colors — regression test for the BGR/RGB fix
  • Large cloud (500k pts) binary perf smoke test (<2s read+write)
  • Empty cloud — no crash

Trajectory I/O

  • Affine3d write/read roundtrip — 1e-9 tolerance
  • Affine3f write/read roundtrip — 1e-5 tolerance
  • Non-zero start index preserved correctly
  • readTrajectory stops cleanly on missing file
  • Empty range returns empty result

Secondary issue noted

readTrajectory() with an untyped std::vector OutputArray and zero results
calls Mat(traj).convertTo(_traj, _traj.depth()) where _traj.depth() returns
-1, causing undefined behavior. Guard with if (!traj.empty()) or assign a
typed empty array. Tracked separately.

Related

Contributes toward the SLAM module I/O test coverage noted in the
GSoC 2026 / 2026 wiki.

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • 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
  • The PR is proposed to the proper branch
  • 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
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

…ctory tests

- readCloud() returned colors in RGB order (PLY spec) instead of BGR
  (OpenCV convention). Fix: swap channels 0 and 2 after sink->Write().
- writeCloud() was already correct (BGR->RGB on write).
- Add test_ply_io.cpp covering:
    * ASCII PLY position roundtrip with 1e-4 tolerance
    * Binary PLY position roundtrip (bit-exact)
    * Binary PLY color roundtrip (catches the BGR/RGB bug)
    * Large cloud (500k pts) binary perf smoke test
    * Empty cloud no-crash
    * Affine3d trajectory write/read roundtrip
    * Affine3f trajectory write/read roundtrip (float precision)
    * Non-zero start index for writeTrajectory/readTrajectory
    * readTrajectory early stop on missing file
    * Empty range returns empty result (also surfaces convertTo
      depth==-1 bug when OutputArray is untyped and traj is empty)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants