Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VideoCapture: wrong colors with BT2020 (HDR) colorspace videos #24185

Open
4 tasks done
Kepnu4 opened this issue Aug 22, 2023 · 3 comments · May be fixed by #24290
Open
4 tasks done

VideoCapture: wrong colors with BT2020 (HDR) colorspace videos #24185

Kepnu4 opened this issue Aug 22, 2023 · 3 comments · May be fixed by #24290
Assignees
Milestone

Comments

@Kepnu4
Copy link

Kepnu4 commented Aug 22, 2023

System Information

OpenCV version: 4.8.0.76
Operating System / Platform: Ubuntu 18.04
Python version: 3.9.16

Detailed description

Reading HDR video with VideoCapture results in complete different colors. Also combining frames back to HDR video with ffmpeg results in visible artefacts.

Steps to reproduce

Example HDR video

example.mov

ffprobe -v error -select_streams v:0 -show_entries stream=codec_name,pix_fmt,color_range,color_space,color_primaries,color_transfer,bit_rate -of json example.mov:

"codec_name": "hevc",
"pix_fmt": "yuv420p10le",
"color_range": "tv",
"color_space": "bt2020nc",
"color_transfer": "arib-std-b67",
"color_primaries": "bt2020",
"bit_rate": "12231190"

script for reading frames:

import cv2
import os

# Input video file path
video_path = 'example.mov'

# Output directory to save frames
output_directory = 'frames/'

# Create output directory if it doesn't exist
os.makedirs(output_directory, exist_ok=True)

# Open the video file
cap = cv2.VideoCapture(video_path)

frame_number = 0
while cap.isOpened():
    ret, frame = cap.read()

    if not ret:
        break

    frame_number += 1
    frame_filename = os.path.join(output_directory, f'frame_{frame_number:04d}.png')

    # Save the frame as an image file
    cv2.imwrite(frame_filename, frame)

    print(f"Saved {frame_filename}")

# Release the video capture
cap.release()

The result frames have visible color shift:
frame_1.png

Combine back with ffmpeg:
ffmpeg -i frames/frame_%04d.png -c:v hevc -pix_fmt yuv420p10le -color_range tv -colorspace bt2020nc -color_primaries bt2020 -color_trc arib-std-b67 -b:v 12231190 -tag:v hvc1 out.mov

out.mov

The result video has visible artifacts:

out.png

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)
@asmorkalov
Copy link
Contributor

@Kepnu4 Thanks a lot for the issue report. I investigated OpenCV decoding pipeline with FFmpeg and made some experiments. Decoding pipeline detects color space and other options correctly, but does not apply any filters. I'm looking on a proper way to take colorspace in account and return back, when have some solution for it.

@asmorkalov
Copy link
Contributor

@Kepnu4 Could you build OpenCV with PR above and report status to the issue? The patch shifts colors on my side and makes frames very close to what I get with console FFmpeg. The patch also affects existing code behaviour and I need to investigate possible side effects.

@ykk648
Copy link

ykk648 commented Aug 15, 2024

any news?

@asmorkalov asmorkalov modified the milestones: 4.11.0, 4.12.0 Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants