halcon格式的图像转位图 --Hobjct转Bitmap

halcon格式的图像转位图 --Hobjct转Bitmap


#应用场景
主要用于halcon的Hobject类型的图像转换为Bitmap的格式,先把Himage的图像转为Hobject再转为Bitmap,
目前AI的算法主要是使用位图进行计算`

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HalconDotNet;
using System.Drawing;
using System.Drawing.Imaging;

namespace ConsoleApplication4
{
    class Program
    {
    // Initialize local and output iconic variable   
        static void Main(string[] args)
        {
            Bitmap s;
            HObject ho_Image1;
            HOperatorSet.ReadImage(out ho_Image1, "C:/Users/Desktop/pps.jpg");
            HObject2Bpp24Net45(ho_Image1, out s);
        }
        //net4.5及以上环境的hobject转bitmap24方法
        /// <summary>
        /// HObject转24位Bitmap,net4.5及以上版本
        /// </summary>
        /// <param name="image"></param>
        /// <param name="res"></param>
        public static void HObject2Bpp24Net45(HObject image, out Bitmap res)
        {
            try
            {
                HTuple hred, hgreen, hblue, type, width, height;
              
                HOperatorSet.GetImagePointer3(image, out hred, out hgreen, out hblue, out type, out width, out height);
                res = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
                Rectangle rect = new Rectangle(0, 0, width, height);
                BitmapData bitmapData = res.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
                int imglength = width * height;
                unsafe
                {
                    byte* bptr = (byte*)bitmapData.Scan0;
                    byte* r = ((byte*)hred.L);
                    byte* g = ((byte*)hgreen.L);
                    byte* b = ((byte*)hblue.L);
                    for (int i = 0; i < imglength; i++)
                    {
                        bptr[i * 4] = (b)[i];
                        bptr[i * 4 + 1] = (g)[i];
                        bptr[i * 4 + 2] = (r)[i];
                        bptr[i * 4 + 3] = 255;
                    }
                }

                res.UnlockBits(bitmapData); 
            }
            catch (Exception ex)
            {
                res = null;
                throw ex;
            }
        }
    }
}


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

少走弯路的少年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值