check_blister.hdev
* 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 analysis 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.
*选取多通道图像中的第一通道
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)
*得到这个区域到0角度位置的仿射矩阵
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)
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)
*角度转成正的(+180)
PhiRef := rad(180) + PhiRef
*得到药品所在的大区域的中心和面积
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
*抠出所有的矩形小区域
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
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)
*如果面积为0,表示没有药品,大于0,表示有
if (Area > 0)
*得到最小最大灰度和面积
min_max_gray (Pill, ImageB, 0, Min, Max, Range)
*如果相交的面积小于某一个值,说明药品不全;如果最小值小于60,说明药品放错了
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)
if (Index < Count)
disp_continue_message (WindowHandle, 'black', 'true')
endif
stop ()
endfor



