您的位置:首页 > 其它

Halcon学习之基于形状的模板匹配(一)

2016-12-06 21:41 579 查看
* The following example shows a production line, which carries
* packages of soft cheese. The cheese packages consist of three
* types of flavor: cream, ham and paprika. During quality
* inspection each package is checked for its right content.
* Each has to contain 4 pieces of cream cheese ('Sahne'),
* 2 pieces of ham ('Schinken') and 2 pieces of paprika ('Paprika')
* - any deviation is reported otherwise. To obtain the number of
* each flavor, we use the shape-based matching feature. After we
* define the models, we count the occurrence of the models to
* get the actual number of the cheese flavors. Finally we give out
* the statistics for each inspection.
*
dev_close_window ()
dev_update_off ()
read_image (Image, 'food/soft_cheese_01')
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_set_line_width (2)
*
*
* As a first step, determine the shape models for the three flavors
rgb1_to_gray (Image, GrayImage)
*功能:将一个多边形存储为一个已填充区域。
gen_region_polygon_filled (Paprika, [77,84,203], [306,405,347])
erosion_circle (Paprika, Paprika, 3.5)
PaprikaCount := 2
gen_region_polygon_filled (Cream, [94,171,210], [424,488,363])
erosion_circle (Cream, Cream, 3.5)
CreamCount := 4
gen_region_polygon_filled (Ham, [185,285,225], [496,492,367])
erosion_circle (Ham, Ham, 3.5)
HamCount := 2
*
reduce_domain (GrayImage, Paprika, ImageReduced)
create_shape_model (ImageReduced, 'auto', 0, rad(360), rad(1.5), ['point_reduction_high','no_pregeneration'], 'use_polarity', [25,30], 5, ModelID1)
reduce_domain (GrayImage, Cream, ImageReduced)
create_shape_model (ImageReduced, 'auto', 0, rad(360), rad(1.5), ['point_reduction_high','no_pregeneration'], 'use_polarity', [25,50], 'auto', ModelID2)
reduce_domain (GrayImage, Ham, ImageReduced)
create_shape_model (ImageReduced, 'auto', 0, rad(360), rad(1.5), ['point_reduction_high','no_pregeneration'], 'use_polarity', [25,50], 'auto', ModelID3)
*
*
* Check for the occurrence of each cheese flavor by using the find model tool
ModelIDs := [ModelID1,ModelID2,ModelID3]
ColorIndex := ['red','magenta','yellow']
TypesIndex := ['P','C','H']
*
NumImages := 18
for Index := 1 to NumImages by 1
AnglesTypes := []
Types := []
read_image (Image, 'food/soft_cheese_' + Index$'02')
rgb1_to_gray (Image, GrayImage)
threshold (GrayImage, Region, 70, 255)
fill_up (Region, RegionFillUp)
opening_circle (RegionFillUp, RegionOpening, 3.5)
reduce_domain (GrayImage, RegionOpening, ImageReduced)
shape_trans (RegionOpening, Circle, 'outer_circle')
find_shape_models (ImageReduced, ModelIDs, 0, rad(360), 0.5, 8, 0.5, 'least_squares', 0, 0.8, Row, Column, Angle, Score, ModelIndex)
area_center (Circle, Area, RowObj, ColumnObj)
*
* display found matches and all over statistics
dev_clear_window ()
dev_display (Image)
if (|Score| == 0)
disp_message (WindowHandle, 'No Model found!', true, 25, 20, 'black', 'true')
else
CountModels := [0,0,0]//计数
for I := 0 to |Score| - 1 by 1
Types := [Types,TypesIndex[ModelIndex[I]]]
dev_set_color (ColorIndex[Mo
4000
delIndex[I]])
CountModels[ModelIndex[I]] := CountModels[ModelIndex[I]] + 1
get_shape_model_contours (Contour, ModelIDs[ModelIndex[I]], 1)
vector_angle_to_rigid (0, 0, 0, Row[I], Column[I], Angle[I], HomMat2D)
affine_trans_contour_xld (Contour, ContoursAffinTrans, HomMat2D)
dev_display (ContoursAffinTrans)
*
* remember order of flavors
*(0,0)点根据HotMat2D转换到新的坐标点,注意RowPiece与Row值相等
affine_trans_point_2d (HomMat2D, 0, 0, RowPiece, ColumnPiece)
*计算两点确定的直线与水平线的夹角
angle_lx (RowObj, ColumnObj, RowPiece, ColumnPiece, AngleTyp)
if (deg(AngleTyp) > 0)
AnglesTypes := [AnglesTypes,deg(AngleTyp)]
else
AnglesTypes := [AnglesTypes,360 + deg(AngleTyp)]
endif
endfor
*自定义函数
display_statistic (Circle, WindowHandle, CountModels, PaprikaCount, CreamCount, HamCount, AnglesTypes, Types)
*
endif
if (Index < NumImages)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
endfor
clear_shape_model (ModelID1)
clear_shape_model (ModelID2)
clear_shape_model (ModelID3)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  halcon