Halcon HObject和C#的Bitmap图像互转

H a l c o n H O b j e c t 和 C S h a r p 的 B i t m a p 图 像 互 转 Halcon HObject和CSharp的Bitmap图像互转 HalconHObjectCSharpBitmap

HObject转bitmap(灰度图)

public void HObject2Bitmap8(HObject image, out Bitmap res)
{
    HTuple hpoint, type, width, height; 
    const int Alpha = 255; 
    HOperatorSet.GetImagePointer1(image, out hpoint, out type, out width, out height); 
    res = new Bitmap(width, height, PixelFormat.Format8bppIndexed); 
    ColorPalette pal = res.Palette; 
    for (int i = 0; i <= 255; i++) 
    { pal.Entries[i] = Color.FromArgb(Alpha, i, i, i); }

    res.Palette = pal; Rectangle rect = new Rectangle(0, 0, width, height); 
    BitmapData bitmapData = res.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); 
    int PixelSize = Bitmap.GetPixelFormatSize(bitmapData.PixelFormat) / 8; 
    IntPtr ptr1 = bitmapData.Scan0; 
    IntPtr ptr2 = hpoint; int bytes = width * height; 
    byte[] rgbvalues = new byte[bytes]; 
    System.Runtime.InteropServices.Marshal.Copy(ptr2, rgbvalues, 0, bytes); 
    System.Runtime.InteropServices.Marshal.Copy(rgbvalues, 0, ptr1, bytes);  
    res.UnlockBits(bitmapData);
  
   }

HObject转bitmap(3通道图)

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;

}

Bitmap转HObject (灰度图)

 
public static void Bitmap2HObjectBpp8(Bitmap bmp, out HObject image)
{
    try
    {
        Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);

        BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);

        HOperatorSet.GenImage1(out image, "byte", bmp.Width, bmp.Height, srcBmpData.Scan0);
        bmp.UnlockBits(srcBmpData);
    }
    catch (Exception ex)
    {
        image = null;
    }
}

Bitmap转HObject(3通道图)

public static void Bitmap2HObjectBpp24(Bitmap bmp, out HObject image)  //90ms
{
    try
    {
        Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);

        BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
        HOperatorSet.GenImageInterleaved(out image, srcBmpData.Scan0, "bgr", bmp.Width, bmp.Height, 0, "byte", 0, 0, 0, 0, -1, 0);
        bmp.UnlockBits(srcBmpData);

    }
    catch (Exception ex)
    {
        image = null;
    }
}

using HalconDotNet;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace bitmaptohobject
{
    class Program
    {
        static void Main(string[] args)
        {
            Bitmap bit = new Bitmap(@"D:/图片/ocr/MaxImage.bmp");  //100M的.bmp格式文件,
            HObject ho;
            HOperatorSet.GenEmptyObj(out ho);
            Stopwatch st = new Stopwatch();
            st.Start();
            // Bitmap2HImageBpp24(bit,out ho);
            Bitmap2CVHImageBpp24(bit, out ho);
            //Bitmap2HObjectBpp24(bit, out ho);
            st.Stop();
            HOperatorSet.WriteImage(ho,"png",0,"1.png");
           
            Console.WriteLine(st.ElapsedMilliseconds.ToString());
            Console.ReadKey();
        }
        public static void Bitmap2HObjectBpp24(Bitmap bmp, out HObject image)  //90ms
        {
            try
            {
                Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
 
                BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
                HOperatorSet.GenImageInterleaved(out image, srcBmpData.Scan0, "bgr", bmp.Width, bmp.Height, 0, "byte", 0, 0, 0, 0, -1, 0);
                bmp.UnlockBits(srcBmpData);
 
            }
            catch (Exception ex)
            {
                image = null;
            }
        }
 
        public static void Bitmap2HImageBpp24(Bitmap bmp, out HObject image) //转换500ms
        {
            try
            {
                Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
 
                BitmapData bmp_data = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
                byte[] arrayR = new byte[bmp_data.Width * bmp_data.Height];//红色数组 
                byte[] arrayG = new byte[bmp_data.Width * bmp_data.Height];//绿色数组 
                byte[] arrayB = new byte[bmp_data.Width * bmp_data.Height];//蓝色数组 
                unsafe
                {
                    byte* pBmp = (byte*)bmp_data.Scan0;//BitMap的头指针 
                                                       //下面的循环分别提取出红绿蓝三色放入三个数组 
                    for (int R = 0; R < bmp_data.Height; R++)
                    {
                        for (int C = 0; C < bmp_data.Width; C++)
                        {
                            //因为内存BitMap的储存方式,行宽用Stride算,C*3是因为这是三通道,另外BitMap是按BGR储存的 
                            byte* pBase = pBmp + bmp_data.Stride * R + C * 3;
                            arrayR[R * bmp_data.Width + C] = *(pBase + 2);
                            arrayG[R * bmp_data.Width + C] = *(pBase + 1);
                            arrayB[R * bmp_data.Width + C] = *(pBase);
                        }
                    }
                    fixed (byte* pR = arrayR, pG = arrayG, pB = arrayB)
                    {
                        HOperatorSet.GenImage3(out image, "byte", bmp_data.Width, bmp_data.Height,
                                                                   new IntPtr(pR), new IntPtr(pG), new IntPtr(pB));
                        //如果这里报错,仔细看看前面有没有写错 
                    }
                }             
 
 
            }
            catch (Exception ex)
            {
                image = null;
            }
        }
 
        public static void Bitmap2CVHImageBpp24(Bitmap bmp, out HObject image)   //150ms
        {
            try
            {
                Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
 
                BitmapData bmp_data = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
 
 
                Mat M = new Mat(bmp.Height,bmp.Width,MatType.CV_8UC3,bmp_data.Scan0,0);
                Mat[] MS = M.Split();
                IntPtr red= MS[0].Ptr(); IntPtr g = MS[1].Ptr();
                IntPtr b = MS[2].Ptr();
 
                HOperatorSet.GenImage3(out image, "byte", bmp_data.Width, bmp_data.Height, red, g, b);
 
            }
            catch (Exception ex)
            {
                image = null;
            }
        }
 
        public static void Bitmap2HObjectBpp8(Bitmap bmp, out HObject image)
        {
            try
            {
                Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
 
                BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
 
                HOperatorSet.GenImage1(out image, "byte", bmp.Width, bmp.Height, srcBmpData.Scan0);
                bmp.UnlockBits(srcBmpData);
            }
            catch (Exception ex)
            {
                image = null;
            }
        }
 
    }
}

你可能感兴趣的:(c#,halcon)