如何在YUV到RGB转换期间垂直翻转缓冲区

时间:2016-08-08 14:37:45

标签: c++ image image-processing

编辑:问题不明确抱歉,我更新并添加了详细信息。

我有一个带有图像数据(YUV格式)的缓冲区,我将其转换为RGB格式。问题是,我想垂直翻转图像(反转Y位置)。

我现在可以做的是将YUV数据转换为缓冲区中的RGB数据,然后垂直翻转此缓冲区。

以下是此代码的工作代码:

unsigned char* DeckLinkCaptureDelegate::convertYUVtoRGB(void* frameBytes)
{
    unsigned char *mycopy = new unsigned char[height*width*3];
    unsigned char *flippedCopy = new unsigned char[height*width*3];
    unsigned char* pData = (unsigned char *) frameBytes;


    //Conversion from YUV to RGB

    for(int i = 0, j=0; i < width * height * 3; i+=6, j+=4)
    {
        unsigned char v = pData[j];
        unsigned char y = pData[j+1];
        unsigned char u = pData[j+2];

        mycopy[i+2] = 1.0*y + 8 + 1.402*(v-128);               // r
        mycopy[i+1] = 1.0*y - 0.34413*(u-128) - 0.71414*(v-128);   // g
        mycopy[i] = 1.0*y + 1.772*(u-128) + 0;                            // b

        y = pData[j+3];
        mycopy[i+5] = 1.0*y + 8 + 1.402*(v-128);               // r
        mycopy[i+4] = 1.0*y - 0.34413*(u-128) - 0.71414*(v-128);   // g
        mycopy[i+3] = 1.0*y + 1.772*(u-128) + 0;
    }

    //Vertical flip

    for (int i = 0; i < width; ++i) {
        for (int j = 0; j < height; ++j) {
            for (int k = 0; k < 3; ++k) {
                flippedCopy[(i + j * width) * 3 + k] = mycopy[(i + (height - 1 - j) * width) * 3 + k];
            }
        }
    }

    return flippedCopy;   
}

我想要获得的性能是将缓冲区 DURING 从YUV转换为RGB。我不知道怎么做,优素福的回答帮助了我,所以这就是我现在所拥有的:

unsigned char* DeckLinkCaptureDelegate::convertYUVtoRGB(void* frameBytes)
{
    unsigned char *mycopy = new unsigned char[height*width*3];
    unsigned char* pData = (unsigned char *) frameBytes;
    int k = height - 1;

    for(int i = 0, j=0; i < width * height * 3; i+=6, j+=4)
    {
            unsigned char v = pData[j];
            unsigned char y = pData[j+1];
            unsigned char u = pData[j+2];

            mycopy[(width*k*3) + i+2] = 1.0*y + 8 + 1.402*(v-128);               // r
            mycopy[(width*k*3) + i+1] = 1.0*y - 0.34413*(u-128) - 0.71414*(v-128);   // g
            mycopy[(width*k*3) + i] = 1.0*y + 1.772*(u-128) + 0;                            // b

            y = pData[j+3];
            mycopy[(width*k*3) + i+5] = 1.0*y + 8 + 1.402*(v-128);               // r
            mycopy[(width*k*3) + i+4] = 1.0*y - 0.34413*(u-128) - 0.71414*(v-128);   // g
            mycopy[(width*k*3) + i+3] = 1.0*y + 1.772*(u-128) + 0;

            if (<i multiple of (width*3)-1>){ 
                k = k - 2;
            }

    }

    return mycopy;
}

如果我是正确的,假设if条件正确,这应该有效。但是我不知道如何表达这个if条件,因为i每次增加6,所以我可以“跳过”正确的时刻递减k

我希望我足够清楚。感谢

1 个答案:

答案 0 :(得分:1)

我认为宽度是均匀的,否则“在新行中减少” - 如果复杂,那么你必须使用2个循环。我没有测试过,但应该看起来像这样;

unsigned char* DeckLinkCaptureDelegate::convertYUVtoRGB(void* frameBytes)
{
    unsigned char *mycopy = new unsigned char[height*width*3];
    unsigned char* pData = (unsigned char *) frameBytes;
    unsigned int k = height - 1;

    for(int i = 0, j=0; i < width * height * 3; i+=6, j+=4)
    {
        unsigned char v = pData[j];
        unsigned char y = pData[j+1];
        unsigned char u = pData[j+2];

        mycopy[(width*k*3) + i+2] = 1.0*y + 8 + 1.402*(v-128);               // r
        mycopy[(width*k*3) + i+1] = 1.0*y - 0.34413*(u-128) - 0.71414*(v-128);   // g
        mycopy[(width*k*3) + i] = 1.0*y + 1.772*(u-128) + 0;                            // b

        y = pData[j+3];
        mycopy[(width*k*3) + i+5] = 1.0*y + 8 + 1.402*(v-128);               // r
        mycopy[(width*k*3) + i+4] = 1.0*y - 0.34413*(u-128) - 0.71414*(v-128);   // g
        mycopy[(width*k*3) + i+3] = 1.0*y + 1.772*(u-128) + 0;

        if (mod(i, width*3) == 0) //reduce in new line (i am not sure how to reduce it, you should think about here)
            k = k - 2;
    }

    return mycopy;
}

并将您的问题标记为图像处理等,而不仅仅是c ++