Halcon例程分析19:點膠質量-斷膠、少膠、膠寬、膠路檢測

打開halcon,按下ctrl+e打開halcon自帶例程。應用範圍->二維測量比較->apply_bead_inspection_model.hdev

* This example shows how to use bead inspection to verify
* adhesive beads. Bead inspection can be used to detect the
* following errors:
*  - segments where adhesive is missing
*  - segments with too much or too little adhesive
*  - segments where adhesive is too far from its destined
*    position
* 
* apply_bead_inspection_model requires that the images to inspect
* are already aligned with the reference contour. In this example,
* the alignment is based on planar deformable matching.
* 
dev_update_off ()
* 
* Create the planar deformable model to align images with the
* reference contour
*prepare_alignment (RegionPart, RowT, ColumnT, ModelID)

Row := [658.232,291.923,314.817,691.533]
Col := [330.071,316.617,971.032,947.008]
* Set respective point coordinates to which the above
* defined points will be rectified
W := 5.4
H := 3.4
Row1 := 300 + [H,0,0,H] * 120.0
Col1 := 300 + [0,0,W,W] * 120.0
*計算像素座標到實際座標的映射關係矩陣
vector_to_proj_hom_mat2d (Row, Col, Row1, Col1, 'normalized_dlt', [], [], [], [], [], [], HomMat2D, Covariance)
* 
* Read the reference image (image withou adhesive bead)
read_image (ImageRef, 'bead/adhesive_bead_ref')
* 
* Rectify the reference image
*用像素座標到實際座標的映射關係矯正參考圖像
projective_trans_image (ImageRef, ImageRefTrans, HomMat2D, 'bilinear', 'false', 'false')
* 
* Determine a region that contains the planar
* part on which the adhesive bead will be applied.
* This planar part will also be used to align the
* test images with the reference image.
*圖像自動二值化
binary_threshold (ImageRefTrans, Region, 'smooth_histo', 'light', UsedThreshold)
*開運算
opening_circle (Region, RegionOpening, 5.5)
*分割圖像
connection (RegionOpening, ConnectedRegions)
*計算每塊分割出的圖像的面積,橫縱座標
area_center (ConnectedRegions, AreaCR, RowCR, ColumnCR)
*選擇分割出來的面積最大的那個區域
select_obj (ConnectedRegions, ObjectSelected,  sort_index(-AreaCR)[0] +1)
*填充區域
fill_up (ObjectSelected, RegionFillUp)
*區域膨脹操作
dilation_circle (RegionFillUp, RegionPart, 5.5)
*從原圖像中摳出選中的區域中的圖像
reduce_domain (ImageRefTrans, RegionPart, ImageReduced)
*計算摳出的那部分圖像的面積與橫縱座標
area_center (RegionPart, AreaT, RowT, ColumnT)
*利用摳出來的那部分圖像創建一個模板
create_planar_uncalib_deformable_model (ImageReduced, 'auto', [], [], 'auto', 1, [], 'auto', 1, [], 'auto', 'none', 'use_polarity', 'auto', 'auto', [], [], ModelID)
*計算那個區域的外包矩形
smallest_rectangle1 (RegionPart, PartRow1, PartColumn1, PartRow2, PartColumn2)
* 
* Define the reference path of the adhesive beads. This path could
* also be generated by drawing it on a reference image with, e.g.,
* draw_nurbs.
*生成點膠軌跡路徑,根據實際項目也可以直接在ROI區域中畫出這個軌跡圖,需要用繪畫XLD格式的對象
gen_contour_nurbs_xld (ContourRef, [701.767,626.953,538.867,443.54,390.447,360.28,354.247,363.9,400.1,458.02,509.907,588.34,659.533,696.94], [319.24,336.133,367.507,431.46,489.38,546.093,646.247,722.267,776.567,826.04,869.48,912.92,934.64,929.813], 'auto', [15,15,15,15,15,15,15,15,15,15,15,15,15,15], 3, 1, 5)
* 
* Create a new bead inspection model with the following parameters
*定義點膠的標準膠路寬帶
TargetWidth := 14
*定義可容忍的膠寬範圍,這裏表示7到21範圍內的膠寬都是合理的
WidthTolerance := 7
*定義膠路可以偏移的範圍,
PositionTolerance := 30
Polarity := 'dark'
*創建膠路檢測模板
create_bead_inspection_model (ContourRef, TargetWidth, WidthTolerance, PositionTolerance, Polarity, [], [], BeadInspectionModel)
* 
* Show a correct adhesive bead together with the reference contour
read_image (Image, 'bead/adhesive_bead_01')
*通過上面的模板找到點膠的大致位置
find_planar_uncalib_deformable_model (Image, ModelID, -0.39, 0.78, 1, 1, 1, 1, 0.4, 1, 1, 5, 0.9, [], [], HomMat2D1, Score)
*把發生偏移了的圖像矯正回與參考圖像一致的位置
hom_mat2d_invert (HomMat2D1, HomMat2DInvert)
hom_mat2d_translate (HomMat2DInvert, RowT, ColumnT, HomMat2DTranslate)
projective_trans_image (Image, ImageAligned, HomMat2DTranslate, 'bilinear', 'false', 'false')
*align_bead (Image, ImageAligned, ModelID, RowT, ColumnT)
* 
* Create two parallel contours to give an impression of how
* thick a correct adhesive bead should be
*根據上面定義的點膠路徑,生成兩個與點膠路徑平行的XLD,用來指示膠路的寬度範圍,純粹是爲了顯示
gen_parallel_contour_xld (ContourRef, ModelSide1, 'regression_normal', TargetWidth * 0.5)
gen_parallel_contour_xld (ContourRef, ModelSide2, 'regression_normal', -TargetWidth * 0.5)
concat_obj (ModelSide1, ModelSide2, ModelSides)
* 
* Create two parallel contours to give an impression of where
* a correct adhesive bead should be located
*根據上面定義的點膠路徑,生成兩個與點膠路徑平行的XLD,用來指示位置的範圍,純粹是爲了顯示更直觀
gen_parallel_contour_xld (ContourRef, PositionToleranceSide1, 'regression_normal', PositionTolerance)
gen_parallel_contour_xld (ContourRef, PositionToleranceSide2, 'regression_normal', -PositionTolerance)
concat_obj (PositionToleranceSide1, PositionToleranceSide2, PositionToleranceSides)
dev_close_window ()
dev_open_window_fit_size (0, 0, PartColumn2 - PartColumn1 + 1, PartRow2 - PartRow1 + 41, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_part (PartRow1 - 20, PartColumn1, PartRow2 + 20, PartColumn2)
dev_display (ImageAligned)
dev_set_line_width (2)
dev_set_color ('green')
dev_display (ContourRef)
dev_set_line_width (1)
dev_display (ModelSides)
dev_set_color ('yellow')
dev_display (PositionToleranceSides)
Message := 'Correct adhesive bead and the reference contour. The'
Message[1] := 'yellow contours indicate the range of position tolerance.'
dev_disp_text (Message, 'window', 12, 12, 'black', [], [])
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
stop ()
* 
* 
* Now, perform the inspection task
*以上定義好了標準的點膠路徑與膠路寬度範圍與膠路位置範圍,下面讀取出圖片進行檢測
TextOffset := 20
NumImages := 7
for Index := 1 to NumImages by 1
    read_image (Image, 'bead/adhesive_bead_' + Index$'02')
    * 
    * Align the input image with the reference image
    *align_bead (Image, ImageAligned, ModelID, RowT, ColumnT)
    *先找出點膠位置的鐵塊在哪裏把之轉換到參考圖像指示的點膠位置處
    find_planar_uncalib_deformable_model (Image, ModelID, -0.39, 0.78, 1, 1, 1, 1, 0.4, 1, 1, 5, 0.9, [], [], HomMat2D1, Score)
    hom_mat2d_invert (HomMat2D1, HomMat2DInvert)
    hom_mat2d_translate (HomMat2DInvert, RowT, ColumnT, HomMat2DTranslate)
    projective_trans_image (Image, ImageAligned, HomMat2DTranslate, 'bilinear', 'false', 'false')
    * 
    * Apply the bead inspection model to the aligned image
    *把圖像轉換到標準點膠路徑位置後對圖像進行膠路檢測,檢測到左輪廓和右輪廓,如果膠路有誤,指示出錯誤類型
    apply_bead_inspection_model (ImageAligned, LeftContour, RightContour, ErrorSegment, BeadInspectionModel, ErrorType)
    * 
    * Display the segmented adhesive bead with its error segments
    set_window_param (WindowHandle, 'flush', 'false')
    dev_clear_window ()
    dev_display (ImageAligned)
    dev_set_line_width (1)
    dev_set_color ('white')
    dev_display (ContourRef)
    dev_display (ModelSides)
    dev_display (PositionToleranceSides)
    dev_set_line_width (2)
    dev_set_color ('green')
    dev_display (LeftContour)
    dev_display (RightContour)
    dev_set_color ('red')
    dev_display (ErrorSegment)
    *檢測到膠路正常沒有錯誤
    if (|ErrorType| == 0)
        * No errors detected
        Message := 'Adhesive bead is OK'
        dev_disp_text (Message, 'window', 12, 12, 'white', 'box_color', 'forest green')
        dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
        flush_buffer (WindowHandle)
        set_window_param (WindowHandle, 'flush', 'true')
        stop ()
    *檢測到膠路異常
    else
        * Display errors by error class
        *以下代碼是顯示出一些錯誤信息
        Message[0] := 'Adhesive bead is not OK:'
        * 
        ErrorClasses := ['no bead','too thin','too thick','incorrect position']
        for ClassIndex := 0 to |ErrorClasses| - 1 by 1
            Class := ErrorClasses[ClassIndex]
            ErrorIndices := find(ErrorType,Class)
            if (ErrorIndices != -1)
                select_obj (ErrorSegment, SelectedSegments, ErrorIndices + 1)
                dev_set_color ('red')
                dev_set_line_width (3)
                if (Class != 'no bead')
                    *生成錯誤區段
                    gen_display_segments (SelectedSegments, LeftContour, RightContour, ErrorParts)
                    dev_display (ErrorParts)
                else
                    dev_display (SelectedSegments)
                endif
                area_center_points_xld (SelectedSegments, Area, Row, Column)
                for E := 0 to |ErrorIndices| - 1 by 1
                    dev_disp_text (ErrorIndices[E] + 1, 'image', Row[E], Column[E] - TextOffset, 'white', 'box_color', 'red')
                    TextOffset := 20 - TextOffset
                endfor
            endif
        endfor
        dev_disp_text (Message, 'window', 12, 12, 'white', 'box_color', 'red')
        dev_disp_text ([1:|ErrorType|] + ': ' + ErrorType, 'image', 500, 500, 'red', 'box', 'false')
        if (Index < NumImages)
            dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
            flush_buffer (WindowHandle)
            set_window_param (WindowHandle, 'flush', 'true')
            stop ()
        endif
    endif
endfor
flush_buffer (WindowHandle)
set_window_param (WindowHandle, 'flush', 'true')
* 
* Release all allocated memory
clear_bead_inspection_model (BeadInspectionModel)
clear_deformable_model (ModelID)

以上例程加入了過多圖像預處理的干擾信息,實際上膠路檢測只需要定義標準膠寬,膠寬範圍,膠路位置範圍,生成膠路路徑。

然後用create_bead_inspection_model 創建檢測模型

讀取檢測圖像後用apply_bead_inspection_model 檢測膠路即可得到膠路的完整性信息

點膠模板圖片:

點膠質量檢測圖片:

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