HObject转换为QImage

2 篇文章 0 订阅

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

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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值