【Halcon 測量篇】(1) 一維測量

一、Halcon一維測量原理:

        像點到點的距離,邊緣對的距離等沿着一維方向的測量都屬於1D測量範疇。Halocn的一維測量首先構建矩形或者扇形的ROI測量對象,然後在ROI內畫出等距離的、長度與ROI寬度一致的、垂直於ROI的輪廓線(profile line)的等距線。如下圖所示。
在這裏插入圖片描述
測量ROI的輪廓線儘量與被測邊緣垂直,寬度適當寬些,等距線的密集度考慮速度與精度綜合選擇,這樣可以減少噪聲。

然後,沿着垂直輪廓線的方向,計算出每一條等距線的平均灰度值,可以得出輪廓線的灰度直方圖,同時可以選擇使用高斯濾波器平滑灰度直方圖。如下圖所示。Halcon算子measure_pairs的參數Sigma指定了高斯濾波器的標準差。

在這裏插入圖片描述
最後,求出平滑灰度直方圖的一階導數,一階導數的極值點作爲邊緣的亞像素精度候選點,只有一階導數極值點的絕對值大於預先設定的閾值(測量算子的參數Threshold)邊緣候選點才被選作爲邊緣中心點。Halcon測量算子最後得到每一條邊緣與輪廓線的交點。

在這裏插入圖片描述
 
二、一維測量算法流程:

在這裏插入圖片描述
核心思想:可參考halcon中一維測量中measure_pin中的例子
1)定位 (Blob分析或模板匹配)
2)測量
3)顯示

Halcon一維測量相關算子:

gen_measure_rectangle2( : : Row, Column, Phi, Length1, Length2, Width, Height, Interpolation : MeasureHandle)
名字:生成仿射矩形區域測量句柄
描述:用於提取垂直於仿射矩形的直邊
參數:
Row:仿射矩形中心行座標
Column:仿射矩形中心列座標
Phi:仿射矩形的縱軸水平角,單位弧度
Length1:仿射矩形寬度的一半
Length2:仿射矩形高度的一半
Width:圖像的寬度
Height:圖像的高度
Interpolation :插值類型
MeasureHandle:測量對象句柄
在這裏插入圖片描述
gen_measure_arc( : : CenterRow, CenterCol, Radius, AngleStart, AngleExtent, AnnulusRadius, Width, Height, Interpolation : MeasureHandle)
名字:生成環形區域測量句柄
描述:用於提取垂直環形圓弧的直邊緣。
參數:
CenterRow:圓弧中心行座標
CenterCol:圓弧中心列座標
Radius:圓弧半徑
AngleStart:圓弧起始角度
AngleExtent:圓弧角度範圍
AnnulusRadius:環形帶的半徑(寬度的一半)
Width:圖像的寬度
Height:圖像的高度
Interpolation :插值類型
MeasureHandle:測量對象句柄

在這裏插入圖片描述
measure_pairs(Image : : MeasureHandle, Sigma, Threshold, Transition, Select : RowEdgeFirst, ColumnEdgeFirst, AmplitudeFirst, RowEdgeSecond, ColumnEdgeSecond, AmplitudeSecond, IntraDistance, InterDistance)
名字:測量邊緣對
描述:提取垂直於仿射矩形或環形圓弧的直邊緣對。
參數:

Image:輸入圖像
MeasureHandle:測量對象句柄
Sigma:高斯平滑參數
Threshold:最小邊緣幅度
Transition:邊緣對極性,第一個與第二個相反
Select :選擇邊緣對
RowEdgeFirst:邊緣點對的第一個邊緣的中心行座標
ColumnEdgeFirst:邊緣點對的第一個邊緣的中心列座標
AmplitudeFirst:第一個邊緣的幅度
RowEdgeSecond:第二個邊緣中心行座標
ColumnEdgeSecond:第二個邊緣中心列座標
AmplitudeSecond:第二個邊緣幅度
IntraDistance:兩個邊緣對之間的距離
InterDistance:相鄰邊緣對之間的距離
 

measure_pos(Image : : MeasureHandle, Sigma, Threshold, Transition, Select : RowEdge, ColumnEdge, Amplitude, Distance)
名字:測量邊緣對
描述:提取垂直於仿射矩形或圓弧的直邊緣。
參數:

Image:輸入圖像
MeasureHandle:測量對象句柄
Sigma:高斯平滑係數
Threshold:最小邊緣幅度
Transition:極性
*Select:邊緣選擇
RowEdge:找到的邊緣中心的行座標
ColumnEdge:找到的邊緣中心列座標
Amplitude:邊緣幅度
Distance:相鄰邊緣之間的距離

