C#与halcon联合(5)模型匹配,单步查找,遍历查找

1.什么是模型?

在这里插入图片描述
以抗体和病毒举例,A抗体作为人的防御系统机制对来物进行把关。
只要A抗体发现来物是A病毒,OK就处理掉;不是A病毒就NG不处理。

那么为什么病毒能够被发现呢?
是因为抗体上面有个特征,这个特征和病毒上面的特征一样。
一接触,特征比对,特征一致就OK处理,不一样就NG不处理。
这个特征就是我们所谓的模型。

模型匹配就是一个过程,A抗体对来物的一个把关过程。
A抗体在来物中找到A病毒就OK,OK就是模型匹配成功的结果
A抗体在来物中找不到A病毒就NG,NG就是模型匹配失败的结果

2.halcon模型匹配代码生成

打开halcon,选择助手中的Matching工具
在这里插入图片描述
第一步加载图片,第二步绘画ROI区域

设置的模型为骰子中的3个点。
在这里插入图片描述
打开应用,第一步加载所有样品图片
第二步检测所有图片中匹配到的模型
检测结果都是每张图片找到了一个能够匹配的模型
在这里插入图片描述
样品图片如下
在这里插入图片描述
在halcon中以上操作代码生成,如下操作
在这里插入图片描述
halcon中生成的代码:

* Matching 02: BEGIN of generated code for model initialization
set_system ('border_shape_models', 'false')
* Matching 02: Obtain the model image
read_image (Image, 'C:/xxx/xxx/Desktop/test.jpg')
* Matching 02: build the ROI from basic regions
gen_rectangle1 (ModelRegion, 73.4873, 323.223, 161.523, 415.268)
* Matching 02: reduce the model template
reduce_domain (Image, ModelRegion, TemplateImage)
* Matching 02: create the shape model
create_shape_model (TemplateImage, 4, rad(0), rad(360), rad(2.075), ['none','no_pregeneration'], 'use_polarity', [19,25,4], 4, ModelId)
* Matching 02: get the model contour for transforming it later into the image
get_shape_model_contours (ModelContours, ModelId, 1)
* Matching 02: END of generated code for model initialization
* Matching 02: BEGIN of generated code for model application
* Matching 02: the following operations are usually moved into
* Matching 02: that loop where the aquired images are processed
* Matching 02: Find the model
find_shape_model (Image, ModelId, rad(0), rad(360), 0.8, 4, 0.5, 'least_squares', [4,1], 0.75, ModelRow, ModelColumn, ModelAngle, ModelScore)
* Matching 02: transform the model contours into the detected positions
for MatchingObjIdx := 0 to |ModelScore| - 1 by 1
    hom_mat2d_identity (HomMat)
    hom_mat2d_rotate (HomMat, ModelAngle[MatchingObjIdx], 0, 0, HomMat)
    hom_mat2d_translate (HomMat, ModelRow[MatchingObjIdx], ModelColumn[MatchingObjIdx], HomMat)
    affine_trans_contour_xld (ModelContours, TransContours, HomMat)
    dev_display (TransContours)
endfor
* Matching 02: Clear model when done
clear_shape_model (ModelId)
* Matching 02: END of generated code for model application

3.将halcon的代码导出

