WPF + Halcon 开发的机器视觉算法平台

前言

随着工业自动化和智能制造的快速发展,机器视觉技术在生产制造、质量控制、物流管理等多个领域中扮演着越来越重要的角色。

本文将介绍一个基于WPF开发的机器视觉算法平台,集成了多种先进的图像处理和识别算法,为大家提供一个功能强大且易于使用的工具。

项目介绍

基于 WPF 开发的机器视觉算法平台,主要使用 HALCON 作为核心图像处理库。

HALCON 是一款广泛应用于工业自动化领域的机器视觉软件,以其强大的图像处理能力和丰富的算法库著称。该项目不仅充分利用 HALCON 的优势,还结合 WPF 强大的界面设计能力,提供一个用户友好且功能全面的开发环境。

该项目的主要目标是帮助开发者快速构建和部署机器视觉应用,减少开发时间和成本。通过集成多种图像处理和识别算法,如形状匹配、相似性匹配、形变匹配、条码识别、二维码识别以及卡尺查找圆识别等功能,该平台能够应对各种复杂的视觉任务。

项目功能

1、内置图像编辑组件

平台提供强大的图像编辑功能,用户可以轻松地对输入图像进行裁剪、旋转、缩放等操作。

2、多种图像目标匹配算法

  • 形状匹配:用于精确匹配图像中的特定形状。

  • 相似性匹配:根据图像特征进行相似性匹配。

  • 形变匹配:适用于处理图像中的变形情况。

3、条码识别与二维码识别

支持多种格式的条码和二维码识别,确保数据读取的准确性。

4、卡尺查找圆识别

通过卡尺工具实现高精度的圆形物体检测。

5、系统多语言支持

平台支持多种语言,方便不同地区的用户使用。

6、系统主题自定义

可以根据个人喜好自定义系统的外观主题,提升用户体验。

项目特点

1、高性能图像处理:利用 HALCON 强大的图像处理能力,确保高效的图像分析和识别。

2、易用性:通过 WPF 提供的现代化界面设计,使得平台操作简单直观。

3、灵活性:多种图像处理算法的选择,适应不同的应用场景。

4、多语言支持:支持多种语言,便于国际化部署。

5、高度可定制化:用户可以根据需求自定义系统主题和功能模块。

项目代码

CircleMeasureService 主要完成以下功能:

  • 在图像中创建一个 测量模型(metrology model)

  • 添加一个圆形或圆弧作为测量对象

  • 对输入图像进行灰度化处理

  • 执行测量并获取结果(包括圆心坐标和半径)

  • 在图像窗口中绘制出检测到的圆轮廓及关键参数信息(如位置、半径)

