为YUV2RGB转换优化python嵌套循环

2024-09-27 21:22:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我们刚刚从海康威视(Hikvision)中导出了YUV2RGB算法。然而,对于720H x 1280W屏幕分辨率的python转换来说,对于一个RGB帧,720x1280=921600轮的计算时间太长(15秒)。有谁知道如何优化以下2个大嵌套循环?YUV2RGB算法是:

def YUV2RGB(Y1,U1,V1,dwHeight,dwWidth):#函数调用

RGB1 = np.zeros(dwHeight * dwWidth * 3, dtype=np.uint8)  # create 1 dimensional empty np array with 720x1280x3

for i in range (0, dwHeight):    #0-720
    for j in range (0, dwWidth): #0-1280 

        # print "cv"

        Y = Y1[i * dwWidth + j]; 
        U = U1[(i / 2) * (dwWidth / 2) + (j / 2)]; 
        V = V1[(i / 2) * (dwWidth / 2) + (j / 2)];

        R = Y + (U - 128) + (((U - 128) * 103) >> 8); 
        G = Y - (((V - 128) * 88) >> 8) - (((U - 128) * 183) >> 8); 
        B = Y + (V - 128) + (((V - 128) * 198) >> 8);

        R = max(0, min(255, R)); 
        G = max(0, min(255, G)); 
        B = max(0, min(255, B));

        RGB1[3 * (i * dwWidth + j)] = B; 
        RGB1[3 * (i * dwWidth + j) + 1] = G; 
        RGB1[3 * (i * dwWidth + j) + 2] = R; 

RGB = np.reshape(RGB1, (dwHeight, dwWidth, 3))

print ("rgb.shape:")
print RGB.shape

return RGB

“对于范围内的i(0,dwHeight):#0-720 对于范围(0,dwWidth):#0-1280”

太大了。任何优化的方法。谢谢。在

马修


Tags: in算法fornprgbminmaxv1

热门问题