【halcon 实现模板匹配,定位,找线,找点,找圆】

这篇具有很好参考价值的文章主要介绍了【halcon 实现模板匹配,定位,找线,找点,找圆】。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


c#联合halcon
链接: https://www.bilibili.com/video/BV1aR4y1473b/.

halcon函数速查
链接: https://pan.baidu.com/s/1rbxf62bMh61Ie0pB4t3AHw .
提取码:0000
图片链接
链接: https://pan.baidu.com/s/1f0ld6nHcqzblvTnekEoSiQ .
0000

读取图片

【halcon 实现模板匹配,定位,找线,找点,找圆】

读取

read_image (Image, 'D:/Data_image/a/m3_1.jpg')
dev_close_window ()
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)

选择模板

*创建带角度矩形
gen_rectangle2 (ROI_0, 310.125, 807.625, rad(-76.7595), 87.3212, 72.8268)

将模板从原图中截取出来

reduce_domain (Image, ROI_0, ImageReduced)

模板匹配

准备形状模型

*  准备一个形状模型来匹配
create_shape_model (ImageReduced, 'auto',0, rad(360), 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID)
get_shape_model_contours (ModelContours, ModelID, 1)

然后就可以开始找目标定位了

模板匹配

 read_image (Image, 'D:/Data_image/a1/'+Indexs+'.jpg')
 rgb1_to_gray (Image, GrayImage)
 get_image_size (GrayImage, Width, Height)
 *计时
 *count_seconds (S1)
 *                                  起始角,角范围,最小得分  匹配个数   最大重叠  亚像素       最大金字塔数    贪婪 0安全慢-1
 find_shape_model (GrayImage, ModelID, 0, rad(360), 0.7,       1,      0,     'least_squares',   2,   0.9,                Row,  Column,    Angle,   Score)

得到 x, y, angle, 分数

根据x,y,angle得到以x,y为中心的空间

dev_set_color ('green')
hom_mat2d_identity (HomMat2DIdentity)
* 齐次2d变换平移
hom_mat2d_translate (HomMat2DIdentity, Row, Column, HomMat2DTranslate)
* 齐次2d变换旋转
hom_mat2d_rotate (HomMat2DTranslate, Angle, Row, Column, HomMat2DRotate)
affine_trans_contour_xld (ModelContours, ShapeModelTrans, HomMat2DRotate)

定位某块区域

通过在新空间上离原点的固定距离(100,-95)得到在原图的像素位置________ RowTrans, ColTrans

affine_trans_pixel (HomMat2DRotate, 100, -95, RowTrans, ColTrans)
gen_rectangle2 (Rectangle1Check, RowTrans, ColTrans, Angle-1.43, 180, 20)

【halcon 实现模板匹配,定位,找线,找点,找圆】

定位找线测量卡尺位置

同样的获取模板空间得到的 固定线段端点 在 原图上的位置
——
黑到白_____ positive
白到黑_____ negative

affine_trans_pixel (HomMat2DRotate, -115, -450, RowTrans1_1, ColTrans1_1)
affine_trans_pixel (HomMat2DRotate, -145, -200,RowTrans1_2, ColTrans1_2)
gen_region_line (Line1, RowTrans1_1, ColTrans1_1, RowTrans1_2, ColTrans1_2)
* 创建测量句柄
create_metrology_model (MetrologyHandle1)
* 设置计量模型图像大小
set_metrology_model_image_size (MetrologyHandle1, Width, Height)
LineParams1 := [ RowTrans1_1, ColTrans1_1, RowTrans1_2, ColTrans1_2]
*                                                                   卡尺高   卡尺宽   sigma   阈值
add_metrology_object_generic (MetrologyHandle1, 'line', LineParams1,   15,   3,        1,      20, \
                          'measure_transition', 'positive', Index)
* 执行测量,获取边缘点集
apply_metrology_model (GrayImage, MetrologyHandle1)
get_metrology_object_measures (Contours1, MetrologyHandle1, 'all', 'all', Row1, Column1)

