2016-06-17
* In this program the size of raw fish sticks is measured
* before they are coated with bread crumbs.
* First, the fish sticks are segmented from the background.
* Then, their dimensions are measured, compared to the
* required minimum size, and the result is displayed.
*
*设置不刷新
dev_update_off ()
*关闭显示窗口
dev_close_window ()
*读取图像
read_image (Image, 'food/fish_sticks_raw_01')
*打开一个显示窗口,-1,-1表示用上一次的窗口的大小
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
*设置显示字体
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
*设置画图为空心
dev_set_draw ('margin')
*保存系统参数到一个临时变量,最后再把这个参数给还原过去
get_system ('store_empty_region', StoreEmptyRegion)
*设置系统参数 store_empty_region
set_system ('store_empty_region', 'true')
* 每像素的换算参数
cm_per_pix := 0.0373
*目标的长度
TargetLength := 9.0
*上公差是多少
ToleranceHigh := 0.8
*下公差是多少
ToleranceLow := 0.2
* 设置图像张数为10
NumImages := 10
*进行for循环
for I := 1 to NumImages by 1
read_image (Image, 'food/fish_sticks_raw_' + I$'.2')
*
* Segment the raw fish sticks 选择比较白的地方
threshold (Image, Region, 50, 255)
connection (Region, ConnectedRegions)
fill_up (ConnectedRegions, RegionFillUp)
select_shape (RegionFillUp, SelectedRegions, 'area', 'and', 1000, 99999)
*
* Select the regions that do not intersect the image border
boundary (Image, RegionBorder, 'inner')
intersection (SelectedRegions, RegionBorder, RegionIntersection)
area_center (RegionIntersection, Area, Row1, Column1)
ValidRegionIndices := find(Area,0) + 1
if (ValidRegionIndices[0] == 0)
ValidRegionIndices := []
endif
select_tuple_larger (Area, 0.0, RegionAtBorderIndices)
RegionAtBorderIndices := RegionAtBorderIndices + 1
select_obj (SelectedRegions, ValidSticks, ValidRegionIndices)
select_obj (SelectedRegions, StickAtBorder, RegionAtBorderIndices)
*
* Check the size of the fish sticks
smallest_rectangle2 (ValidSticks, Row, Column, Phi, Length1, Length2)
FishLength1 := Length1 * 2 * cm_per_pix
select_tuple_larger (FishLength1, TargetLength - ToleranceLow, IndicesGood)
select_tuple_larger (FishLength1, TargetLength + ToleranceHigh, IndicesBad1)
tuple_remove (IndicesGood, IndicesBad1, IndicesGood)
select_tuple_larger (-FishLength1, -(TargetLength - ToleranceLow), IndicesBad2)
IndicesBad := [IndicesBad1,IndicesBad2]
RowGood := subset(Row,IndicesGood)
ColumnGood := subset(Column,IndicesGood)
RowBad := subset(Row,IndicesBad)
ColumnBad := subset(Column,IndicesBad)
FishLength1Good := subset(FishLength1,IndicesGood)
FishLength1Bad := subset(FishLength1,IndicesBad)
if (|IndicesGood| > 0)
gen_rectangle2 (GoodSticks, RowGood, ColumnGood, subset(Phi,IndicesGood), subset(Length1,IndicesGood), subset(Length2,IndicesGood))
else
gen_empty_obj (GoodSticks)
endif
if (|IndicesBad| > 0)
gen_rectangle2 (BadSticks, RowBad, ColumnBad, subset(Phi,IndicesBad), subset(Length1,IndicesBad), subset(Length2,IndicesBad))
else
gen_empty_obj (BadSticks)
endif
*
* Display the results
dev_display (Image)
dev_set_color ('white')
dev_set_line_width (1)
dev_display (ValidSticks)
dev_set_line_width (3)
dev_set_color ('yellow')
dev_display (StickAtBorder)
dev_set_color ('green')
dev_display (GoodSticks)
dev_set_color ('red')
dev_display (BadSticks)
String := 'Target length: ' + TargetLength$'.1f' + ' cm'
String[1] := 'Tolerance: -' + ToleranceLow$'.1f' + '/+' + ToleranceHigh$'.1f'
disp_message (WindowHandle, String, 'window', 12, 12, 'black', 'true')
for J := 0 to |IndicesGood| - 1 by 1
disp_message (WindowHandle, 'OK', 'image', 80, ColumnGood[J] - 10, 'green', 'false')
disp_message (WindowHandle, FishLength1Good[J]$'.1f' + 'cm', 'image', RowGood[J], ColumnGood[J] - 28, 'white', 'false')
endfor
for J := 0 to |IndicesBad| - 1 by 1
disp_message (WindowHandle, 'Not OK', 'image', 80, ColumnBad[J] - 30, 'red', 'false')
disp_message (WindowHandle, FishLength1Bad[J]$'.1f' + 'cm', 'image', RowBad[J], ColumnBad[J] - 28, 'white', 'false')
endfor
if (I != NumImages)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
endfor
set_system ('store_empty_region', StoreEmptyRegion)
posted on 2016-06-17 21:34 LongHuaiYu 阅读(518) 评论(0) 编辑 收藏 举报