我试图将带有YUV数据的图像缓冲区转换为QImage(RGBA8888格式)。
以下是从缓冲区到IplImage的转换:
void DeckLinkCaptureDelegate::convertFrameToOpenCV(void* frameBytes, IplImage * m_RGB){
if(!m_RGB) m_RGB = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
unsigned char* pData = (unsigned char *) frameBytes;
for(int i = 0, j=0; i < width * height * 3; i+=6, j+=4)
{
unsigned char u = pData[j];
unsigned char y = pData[j+1];
unsigned char v = pData[j+2];
//fprintf(stderr, "%d\n", v);
m_RGB->imageData[i+2] = 1.0*y + 8 + 1.402*(v-128); // r
m_RGB->imageData[i+1] = 1.0*y - 0.34413*(u-128) - 0.71414*(v-128); // g
m_RGB->imageData[i] = 1.0*y + 1.772*(u-128) + 0; // b
y = pData[j+3];
m_RGB->imageData[i+5] = 1.0*y + 8 + 1.402*(v-128); // r
m_RGB->imageData[i+4] = 1.0*y - 0.34413*(u-128) - 0.71414*(v-128); // g
m_RGB->imageData[i+3] = 1.0*y + 1.772*(u-128) + 0;
}
}
目标是将IplImage * m_RGB替换为QImage对象。稍后可以使用Qt函数更改QImage的格式以转换QImage格式。我设法从IplImage获得了一个QImage,但是我试图绕过这一步以完全删除IplImage步骤。
问题是,我很难理解上面的代码,所以我不知道如何为QImage做到这一点。
你能帮我做一下,或者至少理解上面的代码吗?
感谢。
编辑:这是我目前所拥有的,但它不起作用。void DeckLinkCaptureDelegate::convertFrameToOpenCV(void* frameBytes, QImage m_RGB){
//if(!m_RGB) m_RGB = QImage(width, height, QImage::Format_RGB888);
unsigned char* pData = (unsigned char *) frameBytes;
for(int i = 0, j=0; i < width * height * 3; i+=6, j+=4)
{
unsigned char u = pData[j];
unsigned char y = pData[j+1];
unsigned char v = pData[j+2];
//fprintf(stderr, "%d\n", v);
m_RGB.bits()[i+2] = 1.0*y + 8 + 1.402*(v-128); // r
m_RGB.bits()[i+1] = 1.0*y - 0.34413*(u-128) - 0.71414*(v-128); // g
m_RGB.bits()[i] = 1.0*y + 1.772*(u-128) + 0; // b
y = pData[j+3];
m_RGB.bits()[i+5] = 1.0*y + 8 + 1.402*(v-128); // r
m_RGB.bits()[i+4] = 1.0*y - 0.34413*(u-128) - 0.71414*(v-128); // g
m_RGB.bits()[i+3] = 1.0*y + 1.772*(u-128) + 0;
}
}
答案 0 :(得分:0)
创建另一个unsigned char数组(比如pDataRGB),将yuv(pData)转换为rgb(pDataRGB),然后创建QImage like-
QImage *m_RGB = new QImage(pDataRGB, width, height, QImage::Format_RGB888);
如果你只想使用Qimage,那么setPixel数据如下: -
m_RGB->setPixel(i, j, qRgb(r, g, b));