HObject转换为QImage

在Qt中需要先将HObject转换为QImage,然后再使用。

cpp 复制代码
int calcBytesPerLine(int width, int bitCounts)
{
    //把图像的宽度数值换算成4的整数倍
    int step = 0;
    if (bitCounts >= 8)
    {
        step = (width * bitCounts / 8 + 3) / 4 * 4;
    }
    else
    {
        step = (width * bitCounts + 31) / 32 * 4;
    }

    return step;
}


QImage MainWindow::HObject2QImage(const HalconCpp::HObject &Hobj) //Halcon中的HObject类型转QImage类型
{
    HalconCpp::HTuple hv_Chn = HalconCpp::HTuple();
    HalconCpp::HTuple hv_Length;
    HalconCpp::HTuple hv_Type;
    HalconCpp::HObject ho_Img;
    QImage image;

    ConvertImageType(Hobj, &ho_Img, "byte");
    CountChannels(ho_Img, &hv_Chn);
    TupleLength(hv_Chn, &hv_Length);
    if (hv_Length.L() == 0)
    {
        return image;
    }

    HalconCpp::HTuple hv_Width;
    HalconCpp::HTuple hv_Height;
    int width = 0;
    int height = 0;
    int step = 0;

    if (hv_Chn[0].I() == 1)
    {
        HalconCpp::HTuple hv_Ptr;
        GetImagePointer1(ho_Img, &hv_Ptr, &hv_Type, &hv_Width, &hv_Height);
        width = (int)hv_Width;
        height = (int)hv_Height;
        step = calcBytesPerLine(width, 8);
        BYTE *p = (BYTE *)hv_Ptr[0].L(); //必须是L(),不能是I()
        QImage temp = QImage(width, height, QImage::Format_Grayscale8);
        image = temp.copy();
        BYTE *data8 = image.bits();
        int pix = 0;

        for (int i = 0; i < height; i++)
        {
            for (int k = 0; k < width; k++)
            {
                pix = step * i + k;
                data8[pix + 0] = p[width * i + k];
            }
        }
    }
    else if (hv_Chn[0].I() == 3)
    {
        HalconCpp::HTuple hv_PtrR, hv_PtrG, hv_PtrB;
        GetImagePointer3(ho_Img, &hv_PtrR, &hv_PtrG, &hv_PtrB, &hv_Type, &hv_Width, &hv_Height);
        width = (int)hv_Width;
        height = (int)hv_Height;
        step = calcBytesPerLine(width, 24); //三通道
        BYTE *pr = (BYTE *)hv_PtrR[0].L();  //必须是L(),不能是I()
        BYTE *pg = (BYTE *)hv_PtrG[0].L();
        BYTE *pb = (BYTE *)hv_PtrB[0].L();

        QImage temp = QImage(width, height, QImage::Format_RGB888);
        image = temp.copy();
        BYTE *data24 = image.bits();
        int pix = 0;

        for (int i = 0; i < height; i++)
        {
            for (int k = 0; k < width; k++)
            {
                pix = step * i + k * 3;
                data24[pix + 0] = pr[width * i + k]; //QImage格式是RGB,不是BGR
                data24[pix + 1] = pg[width * i + k];
                data24[pix + 2] = pb[width * i + k];
            }
        }
    }

    return image;
}
相关推荐
熬夜的猪仔21 分钟前
QT多线程
qt
꧁坚持很酷꧂3 小时前
QT登录系统界面
开发语言·qt
达斯维达的大眼睛12 小时前
qt小项目,简单的音乐播放器
开发语言·qt
Ryan_Gosling15 小时前
QT-异步编程
开发语言·qt
信必诺20 小时前
GStreamer —— 2.2、Windows下Qt加载GStreamer库后运行 - “教程2:GStreamer 概念“(附:完整源码)
qt·gstreamer
宁静致远202121 小时前
qt 操作多个sqlite文件
qt·sqlite·嵌入式linux开发
laimaxgg21 小时前
Qt常用控件之表格QTableWidget
开发语言·前端·c++·qt·qt5·qt6.3
四维碎片1 天前
Visual studio + Qt 项目配置管理
ide·qt·visual studio
结衣结衣.1 天前
【Qt】自定义信号和槽函数
开发语言·c++·qt·c++11
勇敢滴勇2 天前
解决 Qt 项目中出现的OPENSSL_Uplink(00007FFA9E04EC88,08): no OPENSSL_Applink
开发语言·qt