dajuric
dajuric

Reputation: 2507

Lossless x265 encoding - YUV420 colorspace

I have a png bgr24 image that is encoded to mp4 with x265 encoder in a lossless way. I am aware that bgr to yuv420p conversion is lossy. I am trying to get the bit-exact reconstruction of a encoded image compared to an image that went to the following process: bgr24 -> yuv -> bgr24. A difference image show massive reconstruction error, and it should be blank. Why is that ?

The code:

import cv2
import numpy as np
import subprocess as sp

#convert image to lossless h265 encoded video
sp.run("ffmpeg.exe -i test.png -c:v libx265 -pix_fmt yuv420p -x265-params loseless=1 out.mp4 -y")
sourceIm = cv2.imread("test.png")

#ensure that the source image goes to the same process because yuv420 is lossy
sourceIm = cv2.cvtColor(sourceIm, cv2.COLOR_BGR2YUV_I420)
sourceIm = cv2.cvtColor(sourceIm, cv2.COLOR_YUV2BGR_I420)

vr = cv2.VideoCapture("out.mp4")
ret, frame = vr.read()
vr.release()

diffIm = np.abs(sourceIm - frame)

cv2.imshow("-", diffIm)
cv2.waitKey()
cv2.destroyAllWindows()

Source image: source image

Diff image (output - which should be blank): output image

Upvotes: 0

Views: 705

Answers (0)

Related Questions