import cv2 def bgr_to_yuv420(bgr_img): # Step 1: Convert BGR to YUV 4:4:4 yuv_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2YUV) # Step 2: Extract Y, U, V channels Y, U, V = cv2.split(yuv_img) # Step 3: Downsample U and V channels (Subsample by a factor of 2) U_downsampled = cv2.resize(U, (U.shape[1] // 2, U.shape[0] // 2), interpolation=cv2.INTER_LINEAR) V_downsampled = cv2.resize(V, (V.shape[1] // 2, V.shape[0] // 2), interpolation=cv2.INTER_LINEAR) # Step 4: Combine Y, U_downsampled, V_downsampled to get YUV 4:2:0 format return Y, U_downsampled, V_downsampled