measure_fill_level.dev
* In this example a fill level check for the
* pharmaceutical industry is demonstrated. The task is
* to check for the fill level of each single nose drop ampoule.
* To do so, we first locate each ampoule head by applying
* shape-based matching, then we find the fill level
* by measuring the gray level change using a 1D Measuring.
*
*
dev_close_window ()
dev_update_off ()
read_image (Image, 'ampoules/ampoules_01')
get_image_size (Image, Width, Height)
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
dev_set_line_width (2)
dev_set_draw ('margin')
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
*
* Create a model for the ampoule head to align the measure handle
gen_rectangle1 (Rectangle, 230, 280, 317, 330)
reduce_domain (Image, Rectangle, ImageModel)
*创建一个模板,用这个模板去匹配所有能找到的图像
create_shape_model (ImageModel, 'auto', 0, 0, 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID)
*
* Initialize the measure handle
* 创建一个测量矩形
Tolerance := 15
*
* Determine the fill level
NumImages := 8
gen_measure_rectangle2 (0, 0, rad(90), 75, 20, Width, Height, 'bilinear', MeasureHandle)
for Index := 1 to NumImages by 1
read_image (Image, 'ampoules/ampoules_' + Index$'.2d')
ColumnEdges := []
FillLevelHeight := []
* 使用模板匹配来找到每一份图像中的所有的模板
find_shape_model (Image, ModelID, 0, 0, 0.7, 0, 0.1, 'least_squares', 0, 0.9, Row, Column, Angle, Score)
* 匹配到的那个模板的平均行的位置
MeanRow := mean(Row)
* 平均行的位置-160 就是我们做的一个标准的位置
RefLevel := MeanRow - 160
* Display tolerance area
dev_display (Image)
dev_set_line_width (1)
dev_set_color ('white')
* 画一个合格的区间范围
gen_rectangle2 (AcceptLevel, RefLevel, mean(Column), 0, 30 + (max(Column) - min(Column)) / 2, Tolerance)
dev_display (AcceptLevel)
dev_set_line_width (2)
*
* Determine fill level of each ampoule
nums:=|Score|
Errors := 0
for Idx := 0 to nums - 1 by 1
* translate_measure( : : MeasureHandle, Row, Column )(选用)
*名字:转换一个度量对象
*描述:一般用于一个程序中有很多测量矩形的情况,当使用第二个测量矩形时,不需要重新 gen_measure_rectangle2生成,将第二个测量矩形的中心坐标放到该算子的第二、三个参数当中即可,其第一个参数得到的句柄就相当于使用gen_measure_rectangle2算子正常生成的测量矩形句柄。然后使用measure_pos对该句柄进行正常计算。
*参数:
*MeasureHandle:测量句柄
*Row:新参考点的行坐标
*Column :新参考点
newrow:=MeanRow - 135
translate_measure (MeasureHandle, newrow, Column[Idx])
* 新的基准
dev_set_color ('pink')
gen_cross_contour_xld (Cross2, newrow, Column[Idx], 40, 45)
* Search for the topmost edge
measure_pos (Image, MeasureHandle, 2, 7, 'all', 'first', RowEdge, ColumnEdge, Amplitude, Distance)
*测量得到的高度值
FillLevelHeight := [FillLevelHeight,RowEdge]
*
ColumnEdges := [ColumnEdges,ColumnEdge]
*
* 画个查号
dev_set_color ('blue')
gen_cross_contour_xld (Cross, RowEdge, ColumnEdge, 15, 0)
gen_rectangle2 (FillLevel, RowEdge, ColumnEdge, 0, 28, 20)
if (abs(FillLevelHeight[Idx] - RefLevel) >= Tolerance)
gen_rectangle2 (ChamberSingle, MeanRow - 133, Column[Idx], 0, 35, 90)
gen_cross_contour_xld (Cross, FillLevelHeight[Idx], ColumnEdges[Idx], 15, 0)
gen_rectangle2 (FillLevel, FillLevelHeight[Idx], ColumnEdges[Idx], 0, 28, 20)
Errors := Errors + 1
dev_set_color ('red')
dev_display (ChamberSingle)
disp_message (WindowHandle, 'NG', 'image', FillLevelHeight[Idx] - 50, ColumnEdges[Idx] - 10, 'red', 'false')
else
disp_message (WindowHandle, 'OK', 'image', FillLevelHeight[Idx] - 50, ColumnEdges[Idx] - 10, 'green', 'false')
dev_set_color ('green')
endif
dev_display (FillLevel)
dev_display (Cross)
endfor
stop()
*
* Check, whether the fill level is within the allowed range - does not deviate too much
* from average fill level
*
* Display statistics
if (Errors > 0)
disp_message (WindowHandle, Errors + ' BAD', 'window', 10, 12, 'red', 'true')
else
disp_message (WindowHandle, 'All OK', 'window', 10, 12, 'forest green', 'true')
endif
if (Index < NumImages)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
endfor