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
类的成员函数中:
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();
}
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 字符串-C++后显示的随机字符
- 继承期间显示未知行为的子类
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 程序崩溃并显示"std::out_of_range"错误
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- C 和 C++ 中开关语句的案例标签的常量值,但显示不同的行为
- 如何使QLineSeries/QXYSeries仅显示一个点标签
- 如何在主窗口标签的对话框中显示QLineEdit的输入
- Qt - 将空指针(原始数据)转换为 QImage 并将其显示在标签上
- 如何在qt标签中显示em破折号字符
- 什么会导致"静态"标签在窗口上不显示WS_VISIBLE
- qprogressdialog未显示文本标签
- 如何在 VTK 中的鼠标光标附近显示标签
- 从文件中读取HTML标签并在终端中正确显示它们
- 在Qt标签中显示IplImages
- 如何在QwtSlider中显示值标签
- 我的应用程序:标签显示一个值,当我按下按钮时应该增加或减少