WPF+Halcon 培训项目实战(12):WPF导出匹配模板

2023-12-31 04:57:53

前言

为了更好地去学习WPF+Halcon,我决定去报个班学一下。原因无非是想换个工作。相关的教学视频来源于下方的Up主的提供的教程。这里只做笔记分享,想要源码或者教学视频可以和他联系一下。

相关链接

微软系列技术教程 WPF 年度公益课程

Halcon开发 CSDN博客专栏

个人学习的Gitee 项目地址仓库

项目专栏

WPF+Halcon实战项目

运行环境

  • .net core 8.0
  • visual studio 2022
  • halcon HDevelop 20.11
  • windows 11

匹配图片

在这里插入图片描述

WPF导出匹配模板

如何了解Halcon和C#代码的对应关系

我之前的博客上面说过,Halcon是可以进行C# 文档导出的功能的

在这里插入图片描述
如果我们对其添加注释,其导出的文件也有添加对应的注释的功能。
在这里插入图片描述

在这里插入图片描述
所以我们只需要将对应的代码超过了,进行对应的转化即可。

逻辑分析:

读取图片
截取ROI
生成模板
导出模板

添加截取ROI功能

我们之前的抽象封装,还缺少截取ROI的功能,这里我们将ROI截取功能添加上去。

截取ROI对应代码

    //截取ROI
    hv_Row1.Dispose();hv_Column1.Dispose();hv_Row2.Dispose();hv_Column2.Dispose();
    HOperatorSet.DrawRectangle1(hv_WindowHandle, out hv_Row1, out hv_Column1, out hv_Row2, 
        out hv_Column2);
    ho_Rectangle.Dispose();
    HOperatorSet.GenRectangle1(out ho_Rectangle, hv_Row1, hv_Column1, hv_Row2, hv_Column2);
    ho_ImageReduced.Dispose();
    HOperatorSet.ReduceDomain(ho_Image, ho_Rectangle, out ho_ImageReduced);

基类

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

namespace HalconExtension.Models
{
    public abstract class HObjBase
    {
        /// <summary>
        /// HS组件主题
        /// </summary>
        public HSmartWindowControlWPF WindowControlWPF { get; set; }
        /// <summary>
        /// 资源路径
        /// </summary>
        public string ResourcesPath { get => AppDomain.CurrentDomain.BaseDirectory + "Resources"; }

        /// <summary>
        /// 导出路径
        /// </summary>
        public string OutputPath
        {
            get
            {
                var path = AppDomain.CurrentDomain.BaseDirectory + "Output";
                //如果不存在就创建
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                return path;
            }
        }


        public HDrawingObject DrawingObject { get; set; }

        public HImage Image { get; set; }

        public HObject ROI = new HObject();

        public HObjBase(HSmartWindowControlWPF windowControlWPF, HImage image)
        {
            WindowControlWPF = windowControlWPF;
            Image = image;
        }

		......其它代码
        /// <summary>
        /// 截取图片
        /// </summary>
        public abstract HObject Reduce();
    }
}

矩形

using HalconDotNet;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HalconExtension.Models
{
    /// <summary>
    /// 矩形封装
    /// </summary>
    public class HRectangleObj : HObjBase
    {
        public HTuple HType { get; private set; }
        public HTuple Row1 { get; private set; }
        public HTuple Row2 { get; private set; }
        public HTuple Column1 { get; private set; }
        public HTuple Column2 { get; private set; }

        /// <summary>
        /// 刷新回调
        /// </summary>
        public Action<HRectangleObj> RefleshCallBack { get; set; }

        /// <summary>
        /// 初始化构造函数
        /// </summary>
        /// <param name="windowControlWPF"></param>
        /// <param name="row1"></param>
        /// <param name="column1"></param>
        /// <param name="row2"></param>
        /// <param name="column2"></param>
        public HRectangleObj(HSmartWindowControlWPF windowControlWPF,HImage hImage, HTuple row1, HTuple column1, HTuple row2, HTuple column2) : base(windowControlWPF, hImage)
        {
            //WindowControlWPF = windowControlWPF;
            Row1 = row1;
            Column1 = column1;
            Row2 = row2;
            Column2 = column2;
            DrawingObject = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.RECTANGLE1, Row1, Column1, Row2, Column2);

            //添加更新事件
            DrawingObject.OnDrag(Reflash);
            DrawingObject.OnResize(Reflash);
        }

        /// <summary>
        /// 默认构造函数
        /// </summary>
        /// <param name="windowControl"></param>
        public HRectangleObj(HSmartWindowControlWPF windowControl,HImage hImage) : this(windowControl,hImage,100,200,300,400)
        {


        }
		
		......其它代码
        /// <summary>
        /// 截取图片
        /// </summary>
        public override HObject Reduce()
        {
            HObject res = new HObject();
            HOperatorSet.GenRectangle1(out ROI, Row1, Column1, Row2, Column2);
            HOperatorSet.ReduceDomain(Image, ROI, out res);

            return res;
        }
    }
}

圆形

using HalconDotNet;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HalconExtension.Models
{
    public class HCircleObj : HObjBase
    {
        public HTuple Row { get; set; }

        public HTuple Column { get; set; }

        public HTuple Radius { get; set; }

        public HTuple HType { get; private set; }

        /// <summary>
        /// 刷新回调
        /// </summary>
        public Action<HCircleObj> RefleshCallBack { get; set; }

        public HCircleObj(HSmartWindowControlWPF windowControlWPF,HImage hImage, HTuple row, HTuple col, HTuple radius):base(windowControlWPF,hImage)
        {
            Row = row;
            Column = col;
            Radius = radius;

            DrawingObject = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.CIRCLE, Row, Column, Radius);

            //添加更新事件
            DrawingObject.OnDrag(Reflash);
            DrawingObject.OnResize(Reflash);
        }

        public HCircleObj(HSmartWindowControlWPF windowControlWPF,HImage hImage) : this(windowControlWPF,hImage,64,64,64)
        {
        }
		
		......其它代码
        /// <summary>
        /// 截取图片
        /// </summary>
        public override HObject Reduce()
        {
            HObject res = new HObject();
            HOperatorSet.GenCircle(out ROI, Row, Column,Radius);
            HOperatorSet.ReduceDomain(Image, ROI, out res);
            return res;
        }
    }
}

生成导出模板

        /// <summary>
        /// 生成匹配模板
        /// </summary>
        private void InitTemplate()
        {
            HTuple modelId = new HTuple();
            var ho_ImageReduced = RectangleObj.Reduce();
            HOperatorSet.CreateShapeModel(ho_ImageReduced, "auto", -0.39, 0.79, "auto", "auto",
    "use_polarity", "auto", "auto", out modelId);
            //导出匹配模板
            HOperatorSet.WriteShapeModel(modelId, RectangleObj.OutputPath+"/output.shm");
            MsgHelper.Success($"生成模板成功!路径为:{RectangleObj.OutputPath + "/output.shm"}");

        }

运行结果:

在这里插入图片描述

可能的报错

你的文件路径不存在

Halcon组件无法自动生成文件路径,如果保存的文件夹不存在会抛出异常
在这里插入图片描述

你选择的区域的内容有效信息过少

在这里插入图片描述

文章来源:https://blog.csdn.net/qq_44695769/article/details/135310849
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。