gpt4 book ai didi

c++ - 将 Yuv420 转换为 rgb 并在 qt 像素图上显示

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:58:32 31 4
gpt4 key购买 nike

我想把一张yuv图片转成RGB显示在QT pixmap上。我现在所拥有的是我看到一个空的像素图。

int convertYUVtoRGB(int y, int u, int v) {
int r,g,b;

r = y + (int)(1.402f*v);
g = y - (int)(0.344f*u +0.714f*v);
b = y + (int)(1.772f*u);

r = r>255? 255 : r<0 ? 0 : r;
g = g>255? 255 : g<0 ? 0 : g;
b = b>255? 255 : b<0 ? 0 : b;
return 0xff000000 | (b<<16) | (g<<8) | r;
}


unsigned char * convertYUV420_NV21toRGB8888(unsigned char data[78080], int width, int height) {
int size = width*height;
int offset = size;
unsigned char * pixels = new unsigned char[size];
int u, v, y1, y2, y3, y4;

// i percorre os Y and the final pixels
// k percorre os pixles U e V
for(int i=0, k=0; i < size; i+=2, k+=2) {
y1 = data[i ]&0xff;
y2 = data[i+1]&0xff;
y3 = data[width+i ]&0xff;
y4 = data[width+i+1]&0xff;

u = data[offset+k ]&0xff;
v = data[offset+k+1]&0xff;
u = u-128;
v = v-128;

pixels[i ] = convertYUVtoRGB(y1, u, v);
pixels[i+1] = convertYUVtoRGB(y2, u, v);
pixels[width+i ] = convertYUVtoRGB(y3, u, v);
pixels[width+i+1] = convertYUVtoRGB(y4, u, v);

if (i!=0 && (i+2)%width==0)
i+=width;
}

return pixels;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
unsigned char * buffer;
unsigned char * image = NULL;
QPixmap pixmap;
QImage img(320, 122, QImage::Format_ARGB32_Premultiplied);
img.fill(QColor(Qt::white).rgb());

ifstream is;
is.open ("bunny.yuv", ios::binary );
// is.seekg (0, ios::end);
// length = is.tellg();
// is.seekg (0, ios::beg);
buffer = new unsigned char[78081];
is.read((char * )buffer,78080);
is.close();

/*
for (int x = 0; x < 10; ++x) {
for (int y = 0; y < 10; ++y) {
img.setPixel(x, y, qRgb(0, 0, 0));
}
}
*/


for(int i = 0; i < 78080; i++)
qDebug() << buffer[i]<< endl;

image = convertYUV420_NV21toRGB8888(buffer,320,122);
QByteArray byteImage((const char *)image);
pixmap.loadFromData(byteImage);


QLabel myLabel;
myLabel.setPixmap(pixmap);
myLabel.setGeometry(20,100,320,122);
myLabel.show();

return a.exec();
}

编辑:我添加了正确的格式和括号。仍然是同样的问题。亲切的问候,

最佳答案

当你在 Jason 的帖子中完成计算后 - 像素图的最佳格式是 QImage::Format_ARGB32_Premultiplied - 格式是 Blue byte Green byte Red byte 0xff

关于c++ - 将 Yuv420 转换为 rgb 并在 qt 像素图上显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13804844/

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