1

I'm writing a small script that shows some things in Pygame and then writes those same things to a video using OpenCV with Python. The weird thing is that I noticed that the colors in the video showed slightly darker.

So I wrote a simpler program that writes a basic video with a dark background and a blue square moving horizontally. I took a snapshot from the video and compared its BGR values with the ones specified in the program. They were, in fact, darker.

It's the first time I play with writing videos so I don't know much about colorspaces, so maybe this is something obvious but I haven't found the solution to match both colors.

import cv2
import numpy as np
from cv2 import VideoWriter, VideoWriter_fourcc

# Video parameters
width = 1920
height = 1080
fps = 30
seconds = 5
video = VideoWriter('./square.avi', VideoWriter_fourcc(*'mp4v'), float(fps), (width, height))

# Square parameters
cellHeight = 100
cellWidth = 500
xPos = 100
yPos = 100
cellColor = (255, 194, 102)   # BGR, ends up as (255, 188, 92) in the written frames
backroundColor = (25, 25, 25) # BGR, ends up as (22, 25, 23)

# Frame loop
for _ in range(seconds * fps):

    frame = np.empty((height, width, 3), dtype=np.uint8)
    frame[:,:] = backroundColor

    pt1 = (xPos, yPos)                         # NW corner
    pt2 = (xPos + cellWidth, yPos + cellWidth) # SE corner

    cv2.rectangle(frame, pt1=pt1, pt2=pt2, color=cellColor, thickness=-1)
    video.write(frame)
    xPos += 10

video.release()

Here you can see the difference between pygame and the video. Despite it is not the same frame, you can see the different colors, especially the green and red.

enter image description here

dvilela
  • 1,200
  • 12
  • 29
  • 1
    Maybe some problems with encoder. Can you try `VideoWriter_fourcc(*'avc1')` for codec? – Gralex Nov 10 '20 at 09:43
  • @Gralex It seems I can't use avc1 as I installed opencv through pip. I've read that I must build OpenCV from source to be able to use it. I'm looking on how to do it. – dvilela Nov 10 '20 at 10:02
  • Ok, I built from source, used avc1 but the colors still show darker. – dvilela Nov 10 '20 at 11:01
  • 2
    mp4v aka MPEG-4 uses a lossy compression. You may try to read your video file with `VideoCapture` and see that the background color is not `25 25 25` (`21 24 22` on my machine). If you use [HFYU](https://stackoverflow.com/a/769025/704244) lossless codec (`VideoWriter_fourcc(*'HFYU')`), then you should get `25 25 25` back again. – bartolo-otrit Nov 10 '20 at 12:06
  • Yes, I think that's it. Both mpeg-4 and AVC1 share the same problem, but HFYU shows the color correctly. Thanks. – dvilela Nov 10 '20 at 12:23
  • 1
    @bartolo-otrit put you comment as an answer so it can easily be seen, ta. – DrBwts Nov 10 '20 at 12:28
  • Yes, and so I can select it as the answer. – dvilela Nov 10 '20 at 12:30
  • Perhaps a difference in color profiles. – fmw42 Nov 10 '20 at 18:11
  • 1
    @DrBwts A lossless codec isn't a solution in my opinion (because of the output size), it's just an explanation. You may try to use ffmpeg [filters](https://video.stackexchange.com/questions/20962/ffmpeg-color-correction-gamma-brightness-and-saturation) to increase brightness. If it's the way to go, then the answers from [Pipe raw OpenCV images to FFmpeg](https://stackoverflow.com/questions/5825173/pipe-raw-opencv-images-to-ffmpeg) could help. – bartolo-otrit Nov 11 '20 at 12:03

0 Answers0