HALCON Hobject和Bitmap相互转换

灰度图HObject转Bitmap

public static Bitmap HObject2Bitmap(HObject ho)
{
    try
    {
        HTuple type, width, height, pointer;
        //HOperatorSet.AccessChannel(ho, out ho, 1);
        HOperatorSet.GetImagePointer1(ho, out pointer, out type, out width, out height);
        //himg.GetImagePointer1(out type, out width, out height);
        Bitmap bmp = new Bitmap(width.I, height.I, PixelFormat.Format8bppIndexed);
        ColorPalette pal = bmp.Palette;
        for (int i = 0; i <= 255; i++)
        {
            pal.Entries[i] = Color.FromArgb(255, i, i, i);
        }
        bmp.Palette = pal;
        BitmapData bitmapData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
        int PixelSize = Bitmap.GetPixelFormatSize(bitmapData.PixelFormat) / 8;
        int stride = bitmapData.Stride;
        int ptr = bitmapData.Scan0.ToInt32();
        for (int i = 0; i < height; i++)
        {
            CopyMemory(ptr, pointer, width * PixelSize);
            pointer += width;
            ptr += bitmapData.Stride;
        }
        bmp.UnlockBits(bitmapData);
        return bmp;
    }
    catch (Exception exc)
    {
        return null;
    }
}

灰度图的HObject转HImage

public HImage HObject2HImage1(HObject hObj)
{
    HImage image = new HImage();
    HTuple type, width, height, pointer;
    HOperatorSet.GetImagePointer1(hObj, out pointer,out type, out width, out height);
    image.GenImage1(type, width, height, pointer);
    return image;
}

Bitbmp转Hobject

public void BitmapToHobject(Bitmap bitmap, out HObject image)
        {
            int height = bitmap.Height;//图像的高度
            int width = bitmap.Width;//图像的宽度


            Rectangle imgRect = new Rectangle(0, 0, width, height);
            BitmapData bitData = bitmap.LockBits(imgRect, ImageLockMode.ReadOnly, bitmap.PixelFormat);


            //由于Bitmap图像每行的字节数必须保持为4的倍数,因此在行的字节数不满足这个条件时,会对行进行补充,步幅数Stride表示的就是补充过后的每行的字节数,也成为扫描宽度
            int stride = bitData.Stride;
            switch (bitmap.PixelFormat)
            {
                case PixelFormat.Format8bppIndexed:
                    {
                        unsafe
                        {
                            int count = height * width;
                            byte[] data = new byte[count];
                            byte* bptr = (byte*)bitData.Scan0;
                            fixed (byte* pData = data)
                            {
                                for (int i = 0; i < height; i++)
                                {
                                    for (int j = 0; j < width; j++)
                                    {
                                        /*
                                         *
                                         如果直接使用GenImage1,传入BitData的Scan0(图像首元素的指针)作为内存指针的话,如果图像不满足行为4的倍数,那么填充的部分也会参与进来,从而导致图像扭曲
                                         *
                                         *
                                         */


                                        //舍去填充的部分
                                        data[i * width + j] = bptr[i * stride + j];
                                    }

                                }
                                HOperatorSet.GenImage1(out image, "byte", width, height, new IntPtr(pData));
                            }
                        }

                    }
                    break;
                case PixelFormat.Format24bppRgb:
                    {
                        unsafe
                        {
                            int count = height * width * 3;//24位的BitMap每个像素三个字节
                            byte[] data = new byte[count];
                            byte* bptr = (byte*)bitData.Scan0;
                            fixed (byte* pData = data)
                            {
                                for (int i = 0; i < height; i++)
                                {
                                    for (int j = 0; j < width * 3; j++)
                                    {
                                        //每个通道的像素需一一对应
                                        data[i * width * 3 + j] = bptr[i * stride + j];
                                    }
                                }
                                HOperatorSet.GenImageInterleaved(out image, new IntPtr(pData), "bgr", bitmap.Width, bitmap.Height, 0, "byte", bitmap.Width, bitmap.Height, 0, 0, -1, 0);
                            }
                        }
                    }
                    break;
                default:
                    {
                        unsafe
                        {
                            int count = height * width;
                            byte[] data = new byte[count];
                            byte* bptr = (byte*)bitData.Scan0;
                            fixed (byte* pData = data)
                            {
                                for (int i = 0; i < height; i++)
                                {
                                    for (int j = 0; j < width; j++)
                                    {
                                        data[i * width + j] = bptr[i * stride + j];
                                    }

                                }
                                HOperatorSet.GenImage1(out image, "byte", width, height, new IntPtr(pData));
                            }
                        }

                    }
                    break;
            }
            bitmap.UnlockBits(bitData);
        }
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值