goulashsoup
goulashsoup

Reputation: 3076

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

I access a camera via USB using a library from the manufacturer. I receive the information about an image via a struct:

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;

All information i have about the image come from tImageInfos struct comments so there are now additional information elsewhere. I know from the tImagePixelType that the image pixel type is 12 bit: Mono12:

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

My goal is to display the image on a QLabel but first i have to use the raw data (pData) and turn it into an image.

With my current approach i just get displayed stripes maybe due wrong processing of the raw data. The procedure happens in a member function of a 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));
}

Here is some output I gathered from an image:

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

Do you know how to transform the image data from void pointer correctly? Then please, enlighten me...

Upvotes: 1

Views: 1250

Answers (1)

goulashsoup
goulashsoup

Reputation: 3076

Here is a working solution:

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

Upvotes: 1

Related Questions