链接:https://pan.baidu.com/s/129sDTW-pBB1uFTchVNyfqw
提取码:dm51
halcon代码
* 1.创建模板 *********************************************
dev_set_draw ('margin')
dev_close_window ()
* 读取模板图片
read_image (ModelImage, './model.bmp')
get_image_size (ModelImage, Width, Height)
dev_open_window_fit_size (0, 0, Width, Height, -1, -1, WindowHandle)
dev_display (ModelImage)
set_system ('border_shape_models', 'false')
gen_rectangle1 (ModelRegion, 6, 6, 464, 216)
reduce_domain (ModelImage, ModelRegion, ImageReduced)
* 创建形状模板
create_shape_model (ImageReduced, 6, rad(0), rad(180), \
rad(0.5), ['point_reduction_low', 'no_pregeneration'],\
'use_polarity', [27, 55, 7], 4, ModelID)
get_shape_model_contours (ModelContours, ModelID, 1)
* 旋转模板区域
area_center (ModelRegion, Area, ModelRow, ModelColumn)
vector_angle_to_rigid (0, 0, 0, ModelRow, ModelColumn, 0, HomMat2D)
affine_trans_contour_xld (ModelContours, ContoursAffineTrans, HomMat2D)
* 显示模板区域
dev_display (ModelImage)
dev_display (ModelRegion)
dev_display (ContoursAffineTrans)
* 导入图片处理
read_image (TestImage, './img/大小头1.bmp')
get_image_size (TestImage, WidthTest, HeightTest)
dev_open_window_fit_size (0, 0, WidthTest, HeightTest, -1, -1, WindowHandle1)
dev_set_window (WindowHandle1)
dev_display (TestImage)
* 最小面积
MinArea := 35000
* 最小宽度
MinWidth := 100
* 最新高度
MinHeight := 350
* 2.循环测试图片 *********************************************
list_files ('./img', ['files','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
for Index := 0 to |ImageFiles| - 1 by 1
read_image (Image, ImageFiles[Index])
* a.模板匹配 ************************
find_shape_model (Image, ModelID, rad(0), rad(180), \
0.5, 3, 0.5,'least_squares', [6, 1], 0.75, \
Row, Column, Angle, Score)
dev_display (Image)
ResultList := []
AreaList := []
WidthList := []
HeightList := []
DrawRowList := []
DrawColList := []
gen_empty_region (InnerDefectList)
* b.循环处理药丸 ************************
for I:= 0 to |Score| - 1 by 1
result := ''
area := -1
width := -1
height := -1
* 获取搜索结果并调整显示结果
hom_mat2d_identity (HomMat2D)
hom_mat2d_rotate (HomMat2D, Angle[I], 0, 0, HomMat2D)
hom_mat2d_translate (HomMat2D, Row[I], Column[I], HomMat2D)
affine_trans_contour_xld (ModelContours, TranContours, HomMat2D)
* dev_display (TranContours)
* 调整药丸外接区域
gen_region_contour_xld (TranContours, Region, 'filled')
union1 (Region, Region)
shape_trans (Region, RegionTrans, 'convex')
fill_up (RegionTrans, RegionFillUp)
* 寻找固定矩形
shape_trans (RegionFillUp, RegionTransRect, 'rectangle1')
dilation_rectangle1 (RegionTransRect, RegionDilation, 30, 30)
reduce_domain (Image, RegionDilation, ImageReduced1)
rgb1_to_gray (ImageReduced1, GrayImage)
smallest_rectangle1 (RegionDilation, Row1, Column1, Row2, Column2)
DrawRowList := [DrawRowList, Row1]
DrawColList := [DrawColList, Column1]
* 寻找固定矩形内部 胶囊区域
binary_threshold (GrayImage, Region1, \
'max_separability', 'light', UsedThreshold)
dilation_circle (Region1, RegionDilation1, 3.5)
fill_up (RegionDilation1, RegionFillUp1)
connection (RegionFillUp1, ConnectedRegions)
select_shape_std (ConnectedRegions, SelectedRegions, 'max_area', 70)
shape_trans (SelectedRegions, RegionTrans1, 'convex')
reduce_domain (ImageReduced1, RegionTrans1, ImagePack)
* 黄颜色筛选 H/S
decompose3 (ImagePack, ImageR, ImageG, ImageB)
trans_from_rgb (ImageR, ImageG, ImageB, \
ImageH, ImageS, ImageI, 'hsv')
threshold (ImageS, HightS, 60, 255)
reduce_domain (ImageH, HightS, HueHighS)
threshold (HueHighS, Yellow, 20, 50)
* 获取药丸区域
connection (Yellow, ImageYellow)
select_shape (ImageYellow, SelectedRegions1, 'area', 'and', 1000, 999999999)
RegionTrans := RegionTrans1
erosion_circle (RegionTrans, RegionErosion, 7.5)
intersection (SelectedRegions1, RegionErosion, RegionIntersection)
connection (RegionIntersection, ConnectedRegions1)
select_shape_std (ConnectedRegions1, SelectedRegions2, 'max_area', 70)
dilation_circle (SelectedRegions2, RegionDilation2, 1.5)
fill_up (RegionDilation2, RegionCapsule)
* 检测是否空粒
count_obj (RegionCapsule, CapsuleCount)
if(CapsuleCount == 0)
result := 'Empty'
ResultList := [ResultList, result]
AreaList := [AreaList, -1]
WidthList := [WidthList, -1]
HeightList := [HeightList, -1]
continue
endif
* 检测胶囊区域 面积与宽度高度
erosion_circle (RegionCapsule, InnerCapsule, 19.5)
region_features (InnerCapsule, 'area', Area)
region_features (InnerCapsule, 'width', Width)
region_features (InnerCapsule, 'height', Height)
AreaList := [AreaList, Area]
WidthList := [WidthList, Width]
HeightList := [HeightList, Height]
if(Area < MinArea or Width < MinWidth or Height < MinHeight)
result := 'ShapeNG'
ResultList := [ResultList, result]
continue
endif
* stop()
* 检测内部区域是否有气泡
reduce_domain (ImagePack, InnerCapsule, InnerImage)
rgb1_to_gray (InnerImage, InnerGray)
mean_image (InnerGray, ImageMean, 7, 7)
dyn_threshold (InnerGray, ImageMean, RegionDynThresh, 8, 'dark')
threshold (InnerGray, threRegion, 0, 128)
union2 (RegionDynThresh, threRegion, RegionUnion)
connection (RegionUnion, ConnectedRegions2)
select_shape (ConnectedRegions2, InnerDefect, 'area', 'and', 30, 999999)
count_obj (InnerDefect, InnerNgCount)
union2 (InnerDefectList, InnerDefect, InnerDefectList)
if(InnerNgCount > 0)
result := 'InnerNG'
ResultList := [ResultList, result]
continue
endif
* OK
result := 'OK'
ResultList := [ResultList, result]
endfor
* 显示结果
dev_clear_window ()
dev_display (Image)
disp_message (WindowHandle1, ResultList, 'image', DrawRowList, DrawColList-10, 'black', 'true')
disp_message (WindowHandle1, 'area:' + AreaList, 'image', DrawRowList+20, DrawColList-10, 'black', 'true')
disp_message (WindowHandle1, 'Width:' + WidthList, 'image', DrawRowList+40, DrawColList-10, 'black', 'true')
disp_message (WindowHandle1, 'Height:' + HeightList, 'image', DrawRowList+60, DrawColList-10, 'black', 'true')
disp_message (WindowHandle1, ImageFiles[Index], 'window', 200, 10, 'black', 'true')
dev_display (InnerDefectList)
stop()
endfor