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

文章目录

前言

为了更好地去学习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对应代码

c 复制代码
    //截取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);

基类

csharp 复制代码
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();
    }
}

矩形

csharp 复制代码
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;
        }
    }
}

圆形

csharp 复制代码
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;
        }
    }
}

生成导出模板

csharp 复制代码
        /// <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组件无法自动生成文件路径,如果保存的文件夹不存在会抛出异常

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

相关推荐
林子漾17 小时前
【paper】分布式无人水下航行器围捕智能目标
分布式·wpf
wyh要好好学习1 天前
C# WPF 记录DataGrid的表头顺序,下次打开界面时应用到表格中
开发语言·c#·wpf
lgcgkCQ2 天前
任务调度中心-XXL-JOB使用详解
java·wpf·定时任务·任务调度
Vicky&James2 天前
英雄联盟客户端项目:从跨平台Uno Platform到Win UI3的转换只需要30分钟
github·wpf·跨平台·英雄联盟·winui·unoplatform
就是有点傻2 天前
WPF中如何使用区域导航
wpf
她说彩礼65万2 天前
WPF程序设置单例启动(互斥体)
wpf
就是有点傻2 天前
WPF中Prism框架中 IContainerExtension 和 IRegionManager的作用
wpf
月落.2 天前
WPF中MVVM工具包 CommunityToolkit.Mvvm
wpf·mvvm
月落.2 天前
WPF Prism框架
wpf·prism
Crazy Struggle3 天前
.NET 8.0 通用管理平台,支持模块化、WinForms 和 WPF
vue·wpf·winform·.net 8.0·通用权限管理