Qt - 将空指针(原始数据)转换为 QImage 并将其显示在标签上

Qt - Transform void pointer (raw data) to QImage and display it on label

本文关键字:显示 标签 QImage 转换 空指针 原始数据 Qt      更新时间:2023-10-16

我使用制造商提供的库通过USB访问相机。我通过结构体接收有关图像的信息:

typedef struct
{
/*! Buffer handle which contains new data. */
BUFF_HANDLE hBuffer;    
/* Pointer to the beginning of the image datas (including MetaDatas(1024 bytes) if any) */
void * pDatas; 
/*! Total Buffer Payload size in bytes (including image, MetaDatas(1024 bytes) and additional bytes required by USB3 protocol */
size_t iImageSize;
/*! Width of the image (not including metadata) */
size_t iImageWidth;
/*! Height of the image */
size_t iImageHeight;
/*! Pixel Type */
tImagePixelType eImagePixelType;
/*! Line Pitch: corresponds to the number of bytes between two consecutive lines
note if MetaDatas are not activated, Line Pitch is equal to iImageWidth*eImagePixelType
note else MetaDatas are located immediately after the number of bytes corresponding to iImageWidth*eImagePixelType
*/
size_t iLinePitch;
/*! Buffer BlockId */
unsigned long long iBlockId;
...
} tImageInfos;

我拥有的有关图像的所有信息都来自结构注释tImageInfos因此现在其他地方还有其他信息。我从tImagePixelType知道图像像素类型12 bit: Mono12

/*! Image Pixel Type */
typedef enum
{
...
/*! Pixel Type 12 bit: Mono12 */
eMono12   = 3
} tImagePixelType;

我的目标是在QLabel上显示图像,但首先我必须使用原始数据(pData(并将其转换为图像。

使用我目前的方法,我只是显示条纹,可能是由于对原始数据的错误处理。该过程发生在QMainWindow类的成员函数中:

void TragVisMain::setImageAndShowPicture(QString message, tImageInfos imageInfos)
{
// Some message
addMessageLineToLogOutput(message);
// Create image from raw data
QImage *img = new QImage(
(uchar *) imageInfos.pDatas, 
static_cast<int>(imageInfos.iImageWidth), 
static_cast<int>(imageInfos.iImageHeight), 
QImage::Format_Mono
);
ui->logOutput->appendPlainText(
QString("image infos: [ height: %1, width: %2, iLinePitch: %3, adress: %4 ]")
.arg(
// image height
QString::number(img->height()),
// output width
QString::number(img->width()),
// iLinePitch
QString::number(imageInfos.iLinePitch),
// address of pData
QString("0x%1").arg((quintptr)imageInfos.pDatas, QT_POINTER_SIZE * 2, 16, QChar('0'))
)
);

this->iv.setImageFromQImage(*img);
this->iv.show();
// pixel type is 3 => 12 bit: Mono12
ui->logOutput->appendPlainText(QString("tImagePixelType: ").append(QString::number(imageInfos.eImagePixelType)));
}

setImageFromQImage

void ImageViewer::setImageFromQImage(QImage image)
{
this->ui->imageLabel->setPixmap(QPixmap::fromImage(image));
}

这是我从图像中收集的一些输出:

image infos: [ height: 1024, width: 1280, iLinePitch: 2560, adress: 0x00000175da5b3040 ]
tImagePixelType: 3

您知道如何正确转换无效指针的图像数据吗? 那么请开导我...

这是一个有效的解决方案:

cv::Mat openCvImage(1024, 1280, CV_16UC1, imageInfos.pDatas);
openCvImage.convertTo(openCvImage, CV_8UC1, 0.04);
QImage qImage = QImage(
openCvImage.data,
1280,
1024,
QImage::Format_Grayscale8
);
imageLabel.setPixmap(QPixmap::fromImage(img));
if(!imageLabel.isVisible()) {
imageLabel.show();
}