几何尺寸与公差论坛

 找回密码
 注册
查看: 1098|回复: 3

三通道,Hobject怎么转Bitmap

[复制链接]
发表于 2022-12-20 09:50:14 | 显示全部楼层 |阅读模式
Bot:

Hobject转Bitmap的方法:

1、使用HDevelop函数:

ConvertImageType:将Hobject图像转换为Bitmap格式;

2、使用Halcon C++ API函数:

HobjectToHbitmap:将Hobject图像转换为Bitmap格式;

3、使用Halcon .NET API函数:

HOperatorSet.HObjectToHBitmap:将Hobject图像转换为Bitmap格式;
 楼主| 发表于 2022-12-20 09:51:40 | 显示全部楼层
Bot:

HObject ho_Image;
Hlong width, height;

// 获取图片宽高
GetImageSize(ho_Image, &width, &height);

// 创建位图
HObject ho_Bitmap;
GenImageConst(&ho_Bitmap, "byte", width, height);

// HObject转Bitmap
ConvertImageType(ho_Image, &ho_Bitmap, "byte");
 楼主| 发表于 2022-12-20 09:56:43 | 显示全部楼层
Bot:
        /// </summary>
        /// <param name="ho_Image">HObject图像</param>
        /// <returns>Bitmap图像</returns>
        public static Bitmap HObjectToBitmap(HObject ho_Image)
        {
            Bitmap bmpImage = null;
            HObject ho_R, ho_G, ho_B;
            HOperatorSet.GenEmptyObj(out ho_R);
            HOperatorSet.GenEmptyObj(out ho_G);
            HOperatorSet.GenEmptyObj(out ho_B);
            HTuple hv_Width, hv_Height;
            HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
            HOperatorSet.Decompose3(ho_Image, out ho_R, out ho_G, out ho_B);
            byte[] bR = new byte[hv_Width * hv_Height];
            byte[] bG = new byte[hv_Width * hv_Height];
            byte[] bB = new byte[hv_Width * hv_Height];
            HOperatorSet.GetGrayval(ho_R, out bR);
            HOperatorSet.GetGrayval(ho_G, out bG);
            HOperatorSet.GetGrayval(ho_B, out bB);
            bmpImage = new Bitmap(hv_Width.I, hv_Height.I, PixelFormat.Format24bppRgb);
            Rectangle rect = new Rectangle(0, 0, hv_Width.I, hv_Height.I);
            BitmapData bmpData = bmpImage.LockBits(rect, ImageLockMode.ReadWrite, bmpImage.PixelFormat);
            IntPtr ptr = bmpData.Scan0;
            int bytes = bmpData.Stride * bmpImage.Height;
            byte[] rgbValues = new byte[bytes];
            for (int i = 0; i < hv_Height.I; i++)
            {
                for (int j = 0; j < hv_Width.I; j++)
                {
                    rgbValues[(i * bmpData.Stride) + (j * 3)] = bB[i * hv_Width.I + j];
                    rgbValues[(i * bmpData.Stride) + (j * 3) + 1] = bG[i * hv_Width.I + j];
                    rgbValues[(i * bmpData.Stride) + (j * 3) + 2] = bR[i * hv_Width.I + j];
                }
            }
            System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
            bmpImage.UnlockBits(bmpData);
            ho_R.Dispose();
            ho_G.Dispose();
            ho_B.Dispose();
            return bmpImage;
        }
    }
}
 楼主| 发表于 2022-12-20 09:59:22 | 显示全部楼层
https://blog.csdn.net/zzyzxb/article/details/126547068
public static Bitmap Honject2Bitmap24(HObject hObject)
{
    //获取图像尺寸
    HTuple width0 = new HTuple();
    HTuple height0 = new HTuple();
    HTuple Pointer = new HTuple();
    HTuple type = new HTuple();
    HTuple width = new HTuple();
    HTuple height = new HTuple();
    HObject InterImage = new HObject();
    HOperatorSet.GetImageSize(hObject, out width0, out height0);
    HOperatorSet.GetImageSize(hObject, out width0, out height0);
    //创建交错格式图像
    HOperatorSet.InterleaveChannels(hObject, out InterImage, "rgb", 4 * width0, 0);
    //获取交错格式图像指针
    HOperatorSet.GetImagePointer1(InterImage, out Pointer, out type, out width, out height);
    IntPtr ptr = Pointer;
    //构建新Bitmap图像
    Bitmap bitmap = new Bitmap(width / 4, height, width, PixelFormat.Format24bppRgb, ptr);
    return bitmap;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|几何尺寸与公差论坛

GMT+8, 2024-12-22 00:35 , Processed in 0.039641 second(s), 21 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表