步骤轻车熟路了,还不会的看我前面的课程
转化有用的代码部分如下所示:

  private void action()
  {


    // Local iconic variables 

    HObject ho_Image, ho_ModelRegion, ho_TemplateImage;
    HObject ho_ModelContours, ho_TransContours=null;

    // Local control variables 

    HTuple hv_ModelId = null, hv_ModelRow = null;
    HTuple hv_ModelColumn = null, hv_ModelAngle = null, hv_ModelScore = null;
    HTuple hv_MatchingObjIdx = null, hv_HomMat = new HTuple();
    // Initialize local and output iconic variables 
    HOperatorSet.GenEmptyObj(out ho_Image);
    HOperatorSet.GenEmptyObj(out ho_ModelRegion);
    HOperatorSet.GenEmptyObj(out ho_TemplateImage);
    HOperatorSet.GenEmptyObj(out ho_ModelContours);
    HOperatorSet.GenEmptyObj(out ho_TransContours);
    //Matching 02: BEGIN of generated code for model initialization
    HOperatorSet.SetSystem("border_shape_models", "false");
    //Matching 02: Obtain the model image
    ho_Image.Dispose();
    HOperatorSet.ReadImage(out ho_Image, "C:/Users/Administrator/Desktop/test.jpg");
    //Matching 02: build the ROI from basic regions
    ho_ModelRegion.Dispose();
    HOperatorSet.GenRectangle1(out ho_ModelRegion, 73.4873, 323.223, 161.523, 415.268);
    //Matching 02: reduce the model template
    ho_TemplateImage.Dispose();
    HOperatorSet.ReduceDomain(ho_Image, ho_ModelRegion, out ho_TemplateImage);
    //Matching 02: create the shape model
    HOperatorSet.CreateShapeModel(ho_TemplateImage, 4, (new HTuple(0)).TupleRad()
        , (new HTuple(360)).TupleRad(), (new HTuple(2.075)).TupleRad(), (new HTuple("none")).TupleConcat(
        "no_pregeneration"), "use_polarity", ((new HTuple(19)).TupleConcat(25)).TupleConcat(
        4), 4, out hv_ModelId);
    //Matching 02: get the model contour for transforming it later into the image
    ho_ModelContours.Dispose();
    HOperatorSet.GetShapeModelContours(out ho_ModelContours, hv_ModelId, 1);
    //Matching 02: END of generated code for model initialization
    //Matching 02: BEGIN of generated code for model application
    //Matching 02: the following operations are usually moved into
    //Matching 02: that loop where the aquired images are processed
    //Matching 02: Find the model
    HOperatorSet.FindShapeModel(ho_Image, hv_ModelId, (new HTuple(0)).TupleRad(), 
        (new HTuple(360)).TupleRad(), 0.8, 4, 0.5, "least_squares", (new HTuple(4)).TupleConcat(
        1), 0.75, out hv_ModelRow, out hv_ModelColumn, out hv_ModelAngle, out hv_ModelScore);
    //Matching 02: transform the model contours into the detected positions
    for (hv_MatchingObjIdx=0; (int)hv_MatchingObjIdx<=(int)((new HTuple(hv_ModelScore.TupleLength()
        ))-1); hv_MatchingObjIdx = (int)hv_MatchingObjIdx + 1)
    {
      HOperatorSet.HomMat2dIdentity(out hv_HomMat);
      HOperatorSet.HomMat2dRotate(hv_HomMat, hv_ModelAngle.TupleSelect(hv_MatchingObjIdx), 
          0, 0, out hv_HomMat);
      HOperatorSet.HomMat2dTranslate(hv_HomMat, hv_ModelRow.TupleSelect(hv_MatchingObjIdx), 
          hv_ModelColumn.TupleSelect(hv_MatchingObjIdx), out hv_HomMat);
      ho_TransContours.Dispose();
      HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_TransContours, 
          hv_HomMat);
      if (HDevWindowStack.IsOpen())
      {
        HOperatorSet.DispObj(ho_TransContours, HDevWindowStack.GetActive());
      }
    }
    //Matching 02: Clear model when done
    HOperatorSet.ClearShapeModel(hv_ModelId);
    //Matching 02: END of generated code for model application
    ho_Image.Dispose();
    ho_ModelRegion.Dispose();
    ho_TemplateImage.Dispose();
    ho_ModelContours.Dispose();
    ho_TransContours.Dispose();

  }

4.制作winform操作Demo

实现功能:
①设置基准模型,并且保存,关闭程序再次开启可读取
②单步匹配,遍历匹配
在这里插入图片描述
启动程序,加载之前保存好的模型
我们读取模型图片
在这里插入图片描述
第一步点击模型按键
第二步绘画ROI区域,找到模型
第三部,确定就保存模型,关闭再次开启可加载
在这里插入图片描述
模型保存成功
在这里插入图片描述
加载样品,遍历查找,或者单步查找模型都可以
在这里插入图片描述

5.代码链接和展示视频链接

代码:https://download.csdn.net/download/adsd1233123/21108855
视频:
https://www.bilibili.com/video/BV1Yy4y1V72v/

观看第六节:制作增删查改直线工具框架,追踪基准模型,制作直线关联框架,点位关联框架,后续加入直线拟合关联框架
第六节开始制作一些基本的框架,学好前面的课程理解后续的内容很是方便。

  • 4
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱搞事的程小猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值