gpt4 book ai didi

c++ - 使用 C++ 将 RGBA 转换为 YUV422 格式 - 没有得到正确的输出

转载 作者:太空宇宙 更新时间:2023-11-03 22:50:56 29 4
gpt4 key购买 nike

我想将图像从 RGBA 转换为 YUV422格式。下面是我的代码:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace cv;
using namespace std;

#define CLIP(X) ( (X) > 255 ? 255 : (X) < 0 ? 0 : X)
#define RGB2Y(R, G, B) CLIP(( ( 66 * (R) + 129 * (G) + 25 * (B) + 128) >> 8) + 16)
#define RGB2U(R, G, B) CLIP(( ( -38 * (R) - 74 * (G) + 112 * (B) + 128) >> 8) + 128)
#define RGB2V(R, G, B) CLIP(( ( 112 * (R) - 94 * (G) - 18 * (B) + 128) >> 8) + 128)

int main(int argc, char *argv[])
{

Mat in_img, in_RGB, out_yuv;
in_img = imread(argv[1],1);
if(!in_img.data)
{
printf("Failed to load the image ... %s\n!", argv[1]);
return -1;
}
short imgwidth = in_img.cols;
short imgheight = in_img.rows;

cvtColor(in_img, in_RGB, CV_BGR2RGB);
out_yuv.create(imgheight, imgwidth, CV_16U);

imwrite("RGB.jpg",in_RGB);

for(int i = 0; i< imgheight; i++)
for(int j = 0; j< imgwidth; j = j+2) {
unsigned int val = in_RGB.at<unsigned int>(i,j);

/*********** for 1st pixel ***********/
unsigned int tmp = val;
unsigned char B = (unsigned char)(((tmp<<16) >> 24) | 0x000000ff ); //Extracting B channel data

tmp = val;
unsigned char G = (unsigned char)(((tmp<<8) >> 24) | 0x000000ff ); //Extracting G channel data

tmp = val;
unsigned char R = (unsigned char)((tmp >> 24) | 0x000000ff ); //Extracting R channel data

unsigned char Y1 = RGB2Y(R, G, B);
unsigned char U = RGB2U(R, G, B);
unsigned char V = RGB2V(R, G, B);

/*********** for 2nd pixel ***********/
unsigned int val1 = in_RGB.at<unsigned int>(i,j+1);
unsigned int tmp1 = val1;
unsigned char B1 = (unsigned char)(((tmp1<<16) >> 24) | 0x000000ff );

tmp1 = val1;
unsigned char G1 = (unsigned char)(((tmp1<<8) >> 24) | 0x000000ff );

tmp1 = val1;
unsigned char R1 = (unsigned char)((tmp1 >> 24) | 0x000000ff );

unsigned char Y2 = RGB2Y(R, G, B);

/********** writing into out image in U-Y1-V-Y2 format *********************/

unsigned short p1 = ((U << 8) | Y1) ;
unsigned short p2 = ((V << 8) | Y2) ;
out_yuv.at<unsigned short>(i,j) = p1;
out_yuv.at<unsigned short>(i,j+1) = p2;

}
imwrite("YUV422.png",out_yuv);
return 0;
}

但我没有得到正确的输出图像。我也尝试用 Y1-U-Y2-V 格式编写它,输出仍然相同。上面的代码可能有什么问题,或者是创建问题的输出图像格式(.png/.jpg)? (cv::imwrite 不允许以 .yuv 格式保存)

最佳答案

out_yuv.create(imgheight, imgwidth, CV_16U);
...
imwrite("YUV422.png",out_yuv);

这里有几个问题:

  • imwrite考虑 CV_16U to be mono channel .
  • 你不能在 png 中写 yuv。您必须转储原始数据,并使用特殊的 yuv 查看器,例如 raw yuvplayer以确保您的数据正确无误。

编辑:
请注意,原始像素首字母缩写词有点大端,即最左边的值是内存中的最低有效字节(大多数时候,视频人们喜欢混淆)

  • RGBA 表示 R = RGB[0] , G = RGB[1] , B = RGB[2] , A = RGB[3]
  • YUYV 表示在缓冲区中 p表示 p[0]是 Y0,p[1]是你,p[2]是 Y1,p[3]是V

你的轮类恰恰相反:)

编辑 2:如果您的图像仍然是杂乱的,您似乎愿意按照平面格式(YUV422P 又名 YV16)进行书写。平面格式意味着每个 channel 都在一个单独的、连续的平面上。为目标数据( unsigned char*std::vector<unsigned char> )使用普通的旧缓冲区会更清楚。

unsigned char* out_yuv_ptr = new unsigned char[imgwidth*imgheight*2];
unsigned char* y_ptr = out_yuv_ptr;
unsigned char* u_ptr = y_ptr + imgwidth*imgheight;
unsigned char* v_ptr = u_ptr + imgwidth*imgheight/2;
...
*y_ptr = Y1;
++y_ptr
*y_ptr = Y2;
++y_ptr
*u_ptr = U;
++u_ptr;
*v_ptr = V;
++v_ptr;

关于c++ - 使用 C++ 将 RGBA 转换为 YUV422 格式 - 没有得到正确的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37808695/

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