Skip to content

fix yuv and luv color conversions - #82

Merged
jasmcaus merged 2 commits into
jasmcaus:devfrom
omadson:fix-yuv-luv-conversions
Sep 23, 2026
Merged

jasmcaus merged 2 commits into
jasmcaus:devfrom
omadson:fix-yuv-luv-conversions

Conversation

@omadson

@omadson omadson commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

Description

Ten color conversions compute a BGR intermediate and then discard it, converting the original YUV/LUV array as if it were BGR. The result is silently wrong pixel values:

  • yuv2gray, yuv2hsv, yuv2hls, yuv2lab, yuv2luv (caer/color/_yuv.py)
  • luv2gray, luv2hsv, luv2hls, luv2lab, luv2yuv (caer/color/_luv.py)

caer.to_gray(), to_hsv(), to_hls(), to_lab(), to_yuv() and to_luv() call these functions, so they were affected too for YUV/LUV inputs.

Reproduction

import cv2 as cv
import numpy as np
import caer

bgr = caer.data.sunrise(rgb=False)
yuv = caer.to_tensor(cv.cvtColor(bgr, cv.COLOR_BGR2YUV), cspace="yuv")
expected = cv.cvtColor(cv.cvtColor(yuv, cv.COLOR_YUV2BGR), cv.COLOR_BGR2GRAY)

print(np.array_equal(caer.yuv2gray(yuv), expected))          # False on dev, True with this PR
print(np.array_equal(caer.yuv2gray(yuv), caer.bgr2gray(yuv)))  # True on dev: YUV data treated as BGR

Tests

The existing tests only checked the output shape and the cspace flag, so the bug went unnoticed. Each of the 10 affected tests in tests/color/test_yuv.py and tests/color/test_luv.py now also asserts the result equals OpenCV's X -> BGR -> Y conversion. These asserts fail on dev (10 failed) and pass with the fix. The full suite passes locally (pytest -v ./tests: 83 passed).

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (fix or new feature that would cause existing functionality to change)
  • Docstrings/Documentation updated
  • New Feature to Caer
  • New Examples to examples
  • New tests added to cover the changes

Related Issues

Closes #81

Checklist:

  • I have followed the contribution guidelines and code style for this project.
  • I have added tests covering my contributions (important).
  • I have updated the documentation accordingly.
  • This PR is being made to dev and not master (important).
  • I will squash merge this PR into dev (optional)

@jasmcaus

Copy link
Copy Markdown
Owner

Kinda picky here, but pls rename img to tens

@omadson

omadson commented Sep 23, 2026

Copy link
Copy Markdown
Contributor Author

No problem, @jasmcaus. It's done!

@jasmcaus
jasmcaus merged commit 98862a4 into jasmcaus:dev Sep 23, 2026
5 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

yuv2gray() / luv2gray() (and other yuv2* / luv2* conversions) seem to ignore the intermediate BGR conversion. Is this intended?

2 participants