/// <summary>
/// 查找圆服务
/// </summary>
publicclassCircleMeasureService : BindableBase
{
public CircleMeasureService()
{
Info = new MethodInfo()
{
Name = "find_circle",
Description = "Add a circle or a circular arc to a metrology model.",
Parameters = new List<MathodParameter>()
{
new MathodParameter(){ Name="MetrologyHandle", Description="Handle of the metrology model." },
new MathodParameter(){ Name="Row ", Description="Row coordinate (or Y) of the center of the circle or circular arc." },
new MathodParameter(){ Name="Column ", Description="Column (or X) coordinate of the center of the circle or circular arc." },
new MathodParameter(){ Name="Radius", Description="Radius of the circle or circular arc." },
new MathodParameter(){ Name="MeasureLength1 ", Description="Half length of the measure regions perpendicular to the boundary." },
new MathodParameter(){ Name="MeasureLength2", Description="Half length of the measure regions tangetial to the boundary." },
new MathodParameter(){ Name="MeasureSigma ", Description="Sigma of the Gaussian function for the smoothing." },
new MathodParameter(){ Name="MeasureThreshold", Description="Minimum edge amplitude." },
new MathodParameter(){ Name="GenParamName ", Description="Names of the generic parameters." },
new MathodParameter(){ Name="GenParamValue ", Description="Values of the generic parameters." },
new MathodParameter(){ Name="Index ", Description="Index of the created metrology object." },
},
Predecessors = new List<string>()
{
"align_metrology_model",
"apply_metrology_model",
}
};
RunParameter = new MeasureRunParameter();
RunParameter.ApplyDefaultParameter();
}

private HWindow hWindow;

public HWindow HWindow
{
get { return hWindow; }
set { hWindow = value; RaisePropertyChanged(); }
}

public MethodInfo Info { get; set; }

private MeasureRunParameter runParameter;

/// <summary>
/// 查找圆运行参数
/// </summary>
public MeasureRunParameter RunParameter
{
get { return runParameter; }
set { runParameter = value; RaisePropertyChanged(); }
}

HObject ho_Contour, ho_Contours;
HTuple hv_MetrologyHandle = new HTuple(), hv_Index = new HTuple(), hv_Row = new HTuple();
HTuple hv_Parameter = new HTuple();
HTuple hv_Row1 = new HTuple(), hv_Column1 = new HTuple();

public void Run(HObject Image)
{
if (Image == ) return;

if (RunParameter.Radius == 0) return;
if (RunParameter.Row == 0 || RunParameter.Column == 0) return;

var ho_GrayImage = Image.Rgb1ToGray();

hv_MetrologyHandle.Dispose();
HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle);

hv_Index.Dispose();
HOperatorSet.AddMetrologyObjectCircleMeasure(hv_MetrologyHandle,
RunParameter.Row,
RunParameter.Column,
RunParameter.Radius,
RunParameter.MeasureLength1,
RunParameter.MeasureLength2,
RunParameter.MeasureSigma,
RunParameter.MeasureThreshold,
new HTuple(), new HTuple(), out hv_Index);

HOperatorSet.GetMetrologyObjectMeasures(out ho_Contours, hv_MetrologyHandle, "all", "all", out hv_Row1, out hv_Column1);
HOperatorSet.ApplyMetrologyModel(ho_GrayImage, hv_MetrologyHandle);
//hv_Parameter.Dispose();
HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, 0, "all", "result_type", "all_param", out hv_Parameter);
//ho_Contour.Dispose();
HOperatorSet.GetMetrologyObjectResultContour(out ho_Contour, hv_MetrologyHandle, 0, "all", 1.5);

if (HWindow != )
{
HOperatorSet.SetColor(HWindow, "red");
HWindow.DispObj(ho_Contours);
HOperatorSet.SetColor(HWindow, "blue");
HWindow.DispObj(ho_Contour);

HWindow.SetString($"查找圆坐标:({hv_Parameter.DArr[0]},{hv_Parameter.DArr[1]}),半径:{hv_Parameter.DArr[2]}", "image", 10, 10, "black", "true");
HOperatorSet.DispCross(HWindow, hv_Parameter.DArr[0], hv_Parameter.DArr[1], 50, 0);
}
}
}

项目效果

平台提供的多种图像处理和识别算法,能够有效解决复杂的视觉问题。

例如,在生产线上的质量检测环节,平台可以通过条码识别和二维码识别功能,快速准确地获取产品信息;通过形状匹配和形变匹配算法,实现对产品的高精度检测。

另外,平台的多语言支持和自定义主题功能,提升了用户的使用体验,使其更符合不同地区和行业的需求。

系统首页

系统设置

系统功能

模版匹配

比较测量

项目源码

GitHub:https://github.com/HenJigg/MachineVision

总结

功能强大且灵活的机器视觉开发平台,它结合了 WPF 和 HALCON 的优势,为大家提供了一个高效、易用的开发环境。不管是工业自动化中的质量检测,还是物流管理中的条码识别,该平台都能提供有效的解决方案。通过多语言支持和高度可定制化的特性,平台能够更好地满足不同用户的需求。

原文链接:,转发请注明来源!