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

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

相关推荐
赵财猫._.40 分钟前
HarmonyOS渲染性能优化:组件树复用与局部刷新机制
wpf·harmonyos·ux
赵财猫._.1 小时前
鸿蒙分布式数据库同步:冲突解决与数据一致性策略
wpf·harmonyos·ux
Macbethad11 小时前
使用WPF编写一个数据记录页面
wpf
dotent·3 天前
C#基于WPF UI框架的通用基础上位机测试WPF框架
ui·c#·wpf
咩图4 天前
WPF+Prism8.0.0.1909+C#创建一个桌面程序
c#·wpf·prism
雁于飞4 天前
分布式基础
java·spring boot·分布式·spring·wpf·cloud native
oioihoii4 天前
WPF入门指南:解析默认项目结构
wpf
极客智造4 天前
深入解析 ReactiveUI:WPF 响应式 MVVM 开发的 “终极方案”
wpf
Macbethad6 天前
使用WPF编写一个多维度伺服系统的程序
大数据·hadoop·wpf
lingxiao168886 天前
WPF Prism框架应用
c#·wpf·prism