文章目录

  • 前言
  • 相关链接
  • 项目专栏
  • 运行环境
  • 匹配图片
  • WPF导出匹配模板
    • 如何了解Halcon和C#代码的对应关系
    • 逻辑分析:
    • 添加截取ROI功能
      • 基类
      • 矩形
      • 圆形
    • 生成导出模板
    • 运行结果:
    • 可能的报错
      • 你的文件路径不存在
      • 你选择的区域的内容有效信息过少

前言

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

//截取ROIhv_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{/// /// HS组件主题/// public HSmartWindowControlWPF WindowControlWPF { get; set; }/// /// 资源路径/// public string ResourcesPath { get => AppDomain.CurrentDomain.BaseDirectory + "Resources"; }/// /// 导出路径/// 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;}......其它代码/// /// 截取图片/// 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{/// /// 矩形封装/// 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; }/// /// 刷新回调/// public Action<HRectangleObj> RefleshCallBack { get; set; }/// /// 初始化构造函数/// /// /// /// /// /// 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);}/// /// 默认构造函数/// /// public HRectangleObj(HSmartWindowControlWPF windowControl,HImage hImage) : this(windowControl,hImage,100,200,300,400){}......其它代码/// /// 截取图片/// 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; }/// /// 刷新回调/// 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){}......其它代码/// /// 截取图片/// public override HObject Reduce(){HObject res = new HObject();HOperatorSet.GenCircle(out ROI, Row, Column,Radius);HOperatorSet.ReduceDomain(Image, ROI, out res);return res;}}}

生成导出模板

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

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