*获取找到的点位
gen_cross_contour_xld (Cross1, Row1, Column1, 6, 0.785398)
* 获取最终测量数据和轮廓线
*获取结果
get_metrology_object_result (MetrologyHandle1, 'all', 'all', 'result_type', 'all_param', Parameter)
*划线
get_metrology_object_result_contour (ContourOut1, MetrologyHandle1, 'all', 'all', 1.5)
*将轮廓拟合成线
fit_line_contour_xld (ContourOut1, 'tukey', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)
* 释放测量句柄
 clear_metrology_object (MetrologyHandle1, 'all')

【halcon 实现模板匹配,定位,找线,找点,找圆】

第二根线同理

affine_trans_pixel (HomMat2DRotate, 30, -430, RowTrans2_1, ColTrans2_1)
affine_trans_pixel (HomMat2DRotate, 200, -405,RowTrans2_2, ColTrans2_2)
gen_region_line (Line2, RowTrans2_1, ColTrans2_1, RowTrans2_2, ColTrans2_2)
* 创建测量句柄
create_metrology_model (MetrologyHandle2)
* 添加测量对象
set_metrology_model_image_size (MetrologyHandle2, Width, Height)

LineParams2 := [ RowTrans2_1, ColTrans2_1, RowTrans2_2, ColTrans2_2]
   *                                                               卡尺高   卡尺宽   sigma   阈值
add_metrology_object_generic (MetrologyHandle2, 'line', LineParams2,   20,   3,        1,      20, \
                             'measure_transition', 'negative', Index)
* 执行测量,获取边缘点集
apply_metrology_model (GrayImage, MetrologyHandle2)
get_metrology_object_measures (Contours2, MetrologyHandle2, 'all', 'all', Row2, Column2)

*获取找到的点位
gen_cross_contour_xld (Cross2, Row2, Column2, 6, 0.785398)
* 获取最终测量数据和轮廓线
*获取结果
get_metrology_object_result (MetrologyHandle2, 'all', 'all', 'result_type', 'all_param', Parameter)
*划线
get_metrology_object_result_contour (ContourOut2, MetrologyHandle2, 'all', 'all', 1.5)
*将轮廓拟合成线
fit_line_contour_xld (ContourOut2, 'tukey', -1, 0, 5, 2, RowBegin1, ColBegin1, RowEnd1, ColEnd1, Nr1, Nc1, Dist1)
* 释放测量句柄
clear_metrology_object (MetrologyHandle2, 'all')

【halcon 实现模板匹配,定位,找线,找点,找圆】

定位找圆测量卡尺位置

圆也一样

 affine_trans_pixel (HomMat2DRotate, 20, -560, RowTrans1, ColTrans1)
 gen_circle (circle1, RowTrans1, ColTrans1, 50)
 LineParams3 := [RowTrans1, ColTrans1, 50]
 create_metrology_model (MetrologyHandle5)
 set_metrology_model_image_size (MetrologyHandle5, Width, Height)
 add_metrology_object_generic (MetrologyHandle5, 'circle', LineParams3, 16, 5, 1, 25, [], [], Index1)
 **                                                                          'negative'    'positive'
 set_metrology_object_param (MetrologyHandle5, 'all', 'measure_transition', 'positive')
 set_metrology_object_param (MetrologyHandle5, 'all', 'num_measures',20)
 apply_metrology_model (GrayImage, MetrologyHandle5)
 get_metrology_object_measures (Contours5, MetrologyHandle5, 'all', 'all', Row5, Column5)
 
 tuple_length (Row5, Length2)
 if (Length2>1)
     gen_cross_contour_xld (Cross6, Row5, Column5, 6, Angle)
     gen_contour_polygon_xld (Contour5, Row5, Column5)
     fit_circle_contour_xld (Contour5, 'algebraic', -1, 0, 0, 3, 2, Row6, Column6, Radius, StartPhi, EndPhi, PointOrder)
     *创建圆或圆弧的XLD轮廓。                                                  圆弧方向
     gen_circle_contour_xld (ContCircle, Row6, Column6, Radius, rad(0), rad(360), 'positive', 1)
 endif
 * 释放测量句柄
  clear_metrology_object (MetrologyHandle5, 'all')
        

