看过我上一篇博客的小伙伴应该知道
halcon教程九
我把halcon里的程序导出来放进了C#程序里
当然 那是控制台程序 那么现在
我们将它可视化 先看效果
单机识别回形针
OK 这个项目我会作为一个开源项目和大家一起学习
文章末尾会放上地址
我只实现了回形针识别,接下来的糖豆识别等等
大家可以完成 或者让这个程序更加美观
当我们想变成WPF程序的时候
无非是把原先的程序进行一个封装
我在halcon生成的类里面做了一些操作
便于我们拿到它解析出来的数据
然后就就是进行数据展示了
namespace HalconPlay.Model
{
public class ClipData
{
public string ImageHeight { get; set; }
public string ImageWidth { get; set; }
public string ClipAreas { get; set; }
public string ClipXs { get; set; }
public string ClipYs { get; set; }
public string ClipCount { get; set; }
}
}
这是一个数据模型,我们展示的就是这些信息
//
// File generated by HDevelop for HALCON/.NET (C#) Version 19.05.0.0
// Non-ASCII strings in this file are encoded in local-8-bit encoding (cp936).
//
// Please note that non-ASCII characters in string constants are exported
// as octal codes in order to guarantee that the strings are correctly
// created on all systems, independent on any compiler settings.
//
// Source files with different encoding should not be mixed in one project.
//
// This file is intended to be used with the HDevelopTemplate or
// HDevelopTemplateWPF projects located under %HALCONEXAMPLES%\c#
using System;
using System.IO;
using HalconDotNet;
using HalconPlay.Model;
public partial class HDevelopExport
{
public ClipData clipData { get; set; }
public HTuple hv_ExpDefaultWinHandle;
public string Path { get; set; }
public HDevelopExport()
{
}
public HDevelopExport(string Path)
{
this.Path = Path;
}
// Main procedure
public void action()
{
// Local iconic variables
HObject ho_Clip1, ho_Clip, ho_Dark, ho_Single;
HObject ho_Selected;
// Local control variables
HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
HTuple hv_Phi = new HTuple(), hv_Area = new HTuple(), hv_Row = new HTuple();
HTuple hv_Column = new HTuple(), hv_Number = new HTuple();
// Initialize local and output iconic variables
HOperatorSet.GenEmptyObj(out ho_Clip1);
HOperatorSet.GenEmptyObj(out ho_Clip);
HOperatorSet.GenEmptyObj(out ho_Dark);
HOperatorSet.GenEmptyObj(out ho_Single);
HOperatorSet.GenEmptyObj(out ho_Selected);
ho_Clip1.Dispose();
HOperatorSet.ReadImage(out ho_Clip1, Path);
ho_Clip.Dispose();
HOperatorSet.Rgb1ToGray(ho_Clip1, out ho_Clip);
hv_Width.Dispose();
hv_Height.Dispose();
HOperatorSet.GetImageSize(ho_Clip, out hv_Width, out hv_Height);
clipData.ImageWidth= hv_Width.ToString();
clipData.ImageHeight= hv_Height.ToString();
ho_Dark.Dispose();
HOperatorSet.Threshold(ho_Clip, out ho_Dark, 128, 255);
ho_Single.Dispose();
HOperatorSet.Connection(ho_Dark, out ho_Single);
ho_Selected.Dispose();
HOperatorSet.SelectShape(ho_Single, out ho_Selected, "area", "and", 5000, 10000);
hv_Phi.Dispose();
HOperatorSet.OrientationRegion(ho_Selected, out hv_Phi);
hv_Area.Dispose();hv_Row.Dispose();hv_Column.Dispose();
HOperatorSet.AreaCenter(ho_Selected, out hv_Area, out hv_Row, out hv_Column);
clipData.ClipAreas = hv_Area.ToString();
clipData.ClipYs= hv_Row.ToString();
clipData.ClipXs = hv_Column.ToString();
hv_Number.Dispose();
HOperatorSet.CountObj(ho_Selected, out hv_Number);
clipData.ClipCount = hv_Number.ToString();
ho_Clip1.Dispose();
ho_Clip.Dispose();
ho_Dark.Dispose();
ho_Single.Dispose();
ho_Selected.Dispose();
hv_Width.Dispose();
hv_Height.Dispose();
hv_Phi.Dispose();
hv_Area.Dispose();
hv_Row.Dispose();
hv_Column.Dispose();
hv_Number.Dispose();
}
public void InitHalcon()
{
// Default settings used in HDevelop
HOperatorSet.SetSystem("width", 512);
HOperatorSet.SetSystem("height", 512);
}
public ClipData RunHalcon(HTuple Window)
{
clipData = new ClipData();
hv_ExpDefaultWinHandle = Window;
action();
if (clipData!=null) {
return clipData;
}
return null;
}
}
这是Halcon 生成程序的更改
有兴趣的小伙伴可以看看这个链接哦
git地址
如果拉去项目后 运行时出现异常 那么
把你的CPU根据你的电脑安装的环境进行更改
x64 或 x86 大部分是x64 目前
ok 改完后正常