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

  • Post author:
  • Post category:其他


c#联合halcon

链接:

https://www.bilibili.com/video/BV1aR4y1473b/

.

halcon函数速查

链接:

https://pan.baidu.com/s/1rbxf62bMh61Ie0pB4t3AHw

.

提取码:0000

图片链接

链接:

https://pan.baidu.com/s/1f0ld6nHcqzblvTnekEoSiQ

.

0000



读取图片

在这里插入图片描述

读取

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)

在这里插入图片描述



定位找线测量卡尺位置

同样的获取模板空间得到的 固定线段端点 在 原图上的位置

——

黑到白_____ 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')

在这里插入图片描述

第二根线同理

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')

在这里插入图片描述



定位找圆测量卡尺位置

圆也一样

 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')
        

在这里插入图片描述



求线段交点

*求线段交点
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
        

在这里插入图片描述



显示之类的

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

在这里插入图片描述



版权声明:本文为qq_41648925原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。