Halcon 图像转QImage
时间: 2023-06-15 13:08:10 浏览: 336
Halcon 提供了将图像转换为 QImage 的函数,具体如下:
```
QImage HObject2QImage(HObject hoImage)
{
HTuple hvChannels, hvType, hvWidth, hvHeight, hvPtr;
HImage hImage;
HObject hoChannel;
QImage qImage;
// 获取图像信息
CountChannels(hoImage, &hvChannels);
GetImagePointer1(hoImage, &hvPtr, &hvType, &hvWidth, &hvHeight);
// 如果是 RGB 图像,将其转换为 GRAY 图像
if (hvChannels == 3)
{
RGB1ToGray(hoImage, &hoChannel);
hImage = HImage(hoChannel);
}
else
{
hImage = HImage(hoImage);
}
// 创建 QImage
switch (hvType.I())
{
case 1: // 8-bit unsigned integer
qImage = QImage((unsigned char*)hvPtr.TuplePointer(), hvWidth.I(), hvHeight.I(), QImage::Format_Grayscale8);
break;
case 2: // 16-bit unsigned integer
qImage = QImage((unsigned char*)hvPtr.TuplePointer(), hvWidth.I(), hvHeight.I(), QImage::Format_Grayscale16);
break;
case 4: // 32-bit floating point
qImage = QImage((unsigned char*)hvPtr.TuplePointer(), hvWidth.I(), hvHeight.I(), QImage::Format_Grayscale8);
break;
}
// 如果是 RGB 图像,将其转换为 QImage
if (hvChannels == 3)
{
QVector<QRgb> colorTable;
for (int i = 0; i < 256; i++)
{
colorTable.append(qRgb(i, i, i));
}
qImage.setColorTable(colorTable);
}
return qImage;
}
```
使用方法如下:
```
HObject hoImage;
ReadImage(&hoImage, "image.jpg");
QImage qImage = HObject2QImage(hoImage);
```
阅读全文