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);
        }
    }

你可能感兴趣的:(计算机视觉,算法,图像处理)