• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
搬运工_阿壮
博客园    首页    新随笔    联系   管理    订阅  订阅

Halcon 和 C# 联合编程 - 图像变量的相互转换(HObject、HImage、Bitmap)

/// <summary>
/// 灰度图像 HObject -> Bitmap
/// </summary>
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;
    }
}
/// <summary>
/// 灰度图像 HObject -> HImage1
/// </summary>
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;
}

/// <summary>
/// 彩色图像 HObject -> HImage3
/// </summary>
public HImage HObject2HImage3(HObject hObj)
{
    HImage image = new HImage();
    HTuple type, width, height, pointerRed, pointerGreen, pointerBlue;
    HOperatorSet.GetImagePointer3(hObj, out pointerRed, out pointerGreen, out pointerBlue, 
                                  out type, out width, out height);
    image.GenImage3(type, width, height, pointerRed, pointerGreen, pointerBlue);
    return image;
}
posted @ 2019-04-23 14:03  搬运工_阿壮  阅读(14780)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3