关于halcon:Halcon例程Blob分析之checkblisterhdev

54次阅读

共计 3982 个字符,预计需要花费 10 分钟才能阅读完成。

1、例程介绍

本例程次要展现了 Halcon 实现胶囊药丸的自动检测,目标是检测主动封装的药丸是否有漏装和错装。第一张图像作为参考图像,用来定位后续图像的胶囊的胶囊仓。用 Blob 剖析宰割的药丸并对进行分类这个实例中,用的最要害的货色是基于灰度的辨认,操作。

次要算子:
仿射变换算子:vector_angle_to_rigid,affine_trans_image
形态学算子:closing_rectangle1,opening_circle,shape_trans

2、例程解析

* 敞开窗体
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 ('fill')
* 设置线宽
dev_set_line_width (3)

* 获取图像中的通道一的图像
access_channel (ImageOrig, Image1, 1)
* 阈值宰割
threshold (Image1, Region, 90, 255)
* 变换形态,整体变凸
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')

* 生成空对象
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)
        * 联结矩形(union 是合几为一,原来的对象没有了。concat 只是组成一个汇合,原        
        * 来的对象还按程序放着)concat_obj (Chambers, Rectangle, Chambers)
    endfor
endfor
* 仿射变换区域到参考区域
affine_trans_region (Blister, Blister, HomMat2D, 'nearest_neighbor')
* 求差集
difference (Blister, Chambers, Pattern)
* 联结区域
union1 (Chambers, ChambersUnion)
* 求区域与极轴的夹角
orientation_region (Blister, PhiRef)
PhiRef := rad(180) + PhiRef
* 求面积和区域核心坐标
area_center (Blister, Area2, RowRef, ColumnRef)
stop ()

* 每一张读取到的图像都要向参考图像对齐,截取感兴趣区域
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')
    *
* 对齐图像 / 胶囊
* 求区域与极轴的夹角
    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, ImageAffineTrans, HomMat2D, 'constant', 'false')
    
    * Segment pills 宰割药片
reduce_domain (ImageAffineTrans, 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')
    
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 (ImageAffineTrans)
* 设置显示色彩
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', 12, 12 + 600, 'red', 'true')
    else
        disp_message (WindowHandle, 'OK', 'window', 12, 12 + 600, 'forest green', 'true')
    endif
    * 
    Message := '# Correct pills:' + (NumberP - NumberWP)
    Message[1] := '# Wrong pills  :' + NumberWP
    Message[2] := '# Missing pills:' + NumberMP
    * 设定各种药片显示色彩
    Colors := gen_tuple_const(3,'black')
    if (NumberWP > 0)
        Colors[1] := 'red'
    endif
    if (NumberMP > 0)
        Colors[2] := 'red'
endif
* 显示信息
disp_message (WindowHandle, Message, 'window', 12, 12, Colors, 'true')
* 设置显示色彩
    dev_set_color ('red')
    dev_display (WrongPill)
dev_display (MissingPill)
* 如果索引值小于计数数量 - 则持续
    if (Index < Count)
        disp_continue_message (WindowHandle, 'black', 'true')
    endif
    stop ()
endfor

3、例程配图

图一为模板图像和阈值和形态学分隔后的胶囊感兴趣区域

图二为理论检测图像成果

正文完
 0