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

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

我使用制造商提供的库通过 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 class:

的成员函数中
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

你知道如何正确的从void指针转换成图像数据吗? 那请赐教...

这是一个可行的解决方案:

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();
}