C#调用halcon做模板和匹配模板
(一)声明
HDevEngine HEngine = new HDevEngine();//声明一个Halcon引擎
HTuple ModelId;//声明一个接受模板的类型
HTuple ModelScore;//声明一个分数下面程序输出的判定
int _index = 0;//声明一个指针用来打开多个图片
string[] _fileNames;//声明一个数组来存取图片
Timer timer = new Timer();//声明一个定时器来定时播放图片
(二)设置
private void Form1_Load(object sender, EventArgs e)
{
//hWindowControl1.HalconWindow设置图像属性以及画笔之类的都在这设计
hWindowControl1.HalconWindow.SetDraw("margin");
hWindowControl1.HalconWindow.SetLineWidth(4);
hWindowControl1.HalconWindow.SetColor("pink");
HDevOpFixedWindowImpl ReciveWindow = new HDevOpFixedWindowImpl(hWindowControl1.HalconWindow);
HEngine.SetHDevOperators(ReciveWindow);
HEngine.SetProcedurePath(@"C:\Users\ASUS\Desktop\works\halcon\halcon");
CreateTimer();//创建一个定时器后面程序会有写明
}
(三)调用
private void button1_Click(object sender, EventArgs e)
{
string Route;//定义一个文件路径的string类型
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
try
{
Route = ofd.FileName;
HObject Image;//定义一个halcon的object类型
HOperatorSet.GenEmptyObj(out Image);//创建一个空对象元组。
Image.Dispose();//释放一下
HOperatorSet.ReadImage(out Image, Route);//读图
var externalFunction = new HDevProcedureCall(new HDevProcedure("dev_Createe"));//读取外部函数
externalFunction.SetInputIconicParamObject("Image", Image);//把Image赋给外部函数的输入“Image”中
externalFunction.Execute();//运行
ModelId = externalFunction.GetOutputCtrlParamTuple("ModelId");//获取外部函数输出的“ModelId”
}
catch { MessageBox.Show("有错误"); }
}
}
以上经过步骤三的操作目的就是得出ModelId ,为后面的匹配模板的输入有用处。
(四)通过定时器触发匹配模板
public void CreateTimer()
{
timer.Tick += new EventHandler(OnTimer);//定时器开启触发OnTimer事件
timer.Interval = 500;//定时器间隔时间
}
public void OnTimer(object sender, EventArgs e)
{
timer.Stop();
if (_index == 0)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
_fileNames = ofd.FileNames;
}
}
try
{
string fileName = _fileNames[_index];//多个文件用数组形式储存
HObject Image;
HOperatorSet.GenEmptyObj(out Image);
Image.Dispose();
HOperatorSet.ReadImage(out Image, fileName);
var externalFunction = new HDevProcedureCall(new HDevProcedure("dev_Find"));
externalFunction.SetInputIconicParamObject("Image", Image);//输入
externalFunction.SetInputCtrlParamTuple("ModelId", ModelId);//输入
externalFunction.Execute();//运行
ModelScore = externalFunction.GetOutputCtrlParamTuple("ModelScore");//输出
}
catch { MessageBox.Show("有错"); }
#region NG OK
if (ModelScore.Length > 0)//分数的长度
{
label1.Text = "OK";
label1.ForeColor = Color.Green;
}
else
{
label1.Text = "NG";
label1.ForeColor = Color.Red;
}
#endregion
_index++;
if (_index >= _fileNames.GetLength(0))
{
DialogResult dlgRes = MessageBox.Show("是否继续检测?", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dlgRes == DialogResult.No)
{
timer.Stop();
return;
}
_index = 0;
}
else
timer.Start();
}
(五)触发定时器
private void button2_Click(object sender, EventArgs e)
{
timer.Start();
}
(六)成果展示
处理上一篇文章的像是图像不在WPF内的问题得到解决,一个解决方式就是采用HDevOpFixedWindowImpl 设置
HDevOpFixedWindowImpl ReciveWindow = new HDevOpFixedWindowImpl(hWindowControl1.HalconWindow);
另一种方法就是在halcon里面编写
dev_close_window()
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
dev_display(Image)
这样就只要在C#里面的输入参数中输入一个windows窗口就行了