勇哥这套halcon引擎的学习笔记贴子共七篇,它是在官方指导文档《http://www.skcircle.com/?id=1343》的基础上学习编写而成的笔记。只是有一篇讲解怎么调用向量变量的没有加入,因为勇哥实在不知道这个向量变量有啥子用。以后如果搞明白了再加入吧。
2020/7/3勇哥注:
原来halcon中的向量就是个容器,跟c++标准模板库中的那个向量是一致的。第八篇加上来吧
halcon引擎学习笔记(七)在在HDevEngine/C#中使用实时编译器JIT
halcon引擎学习笔记(六)多线程并发执行外部函数,多窗口显示
halcon引擎学习笔记(二)执行Procedure程序,扩展名为hdvp的halcon函数
演示程序勇哥用的是halcon19.11,C#使用的是vs2013版本。
全部测试代码勇哥已经打包,请点击《下载》
如果你要调用的程序是一个hdev主程序,且当中没有外部函数或者自定义函数,则可以看(一)
如果你要调用的程序是一个hdev主程序,且当中有若干的外部函数或者自定义函数,则可以看(三)
如果你要调用的程序只是一个hdvp的外部函数,则可以看(二)
如果你需要多线程调用外部函数,则可以看(六)(七)
如果你的程序中用到向量变量,则可以看(八)
2020/10/15勇哥注:
勇哥最近寻遍halcon引擎类的功能,发现无法实现修改halcon程序并保存后,C#这边能实时运行修改后的halcon程序。
必须要退出C#程序后,再次执行才是跑的修改后的代码。
这真是个遗憾,因为机器正在做货时,重启C#程序是相当不方便的一件事。
如果有人知道怎么实现,麻烦告诉勇哥,非常感谢!
2020/12/18勇哥注:
由网友“小黄鱼”指出,HDevEngine 类有个UnloadProcedure方法调用后,可以实现不需要重启C#程序即可执行更新后的外部函数。
经我测试后,发现是有效的!
在这里非常感谢他的指点!!!
正文部分
========================
halcon引擎调用函数或者程序时,怎么由被调用的halcon程序返回出错信息给C#这边呢?
本篇就来谈谈这个话题。
先上演示程序源码,后面再说说运行的结果。
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
//演示程序by勇哥 www.skcircle.com
public partial class Form1 : Form
{
private HDevOpMultiWindowImpl MyHDevOperatorImpl;
private HDevProgramCall ProgramCall;
private HWindow Window;
string ProgramPathString = @"C:\Users\Public\Documents\MVTec\HALCON-19.11-Progress\examples\hdevengine\hdevelop\fin_detection1.hdev";
private HDevEngine MyEngine = new HDevEngine();
public Form1()
{
InitializeComponent();
}
private void DisplayException(HDevEngineException Ex)
{
string FullMessage = "Message: <" + Ex.Message + ">" + ", Error in program/procedure: <"
+ Ex.ProcedureName + ">" + ",program line:<" + Ex.LineText + ">" + ",line number: <" +
Ex.LineNumber + ">" + ",HALCON Error Number:<" + Ex.HalconError + ">";
string Title = "HDevEngine Exception (Category:" + Ex.Category.ToString() + ")";
MessageBox.Show(FullMessage, Title);
}
private void button1_Click(object sender, EventArgs e)
{
try
{
MyEngine.SetHDevOperators(MyHDevOperatorImpl);
var Program = new HDevProgram(ProgramPathString);
ProgramCall = new HDevProgramCall(Program);
ProgramCall.Execute();
MessageBox.Show("ok");
}
catch (HDevEngineException Ex)
{
DisplayException(Ex);
return;
}
catch
{
MessageBox.Show("其它错误");
return;
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
MyEngine.SetHDevOperators(MyHDevOperatorImpl);
var pro = new HDevProcedureCall(new HDevProcedure( "detect_fin_with_error_inpnotinit"));
HObject img=new HObject();
HOperatorSet.ReadImage(out img, "e:\\timg.jpg");
pro.SetInputIconicParamObject("Image", img);
pro.Execute();
}
catch (HDevEngineException Ex)
{
DisplayException(Ex);
return;
}
catch
{
MessageBox.Show("其它错误");
return;
}
}
private void Form1_Load(object sender, EventArgs e)
{
var halconExamples = HSystem.GetSystem("example_dir");
var ProcedurePath = halconExamples + @"\hdevengine\procedures";
MyEngine.SetProcedurePath(ProcedurePath);
Window = hSmartWindowControl1.HalconWindow;
Window.SetDraw("margin");
Window.SetLineWidth(4);
MyHDevOperatorImpl = new HDevOpMultiWindowImpl(Window);
}
private void button3_Click(object sender, EventArgs e)
{
try
{
MyEngine.SetHDevOperators(MyHDevOperatorImpl);
var pro = new HDevProcedureCall(new HDevProcedure("detect_fin_with_error_call"));
HObject img = new HObject();
HOperatorSet.ReadImage(out img, "e:\\timg.jpg");
pro.SetInputIconicParamObject("Image", img);
pro.Execute();
}
catch (HDevEngineException Ex)
{
DisplayException(Ex);
return;
}
catch
{
MessageBox.Show("其它错误");
return;
}
}
}
}载入Load fin_detection.hdev时出错。
这是因为勇哥故意把调用的程序名改为fin_detection1
这个错误是载入不存在的hdev程序。

运行detect_fin_with_error_inpnotinit.hdvp后报错。
这个错误是halcon函数输入参数没有初始化。
请看后面的detect_fin_with_error_inpnotinit源码,在第一行故意写了一个不存在的image名字。

运行detect_fin_with_error_call.hdvp后报错。
这个错误是参数无效。
你可以看看后面列出来的detect_fin_with_error_call.hdvp的源码,其第5行算子closing_circle的第3个参数半径取了-1
这个与报错信息是一致的。

detect_fin_with_error_inpnotinit.hdev的执行结果:

程序源码如下:
* Detects fins using a procedure.
init_acquisition (AcqHandle, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width / 2, Height / 2, 'black', WinID)
dev_set_part (0, 0, Height - 1, Width - 1)
dev_set_draw ('margin')
dev_set_line_width (4)
grab_image (Image, AcqHandle)
dev_display (Image)
detect_fin (Image, FinRegion, FinArea)
dev_display (Image)
dev_set_color ('red')
dev_display (FinRegion)
dev_set_color ('white')
* set_tposition (WinID, 150, 20)
* write_string (WinID, 'Fin Area: ' + FinArea)
display_zoomed_region (Image, FinRegion, 2, 5)
close_framegrabber (AcqHandle)
dev_close_window ()detect_fin_with_error_inpnotinit.hdvp的参数

源码如下,其中故意让输入参数Image非法。

detect_fin_with_error_call.hdvp 参数如下:

源码如下:
binary_threshold (Image, Dark, 'max_separability', 'dark', UsedThreshold)
difference (Image, Dark, Background)
dev_set_color ('blue')
dev_display (Background)
closing_circle (Background, ClosedBackground, -1)
dev_set_color ('green')
dev_display (ClosedBackground)
difference (ClosedBackground, Background, RegionDifference)
opening_rectangle1 (RegionDifference, FinRegion, 5, 5)
area_center (FinRegion, FinArea, Row, Column)
return ()---------------------
作者:hackpig
来源:www.skcircle.com
版权声明:本文为博主原创文章,转载请附上博文链接!


少有人走的路



















