当前例程实现约片上指定区域,胶囊是否有填充,也就是检查胶囊的有无。
使用了blob分析中极为常用的算子组合。
第一次遇到陌生的算子一定要结合算子的效果记忆算子,结合帮助手册查看算子更多具体信息。
例程思路是:先载入一图为参考,通过特征选择提取药片,将药片旋转平移平到固定坐标与角度,再生成检测区域。
这样后续图片处理中,都可以提取出药片,旋转平移平到前面的固定坐标与角度,在前面检测区域中检测药片有无。
可以运行下面的代码,
查看代码执行结果与效果。
dev_close_window ()
read_image (Image,'blister/blister_reference')
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
dev_display (Image)
stop()
count_channels (Image, Channels)
decompose3 (Image, R, G, B)
access_channel (Image, Image1, 1)
threshold (Image1, Region, 90, 255)
shape_trans (Region, RegionTrans, 'convex')
area_center (RegionTrans, Area, Row, Column)
orientation_region (RegionTrans, Phi)
vector_angle_to_rigid (Row, Column, Phi, Row, Column, 0, HomMat2D)
affine_trans_image (Image1, ImageAffineTrans, HomMat2D, 'constant', 'false')
draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2)
gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)
affine_trans_region (RegionTrans, RegionAffineTrans, HomMat2D, 'nearest_neighbor')
area_center (RegionAffineTrans, Area, RowRef, ColumnRef)
orientation_region (RegionAffineTrans, PhiRef)
PhiRef := PhiRef+rad(180)
dev_set_draw ('margin')
dev_display(Image1)
dev_display (Rectangle)
stop()
ImageNum:=6
for i :=1 to ImageNum by 1
file := 'blister/blister_'+i$'02'
read_image (Image,file)
threshold (Image, Region1, 90, 255)
connection (Region1, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 10000, 999999)
shape_trans (SelectedRegions, RegionTrans1, 'convex')
area_center (RegionTrans1, Area1, Row3, Column3)
orientation_region (RegionTrans1, Phi1)
vector_angle_to_rigid (Row3, Column3, Phi1, RowRef, ColumnRef, PhiRef, HomMat2D1)
affine_trans_image (Image, ImageAffineTrans1, HomMat2D1, 'constant', 'false')
dev_display(ImageAffineTrans)
dev_display (Rectangle)
reduce_domain (Image, Rectangle, ImageReduced)
decompose3 (ImageReduced, R, G, B)
var_threshold (B, Region2, 7, 7, 0.2, 2, 'dark')
connection (Region2, ConnectedRegions1)
closing_rectangle1 (ConnectedRegions1, RegionClosing, 3, 3)
fill_up (RegionClosing, RegionFillUp)
select_shape (RegionFillUp, SelectedRegions1, 'area', 'and', 1000, 99999)
dev_display (ImageAffineTrans1)
count_obj(SelectedRegions1, Number)
if(Number == 0)
dev_disp_text ('not filled', 'window', 12, 12, 'black', [], [])
else
dev_display (SelectedRegions1)
endif
stop()
endfor
帖子为下面halcon视频教程的部分内容,
可以下载教程观看学习。
上位机VC MFC程序开发精典实例大全源码与视频讲解配套下载408例 经历1年的编程与录制点击进入查看
如果您认可,可联系功能定制! 如果您着急,充值会员可直接联系发您资料!
|