halcon中variation_model_single實例註釋.

 
* This example shows how to employ the new extensions of HALCON's variation model operators
* to perform customary print quality tests.
* In this example the variation model is built upon a single reference image.
* The example consists of three steps:
* 1. align the print objects similar to the reference image using a shape-based model
* 2. define the variation image by smoothing the object's contours
* 3. create the variation model
* Whether a print is labelled as OK or not, depends upon the size (area) of the difference to the reference image
app

dev_close_window ()dom

*讀取圖片ide

read_image (Image, 'pen/pen-01')字體

*獲取大小ui

get_image_size (Image, Width, Height)this

*打開窗口orm

dev_open_window (0, 0, Width, Height, 'black', WindowHandle)對象

*關閉窗口更新three

dev_update_off ()圖片

*設置字體

set_display_font (WindowHandle, 12, 'courier', 'true', 'false')

*顯示圖片

dev_display (Image)


* segment the logo and create a shape model for the alignment

*二值化圖片

threshold (Image, Region, 125, 255)

*填充圖片

fill_up (Region, RegionFillUp)

*計算兩圖片的不一樣,獲得中間灰色有圖像的部分.

difference (RegionFillUp, Region, RegionDifference)

*將區域進行凸包變換

shape_trans (RegionDifference, LogoArea, 'convex')

*腫脹區域,圖片reduce區域處理完成

dilation_circle (LogoArea, LogoArea, 7)

*將圖片從新剪切爲有顯示的區域.

reduce_domain (Image, LogoArea, ImageReduced)

*建立模板

create_shape_model (ImageReduced, 'auto', -rad(10), rad(20), 'auto', 'auto', 'use_polarity', [40,50], 40, ShapeModelID)

*計算中心點和麪積

area_center (LogoArea, Area, ModelRow, ModelColumn)

 


* define the variation image by smoothing the dilated regions obtained from the object's contours:
* Besides a binomial filter a neat trick is applied to get smoothly "polished" regions along the contours.
* In particular, the edges are enlarged and after their conversion into a dilated region the image

* is zoomed back to its original size using a weighting that smoothes the images further.

*亞像素分割圖片

edges_sub_pix (ImageReduced, Edges, 'sobel_fast', 0.5, 10, 20)

*建立一個變換矩陣

hom_mat2d_identity (HomMat2DIdentity)

*矩陣x,y放大

hom_mat2d_scale (HomMat2DIdentity, 4, 4, 0, 0, HomMat2DScale)

*變換矩陣

affine_trans_contour_xld (Edges, ZoomedEdges, HomMat2DScale)

*產生空白圖片

gen_image_const (VarImageBig, 'byte', 4*Width, 4*Height)

*計算XLD的對象

count_obj (ZoomedEdges, NEdges)

for i := 1 to NEdges by 1

   *選擇對象

    select_obj (ZoomedEdges, ObjectSelected, i)

   *獲得XLD的XY座標點集

    get_contour_xld (ObjectSelected, RowEdge, ColEdge)

   *根據XLD座標點集生成多邊形

    gen_region_polygon (Region1, RowEdge, ColEdge)

  *腫脹區域,變得圓滑一些.

    dilation_circle (Region1, RegionDilation, 2.5)

   *將區域畫到新的空圖片上.

    paint_region (RegionDilation, VarImageBig, VarImageBig, 255, 'fill')

endfor

*壓縮圖片大小及正常大小

zoom_image_size (VarImageBig, VarImageSmall, Width, Height, 'weighted')

*binomial平滑圖片

binomial_filter (VarImageSmall, VarImage, 3, 3)

*創建一個可變化比較模板

create_variation_model (Width, Height, 'byte', 'direct', VarModelID)

*將image圖片轉換爲可變化比較模板VarModelID

prepare_direct_variation_model (Image, VarImage, VarModelID, 15, 4)

*顯示這個模板

dev_display (VarImage)

disp_message (WindowHandle, 'Variation Image', 'window', -1, -1, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()

* print inspection

*如下開始比較圖片了

for i := 1 to 30 by 1

    *讀取圖片

    read_image (Image, 'pen/pen-'+i$'02d')

    * locate the logo and align it to the reference image

* 經過模板查找目標目標位置座標和角度

    find_shape_model (Image, ShapeModelID, -rad(10), rad(20), 0.5, 1, 0.5, 'least_squares', 0, 0.9, Row, Column, Angle, Score)

    if (|Score| # 0)

       *找到了目標,就剛性變換

        vector_angle_to_rigid (Row, Column, Angle, ModelRow, ModelColumn, 0, HomMat2D)

        affine_trans_image (Image, ImageAffinTrans, HomMat2D, 'constant', 'false')

       *剪切圖片

        reduce_domain (ImageAffinTrans, LogoArea, ImageReduced1)

      * 開始比較

        compare_ext_variation_model (ImageReduced1, RegionDiff, VarModelID, 'absolute')

        *連通區域

        connection (RegionDiff, ConnectedRegions)

        *經過面積選擇區域,忽略過小的點啊啥的.

        select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 10, 99999)

        *如下開始顯示結果了 

      

        dev_display (ImageAffinTrans)

        *計算查找到的不一樣區域個數並顯示ng or pass

        count_obj (SelectedRegions, NDefects)
        if (NDefects > 0)
            dev_set_color ('red')
            dev_set_draw ('margin')
            dev_set_line_width (2)
            dev_display (SelectedRegions)
            dev_set_color ('green')
            dev_set_line_width (1)
            dev_display (Edges)
            disp_message (WindowHandle, 'Image check not OK', 'window', -1, -1, 'red', 'false')
        else
            disp_message (WindowHandle, 'Image check OK', 'window', -1, -1, 'green', 'false')
        endif
    endif
    disp_continue_message (WindowHandle, 'black', 'true')
    stop ()
endfor

* clean up

*最後記得要清理垃圾

clear_shape_model (ShapeModelID) clear_variation_model (VarModelID)

相關文章
相關標籤/搜索