measure_pairs與measure_pos的區別:一般,measure_pairs可以用於許多組邊緣對的情況,measure_pos用於一組邊緣對的情況。
幅度是指邊緣處明暗灰度各自的均值差,如下圖所示。

 
三、實例分析:測量藥瓶中液體的充滿率

dev_close_window ()
dev_update_off ()
read_image (Image, 'ampoules/ampoules_01')
get_image_size (Image, Width, Height)
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
dev_set_line_width (2)
dev_set_draw ('margin')
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
* 
* Create a model for the ampoule head to align the measure handle
gen_rectangle1 (Rectangle, 230, 280, 317, 330)
reduce_domain (Image, Rectangle, ImageModel)
create_shape_model (ImageModel, 'auto', 0, 0, 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID)
* 
* Initialize the measure handle
gen_measure_rectangle2 (0, 0, rad(90), 75, 20, Width, Height, 'bilinear', MeasureHandle)
Tolerance := 15
* 
* Determine the fill level
NumImages := 8
for Index := 1 to NumImages by 1
    read_image (Image, 'ampoules/ampoules_' + Index$'.2d')
    ColumnEdges := []
    FillLevelHeight := []
    * 
    find_shape_model (Image, ModelID, 0, 0, 0.7, 0, 0.1, 'least_squares', 0, 0.9, Row, Column, Angle, Score)
    MeanRow := mean(Row)
    RefLevel := MeanRow - 160
    * Display tolerance area
    dev_display (Image)
    dev_set_line_width (1)
    dev_set_color ('white')
    gen_rectangle2 (AcceptLevel, RefLevel, mean(Column), 0, 30 + (max(Column) - min(Column)) / 2, Tolerance)
    dev_display (AcceptLevel)
    dev_set_line_width (2)
    * 
    *  Determine fill level of each ampoule
    Errors := 0
    for Idx := 0 to |Score| - 1 by 1
        translate_measure (MeasureHandle, MeanRow - 135, Column[Idx])
        * Search for the topmost edge
        measure_pos (Image, MeasureHandle, 2, 7, 'all', 'first', RowEdge, ColumnEdge, Amplitude, Distance)
        FillLevelHeight := [FillLevelHeight,RowEdge]
        ColumnEdges := [ColumnEdges,ColumnEdge]
        gen_cross_contour_xld (Cross, RowEdge, ColumnEdge, 15, 0)
        gen_rectangle2 (FillLevel, RowEdge, ColumnEdge, 0, 28, 20)
        if (abs(FillLevelHeight[Idx] - RefLevel) >= Tolerance)
            gen_rectangle2 (ChamberSingle, MeanRow - 133, Column[Idx], 0, 35, 90)
            gen_cross_contour_xld (Cross, FillLevelHeight[Idx], ColumnEdges[Idx], 15, 0)
            gen_rectangle2 (FillLevel, FillLevelHeight[Idx], ColumnEdges[Idx], 0, 28, 20)
            Errors := Errors + 1
            dev_set_color ('red')
            dev_display (ChamberSingle)
            disp_message (WindowHandle, 'NG', 'image', FillLevelHeight[Idx] - 50, ColumnEdges[Idx] - 10, 'red', 'false')
        else
            disp_message (WindowHandle, 'OK', 'image', FillLevelHeight[Idx] - 50, ColumnEdges[Idx] - 10, 'green', 'false')
            dev_set_color ('green')
        endif
        dev_display (FillLevel)
        dev_display (Cross)
    endfor

    if (Errors > 0)
        disp_message (WindowHandle, Errors + ' BAD', 'window', 10, 12, 'red', 'true')
    else
        disp_message (WindowHandle, 'All OK', 'window', 10, 12, 'forest green', 'true')
    endif
    if (Index < NumImages)
        disp_continue_message (WindowHandle, 'black', 'true')
        stop ()
    endif
endfor
close_measure (MeasureHandle)
clear_shape_model (ModelID)

在這裏插入圖片描述
中心思想:以瓶底爲末班進行模板匹配,可以找到每個藥瓶中心點列座標,創建一個覆蓋瓶身的測量矩形框,以每個藥瓶中心點列座標爲基準平移測量矩形,可以得到藥瓶子液體邊緣的中心座標。生成一個白色的參考矩形框,只有液體邊緣行座標在這個矩形框中,纔算合格。

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