【halcon 实现模板匹配,定位,找线,找点,找圆】

求线段交点

*求线段交点
tuple_length (RowBegin, Length)
tuple_length (RowBegin2, Length1)
if (Length==1 and Length1==1)
    *两线段交点
    intersection_lines ( RowBegin, ColBegin, RowEnd, ColEnd,\
                    RowBegin1, ColBegin1, RowEnd1, ColEnd1,\
                    Row4, Column4, IsOverlapping) 
    gen_cross_contour_xld (Cross4, Row4, Column4, 6, Angle)
    
    *点到线的投影(距离)
    projection_pl (Row4, Column4, RowBegin2, ColBegin2, RowEnd2, ColEnd2, RowProj, ColProj)
    gen_cross_contour_xld (Cross5, RowProj, ColProj, 6, Angle)
    distance_pp (Row4, Column4, RowProj, ColProj, Distance)
*             disp_message (WindowHandle, '点到点的距离'+ Distance*ratio +'mm', 'window', 30, 12, 'black', 'true')
    
    *两线夹角
    angle_ll ( RowBegin, ColBegin, RowEnd, ColEnd,\
            RowBegin1, ColBegin1, RowEnd1, ColEnd1,\
            Angle1)
    disp_message (WindowHandle, '两线的夹角为'+deg(Angle1), 'window', 48, 12, 'black', 'true')
    
else
    disp_message (WindowHandle, '没有找到线段', 'window', 12, 180, 'black', 'true')
endif
        

【halcon 实现模板匹配,定位,找线,找点,找圆】

显示之类的

angle :=deg(Angle)
if (angle>180)
    angle :=angle-360
endif


dev_display (ShapeModelTrans)
dev_set_draw ('margin')
dev_set_line_width (1)
dev_display (Rectangle1Check)
dev_set_color ('blue')
dev_set_line_width (2)
dev_display (Line1)
dev_display (Line2)
dev_set_line_width (1)
dev_display (Contours1)
dev_display (Contours2)
dev_display (Contours3)

dev_set_color ('green')
dev_set_line_width (2)
dev_display (ContourOut1)
dev_display (ContourOut2)
dev_display (ContourOut3)

if (Length2>1)
    dev_display (ContCircle)
endif

*窗口绘制直线
*         disp_line (WindowHandle, RowBegin, ColBegin, RowEnd, ColEnd)
*         disp_line (WindowHandle, RowBegin1, ColBegin1, RowEnd1, ColEnd1)
if (Length==1 and Length1==1)
    dev_display (Cross4)
*             dev_set_color ('red')
    dev_display (Cross5)
    
endif
disp_message (WindowHandle, 'Score: ' + Score$'.2f'+'    水平角度'+angle, 'image', Row, Column+70, 'black', 'true')

endif

【halcon 实现模板匹配,定位,找线,找点,找圆】文章来源地址https://www.toymoban.com/news/detail-454128.html

