Halcon HImage 与 Qt QImage 的相互转换

Halcon HImage 与 Qt QImage 的相互转换

 

    /**
     * @brief HImage2QImage 将 Halcon 的 HImage 转换为 Qt 的 QImage
     * @param from HImage ,暂时只支持 8bits 灰度图像和 8bits 的 3 通道彩色图像
     * @param to QImage ,这里 from 和 to 不共享内存。如果 to 的内存大小合适,那么就不用重新分配内存。所以可以加快速度。
     * @return  true 表示转换成功,false 表示转换失败
     */
    bool HImage2QImage(HalconCpp::HImage &from, QImage &to)
    {
        Hlong width;
        Hlong height;
        from.GetImageSize(&width, &height);
    
        HTuple channels = from.CountChannels();
        HTuple type = from.GetImageType();
    
        if( strcmp(type[0].S(), "byte" )) // 如果不是 byte 类型,则失败
        {
            return false;
        }
    
        QImage::Format format;
        switch(channels[0].I())
        {
        case 1:
            format = QImage::Format_Grayscale8;
            break;
        case 3:
            format = QImage::Format_RGB32;
            break;
        default:
            return false;
        }
    
        if(to.width() != width || to.height() != height || to.format() != format)
        {
            to = QImage(static_cast<int>(width),
                        static_cast<int>(height),
                        format);
        }
        HString Type;
        if(channels[0].I() == 1)
        {
            unsigned char * pSrc = reinterpret_cast<unsigned char *>( from.GetImagePointer1(&Type, &width, &height) );
            memcpy( to.bits(), pSrc, static_cast<size_t>(width) * static_cast<size_t>(height) );
            return true;
        }
        else if(channels[0].I() == 3)
        {
            uchar *R, *G, *B;
            from.GetImagePointer3(reinterpret_cast<void **>(&R),
                                  reinterpret_cast<void **>(&G),
                                  reinterpret_cast<void **>(&B), &Type, &width, &height);
    
            for(int row = 0; row < height; row ++)
            {
                QRgb* line = reinterpret_cast<QRgb*>(to.scanLine(row));
                for(int col = 0; col < width; col ++)
                {
                    line[col] = qRgb(*R++, *G++, *B++);
                }
            }
            return true;
        }
    
        return false;
    }

 


 

    /**
     * @brief QImage2HImage 将 Qt QImage 转换为 Halcon 的 HImage
     * @param from 输入的 QImage
     * @param to 输出的 HImage ,from 和 to 不共享内存数据。 每次都会为 to 重新分配内存。
     * @return true 表示转换成功,false 表示转换失败。
     */
    bool QImage2HImage(QImage &from, HalconCpp::HImage &to)
    {
        if(from.isNull()) return false;
    
        int width = from.width(), height = from.height();
        QImage::Format format = from.format();
    
        if(format == QImage::Format_RGB32 ||
                format == QImage::Format_ARGB32 ||
                format == QImage::Format_ARGB32_Premultiplied)
        {
            to.GenImageInterleaved(from.bits(), "rgbx", width, height, 0,  "byte", width, height, 0, 0, 8, 0);
            return true;
        }
        else if(format == QImage::Format_RGB888)
        {
            to.GenImageInterleaved(from.bits(), "rgb", width, height, 0,  "byte", width, height, 0, 0, 8, 0);
            return true;
        }
        else if(format == QImage::Format_Grayscale8 || format == QImage::Format_Indexed8)
        {
            to.GenImage1("byte", width, height, from.bits());
            return true;
        }
        return false;
    }

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值