BGR数据与NV12互转

import numpy as np
import cv2
img = cv2.imread("greendata182.bmp")
flags = [i for i in dir(cv2) if i.startswith('COLOR_')]
print(flags)
def mergeUV(u, v):
    if u.shape == v.shape:
        uv = np.zeros(shape=(u.shape[0], u.shape[1]*2))
        for i in range(0, u.shape[0]):
            for j in range(0, u.shape[1]):
                uv[i, 2*j] = u[i, j]
                uv[i, 2*j+1] = v[i, j]
        return uv
    else:
        print("size of Channel U is different with Channel V")


def rgb2nv12(image):
    if image.ndim == 3:
        b = image[:, :, 0]
        g = image[:, :, 1]
        r = image[:, :, 2]
        y = (0.299*r+0.587*g+0.114*b)
        u = (-0.169*r-0.331*g+0.5*b+128)[::2, ::2]
        v = (0.5*r-0.419*g-0.081*b+128)[::2, ::2]
        
        #y = 0.257*r  + 0.504*g  + 0.098*b + 16
        #u = (-0.148*r-0.291*g+0.439*b+128)[::2, ::2]
        #v = (0.439*r-0.368*g-0.071*b+128)[::2, ::2]
        
        uv = mergeUV(u, v)
        yuv = np.vstack((y, uv))
        return yuv.astype(np.uint8)
    else:
        print("image is not BGR format")
yuv = rgb2nv12(img)
yuv.tofile("yuv.nv12")

方式2

img = cv2.imread("it_0_rgb.bmp")
B,G,R = cv2.split(img)
Y = (0.299*R + 0.587*G+0.114*B).astype(np.uint8)
U = (- 0.1687* R - 0.3313* G + 0.5 *B + 128).astype(np.uint8)
V = (0.5* R - 0.4187 *G - 0.0813 *B + 128).astype(np.uint8)
U = U[::2,:]
V = V[::2,:]
U[:,1::2] = V[:,1::2]
YUV= np.vstack((Y,U))
YUV.tofile("yuv.data")

NV12转BGR

import cv2
import numpy as np
import glob
import os
import tqdm
import argparse

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--input", required=True,help="path to the input rgb")
ap.add_argument("-o", "--output", required=True,help="path to output directory to store augmentation examples")
ap.add_argument("-hh", "--height",type=int,default=2160,help="image H")
ap.add_argument("-w", "--width", type=int,default=3840,help="image W")
args = vars(ap.parse_args())



def NV212RGB(yuv_path,width,height):
    with open(yuv_path, 'rb') as f:
        yuvdata = np.fromfile(f, dtype=np.uint8)
    #yuvdata = yuvdata[4 * 4 :]
    cv_format=cv2.COLOR_YUV2BGR_NV12
    bgr_img = cv2.cvtColor(yuvdata.reshape((height*3//2, width)), cv_format)                    
    return bgr_img


if __name__ == '__main__':
    input_folder = args['input']
    out_folder = args['output']

    width  = int(args['width'])
    height = int(args['height'])

    yuv_path_list = glob.glob(os.path.join(input_folder,"*.yuv"))
    for yuv_path in tqdm.tqdm(yuv_path_list):                                        
        bgr_img = NV212RGB(yuv_path,width,height)
        basename = os.path.basename(yuv_path)
        savepath = os.path.join(out_folder,basename.replace(".yuv",".bmp"))
        #cv2.imwrite(savepath,bgr_img)
        cv2.imencode('.bmp', bgr_img[:,:4096,:])[1].tofile(savepath)

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值