到了这里,关于【halcon 实现模板匹配,定位,找线,找点,找圆】的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 使用halcon实现3维点云物体与模型的匹配并显示差异

    一、读取3D模型文件,并采样生成对应的模板文件 二、一次读取3D的模型文件进行配准显示差异 三、效果展示 蓝色为模型相对于3D模板多出的部分 红色为模型相对于3D模板缺少的部分

    2024年02月16日
    浏览(25)
  • 基于halcon实现视觉定位框架(C#做主算法,C#、MFC、Qt二次开发)【附源码】

    本文主要实现halcon二次开发,基于C#做视觉算法的编辑,已C#做用户空间,然后使用C#、C++(MFC、Qt)分别实现调用,从而实现多相机的使用。 换句话说就是:C#做算法及主界面开发,然后把生成的控件dll移交给C#或者MFC或者QT进行二次调用实现二次开发,这里主要想展示的是多

    2024年01月19日
    浏览(69)
  • Python+Kivy+OpenCV实现图片定位和匹配

    在本文中,我们将介绍如何使用Kivy和OpenCV库来构建一个简单的图片定位工具。这个工具可以帮助您在一张大图片中找到与模板图片匹配的位置,并将匹配结果以矩形框的形式显示出来。 首先,我们需要安装Kivy和OpenCV库。Kivy是一个开源的Python库,用于开发多点触摸应用程序。

    2024年02月21日
    浏览(29)
  • 图像模板匹配 opencv c++实现

    用T表示模板图像,I表示待匹配图像,切模板图像的宽为w高为h,用R表示匹配结果,匹配过程如下图所示: 1、平方差匹配算法method=TM_SQDIFF 这类方法利用平法差来进行匹配,最好匹配为0,而若匹配越差,匹配值则越大 2、归一化平方差匹配法method=TM_SQDIFF_NORMED 3、相关匹配法

    2024年02月04日
    浏览(33)
  • [3D&Halcon] 三维点云匹配&无序抓取

    📢博客主页:https://loewen.blog.csdn.net 📢欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正! 📢本文由 丶布布 原创,首发于 CSDN, 转载注明出处 🙉 📢现在的付出,都会是一种沉淀,只为让你成为更好的人✨ 一. 3D无序抓取原理 通过 3D 成像系统(激光三角、结构光+单/双目等

    2024年02月08日
    浏览(31)
  • 定位教程1----Halcon自标定方法

    本章节给大家介绍不用标定板,标定精度还很高的方法。 原始照片如下: 发现照片是畸变很大的照片。 此时可能会采用机器人九点标定,但实际情况是由于机械空间限制,机器人标定的时候,其范围只有如下图所示: 只能在这个小的空间里进行标定,红色框范围内的电池抓取还

    2024年02月09日
    浏览(22)
  • c#联合Halcon进行几何定位

    1: 首先配置在winfom引用程序中引用两个halcon应用程序的库:分别是halcon.dll和halcondotnet.dll,而后把这两个库放在你的应用程序输出路径下面,然后在下边的图片中取消首选32位的勾选。 2:右键点击工具箱,单击选择项,然后点击浏览按钮,引用 halcondotnet.dll控件,点击确定就

    2024年01月20日
    浏览(29)
  • 多尺度模板匹配(openCV实现find_scaled_shape_model)

            一直在写基于形状的模板匹配(仿照halcon,cognex),我们知道任意的二维仿射变换可以分解为以下几种变换:缩放变换(用不同比例因子分别对图像X轴Y轴进行缩放)、倾斜变换(X轴保持固定的情况下,Y轴相对于X轴旋转角度θ)、旋转变换(X轴Y轴同时旋转角度Φ)

    2024年02月04日
    浏览(71)
  • 【halcon】特征点匹配proj_match_points_ransac算子

            halcon的许多算子内涵两种以上的理论或算法,proj_match_points_ransac就是其中之一,它至少包含了射影几何和ransac算法两个内容。本篇将讲述三个方面: 1、射影几何的透视投影原理; 2、ransac的回归原理; 3、proj_match_points_ransac如何使用的案例;         该算子有

    2024年02月11日
    浏览(35)
  • 利用OpenCV与Qt5构建卡尺找圆工具(C++实现)

    基于OpenCV与Qt5构建卡尺找圆工具 博主近期基于海康Vision Master4.0做了一个工业视觉工程项目,其中就使用到了海康VM的找圆工具,然后博主根据其中的技术原理,也仿照开发了一个类似的功能,基于OpenCV和Qt5构建一个卡尺找圆的工具。卡尺找圆是在计算机视觉领域中常用的技术

    2024年02月10日
    浏览(53)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包