0

I am trying to write jpg's to a video using cv2. I define a mock image as such:

fake_image = np.array([[[122, 131, 140]] * 416] * 416).astype(np.uint8)

Next, I write this to a video using:

out = cv2.VideoWriter('mock_video.mp4',cv2.VideoWriter_fourcc(*'DIVX'), 1, (416, 416))
out.write(fake_image)
out.release()

Then, I try to read the frames of my video:

vid = cv2.VideoCapture('mock_video.mp4')
return_value, frame = vid.read()

And now the rgb values have changed! Rather than being [122, 131, 140], they are now [118, 129, 137]!

Why is this happening and can I stop it from happening?

Edit: yes i know cv2 uses BGR, that is not what is going on (it is not the case that the order of channels is reversed)

hchw
  • 1,388
  • 8
  • 14
  • ^ that is not what is going on... please read the question :/ – hchw Oct 15 '19 at 00:52
  • `mp4` similar to `jpg` changes pixels to better compress it. For images you could use `.png` but for video it may not be good solution. – furas Oct 15 '19 at 00:52
  • on Wikipedia you can see list of [lossless video codecs](https://en.wikipedia.org/wiki/List_of_codecs#Lossless_video_compression) and you would have to check in `cv2` documentation if it can use some of these codecs. – furas Oct 15 '19 at 00:58
  • Using Google I found: [Lossless compression for video in opencv](https://stackoverflow.com/questions/15728939/lossless-compression-for-video-in-opencv) and [How to write lossless video in Python](https://www.imagetracking.org.uk/2017/12/how-to-write-lossless-video-in-python/) – furas Oct 15 '19 at 01:00

0 Answers0