Converted bgr to yuv420p, and then converted yuv420p back to bgr, ended up a RGB image

run the following to do the conversions

def yuv420p_from_cv():
    bgr_img = cv.imread(r"bgr.jpg")
    print(bgr_img.shape)
    yuv420p_img = cv.cvtColor(bgr_img, cv.COLOR_BGR2YUV_I420)
    print(yuv420p_img.shape)
    np.save("cv_yuv420p", yuv420p_img)
    #
    new_bgr = cv.cvtColor(yuv420p_img, cv.COLOR_YUV420P2BGR)
    cv.imwrite("new_bgr.jpg", new_bgr)
    print(new_bgr.shape)
    cv.imshow("adsd", new_bgr)
    cv.waitKey()

the new_bgr looked like a rgb image, it was a wrong image with r and b misplacement.

the left one is the bgr_img, the original one, and the right one is the new_bgr.

but if i converted the yuv420p_img to rgb, like

new_bgr = cv.cvtColor(yuv420p_img, cv.COLOR_YUV420P2RGB)

then the new_bgr looked fine, it looked exact like the bgr_img.

1 Like

this is the original image

well, apparently, to convert back to bgr, we should pass COLOR_YUV2BGR_I420 instead of

COLOR_YUV420P2BGR into cv.cvtColor

so, YUV_I420 and YUV420P are two different formats in opencv, what’s the difference?