halcon 中的药片检查check_blister实例注释

时间:2021-12-27 21:04:50
* This example demonstrates an application from the pharmaceutical
* industry. The task is to check the content of automatically filled
* blisters. The first image (reference) is used to locate the chambers
* within a blister shape as a reference model, which is then used to
* realign the subsequent images along to this reference shape. Using
* blob analyses the content of each chamber is segmented and finally
* classified by a few shape features.
* 先生成标准区域,然后按新的图片区域与标准区域交集的灰度和面积来判定缺陷.


dev_close_window ()
dev_update_off ()
read_image (ImageOrig, 'blister/blister_reference')
dev_open_window_fit_image (ImageOrig, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (3)

* In the first step, we create a pattern to cut out the chambers in the
* subsequent blister images easily.
*二值化,选取大于90小于255的灰度区域 
dev_display(ImageOrig)
threshold (ImageOrig, Region, 90, 255)
dev_clear_window()
dev_display(Region)
*区域凸包变换.变换后区域就规范了.
shape_trans (Region, Blister, 'convex')
*区域的定向
orientation_region (Blister, Phi)
*求出区域中心点和角度
area_center (Blister, Area1, Row, Column)
*图片刚性变换,只是角度变化,这下摆正了.
vector_angle_to_rigid (Row, Column, Phi, Row, Column, 0, HomMat2D)
affine_trans_image (ImageOrig, Image2, HomMat2D, 'constant', 'false')
dev_display(ImageOrig)
stop()
dev_clear_window()
dev_display(Image2)
stop()
*创建一个空的对象
gen_empty_obj (Chambers)
for I := 0 to 4 by 1
    Row := 88+I*70
    for J := 0 to 2 by 1
        Column := 163 + J*150
        gen_rectangle2 (Rectangle, Row, Column, 0, 64, 30)
        *加入rectangle对象到空对象中
        concat_obj (Chambers, Rectangle, Chambers)
    endfor
endfor
*区域变换,把区域摆正.
affine_trans_region (Blister, Blister, HomMat2D, 'false')
*差运算生成网格
difference (Blister, Chambers, Pattern)
*并运算
union1 (Chambers, ChambersUnion)
*求出区域方向.
orientation_region (Blister, PhiRef)

PhiRef := rad(180)+PhiRef

halcon 中的药片检查check_blister实例注释

*求出中心点
area_center (Blister, Area2, RowRef, ColumnRef)


* Each image read will be aligned to this pattern and reduced to the area of interest,
* which is the chambers of the blister
Count := 6
for Index := 1 to Count by 1
    read_image (Image, 'blister/blister_'+Index$'02')
    threshold (Image, Region, 90, 255)
    connection (Region, ConnectedRegions)
    select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 5000, 9999999)
    shape_trans (SelectedRegions, RegionTrans, 'convex')
    * 
    * align pattern along blister of image
    orientation_region (RegionTrans, Phi)
    area_center (RegionTrans, Area3, Row, Column)
    *仿射变换,角度和点都有变化.
    vector_angle_to_rigid (Row, Column, Phi, RowRef, ColumnRef, PhiRef, HomMat2D)
    affine_trans_image (Image, ImageAffinTrans, HomMat2D, 'constant', 'false')
    * 
    * segment pills
    dev_clear_window()
    dev_display(ImageAffinTrans)

    reduce_domain (ImageAffinTrans, ChambersUnion, ImageReduced)

   *分离彩色通道

    decompose3 (ImageReduced, ImageR, ImageG, ImageB)
    *阀值分割,较好地分开前景和背景
    var_threshold (ImageB, Region, 7, 7, 0.2, 2, 'dark')
    *连通区域
    connection (Region, ConnectedRegions0)
    * 闭运算,填充的效果
    closing_rectangle1 (ConnectedRegions0, ConnectedRegions, 3, 3)
    *填充
    fill_up (ConnectedRegions, RegionFillUp)
    *选择区域
    select_shape (RegionFillUp, SelectedRegions, 'area', 'and', 1000, 99999)
    *开运算.去掉小干扰点
    opening_circle (SelectedRegions, RegionOpening, 4.5)
    *再次连通区域
    connection (RegionOpening, ConnectedRegions)
    *选择区域
    select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 1000, 99999)
    *凸包变换
    shape_trans (SelectedRegions, Pills, 'convex')
    * 

    * classify segmentation results and display statistics

halcon 中的药片检查check_blister实例注释

    *计算对象
    count_obj (Chambers, Number)
    gen_empty_obj (WrongPill)
    gen_empty_obj (MissingPill)
    for I := 1 to Number by 1
        *选择标准对象
        select_obj (Chambers, Chamber, I)
        *交集
        intersection (Chamber, Pills, Pill)
        *计算交集中心点和面积
        area_center (Pill, Area, Row1, Column1)
        if (Area > 0)
            *计算最大最小灰度值
            min_max_gray (Pill, ImageB, 0, Min, Max, Range)
            if (Area<3800 or Min < 60)
                concat_obj (WrongPill, Pill, WrongPill)
            endif
        else
            concat_obj (MissingPill, Chamber, MissingPill)
        endif
    endfor
    * 
    dev_clear_window ()
    dev_display (ImageAffinTrans)
    dev_set_color ('forest green')
    count_obj (Pills, NumberP)
    count_obj (WrongPill, NumberWP)
    count_obj (MissingPill, NumberMP)
    dev_display (Pills)
    if (NumberMP > 0 or NumberWP > 0)
        disp_message (WindowHandle, 'Not OK', 'window', 10, 10+600, 'red', 'true')
    else
        disp_message (WindowHandle, 'OK', 'window', 10, 10+600, 'forest green', 'true')
    endif
    disp_message (WindowHandle, '# correct pills: ' + (NumberP - NumberWP), 'window', 10, 10, 'black', 'true')
    disp_message (WindowHandle, '# wrong pills  :  ' + NumberWP, 'window', 10+25, 10, 'black', 'true')
    if (NumberWP>0)
        disp_message (WindowHandle, NumberWP, 'window', 10+25, 10+180, 'red', 'true')
    endif
    disp_message (WindowHandle, '# missing pills:  ' + NumberMP, 'window', 10+50, 10, 'black', 'true')
    if (NumberMP > 0)
        disp_message (WindowHandle, NumberMP, 'window', 10+50, 10+180, 'red', 'true')
    endif
    dev_set_color ('red')
    dev_display (WrongPill)

    dev_display (MissingPill)

halcon 中的药片检查check_blister实例注释

    if (Index < Count)
        disp_continue_message (WindowHandle, 'black', 'true')
    endif
    stop ()
endfor