【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

 
在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章