【Halcon 测量篇】(2) 拟合求距离

一、拟合流程:
在这里插入图片描述
1、 采集图像

2、图像预处理: 一般是去噪或抠图(blob分析抠图或手绘ROI区域抠图)两方面

3、轮廓提取
1)boundary:区域轮廓提取
2)edges_sub_pix:图像轮廓提取
3)threshold_sub_pix:图像轮廓提取

使用算子edges_sub_pix进行亚像素的边缘提取最为普遍。其用到的滤波器有Deriche, Lanser, Shen, or Canny filters。
关于这几个滤波器的对比,帮助文档有如下介绍:

  • Deriche, Lanser, Shen为递归滤波器,Canny 为掩膜滤波器;
  • 递归滤波器的执行时间不依赖滤波器的大小,Canny的执行时间与滤波器大小成正相关。
  • 参数alpha数值越大,Deriche, Lanser, Shen滤波器宽度越小,平滑越差,细节越突出,而Canny效果相反。

4、 分割、联合(根据情况而定)

分割算子:
segment_contours_xld:可分割’lines’,‘lines_circles’,‘lines_ellipses’,原理是多边形逼近,逼近程度通过算子中后两个阀值参数控制。

联合算子:
临近:union_adjacent_contours_xld (Operator)
共线:union_collinear_contours_xld (Operator)
共圆:union_cocircular_contours_xld (Operator)

5、 拟合
fit_line_contour_xld:拟合直线
fit_line_contour_xld:拟合圆
fit_ellipse_contour_xld:拟合椭圆
fit_rectangle2_contour_xld:拟合矩形

注:有时候在拟合轮廓之前需要判断一下轮廓属性,以确定应拟合成直线还是还是圆,可通过算子:
get_contour_global_attrib_xld (SingleSegment,‘cont_approx’, Attrib)
名字:获取轮廓属性
描述:用于确定应拟合成直线还是还是圆
参数:
SingleSegment:输入轮廓(input_object)
cont_approx:属性名称,即采用什么方式去计算 ,一般用这个参数就可以了(input_control)
Attrib:属性值: Attrib>0:拟合圆,否则拟合直线(output_control) )

6、 求距离
 

二、示例:

*1、读取图片
dev_close_window ()
dev_open_window (0, 0, 680, 350, 'black', WindowHandle1)
dev_update_window ('off')
read_image (Image1, 'C:/Users/Administrator/Desktop/图片/1.bmp')
dev_set_line_width (3)
*2、边缘提取
edges_sub_pix (Image1, Edges, 'canny', 1, 20, 40)
*3、分割
segment_contours_xld (Edges, ContoursSplit, 'lines_circles', 5, 1.5,2)
dev_display (Edges)
sort_contours_xld (ContoursSplit, SortedContours, 'upper_left', 'true', 'row')
dev_clear_window ()
dev_set_colored (12)
dev_display (SortedContours)
NumCircle:=0
NumLine:=0
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
dev_set_part (117.891, 154.794, 500, 385.382)
dev_display (Image1)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
count_obj (SortedContours, Number)
for i := 1 to Number by 1
    select_obj (SortedContours, ObjectSelected, i)
    get_contour_global_attrib_xld (ObjectSelected, 'cont_approx', Attrib)
    if(Attrib > 0)  
        NumCircle:=NumCircle+1
        *4、拟合
        fit_circle_contour_xld (ObjectSelected, 'algebraic', -1, 0, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
        gen_ellipse_contour_xld (ContEllipse, Row, Column, 0, Radius, Radius, 0, 6.28318, 'positive', 1.5)
        *显示
        dev_set_color('white') 
        dev_display (ContEllipse)             
        set_tposition (WindowHandle, Row-Radius-15, Column-Radius)
        write_string (WindowHandle, 'C'+NumCircle)
        ResultText := 'C'+ NumCircle +': Radius ='+ Radius
    else
         NumLine:= NumLine+1
        *4、拟合,求距离
        fit_line_contour_xld (ObjectSelected, 'tukey', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)
        gen_contour_polygon_xld (Contour, [RowBegin,RowEnd], [ColBegin,ColEnd])
        *显示
        dev_set_color('yellow') 
        dev_display (Contour)             
        distance_pp (RowBegin, ColBegin, RowEnd, ColEnd, Distance)
        set_tposition (WindowHandle, (RowBegin+RowEnd)/2-15,(ColBegin+ColEnd)/2-10)
        write_string (WindowHandle, 'C'+NumLine)
        ResultText :='C'+ NumLine+': length = '+ Distance
    endif
    set_tposition (WindowHandle,300+i*15,180)
    *5、显示
    write_string (WindowHandle, ResultText )
endfor

 
在这里插入图片描述
在这里插入图片描述

发布了74 篇原创文章 · 获赞 24 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_43197380/article/details/95214690
今日推荐