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;
}
相关推荐
__BMGT()1 小时前
C++ QT 打开图片
前端·c++·qt
TNTLWT2 小时前
Qt控件:交互控件
开发语言·qt
溟洵5 小时前
【C++ Qt】布局管理器
开发语言·c++·qt
C++ 老炮儿的技术栈6 小时前
自定义CString类与MFC CString类接口对比
c语言·c++·windows·qt·mfc
20242817李臻8 小时前
20242817-李臻-课下作业:Qt和Sqlite
jvm·qt·sqlite
夜松云11 小时前
Qt信号槽机制与UI设计完全指南:从基础原理到实战应用
开发语言·qt·ui·qt designer·布局管理·参数传递·qt信号槽
mahuifa20 小时前
(7)python开发经验
python·qt·pyside6·开发经验
galaxy_strive21 小时前
qtc++ qdebug日志生成
开发语言·c++·qt
TNTLWT21 小时前
Qt功能区:简介与安装
开发语言·qt
凯雀安全1 天前
printspoofer的RPC调用接口的简单代码
qt·网络协议·rpc