gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:24:06 25 4
gpt4 key购买 nike

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

我有一个缓冲区,其中包含我转换为 RGB 格式的图像数据(YUV 格式)。问题是,我想垂直翻转图像(反转 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 的转换。我不知道该怎么做,Yusuf 的回答帮助了我,所以这就是我目前所拥有的:

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

希望我说得够清楚了。谢谢

最佳答案

我假设宽度是偶数,否则“在新行中减少”——如果会很复杂,那么你必须使用 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++

关于c++ - 如何在 YUV 到 RGB 转换期间垂直翻转缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38832066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com