发布时间 : 星期一 文章Halcon检测圆更新完毕开始阅读
Halcon检测圆(blob外接圆法) (转) 原图如下:
检测结果如下:
代码如下:
dev_close_window ()
dev_update_window ('off')
read_image (Image, 'C:/Documents and Settings/Administrator/桌面/样品图.jpg')
get_image_size (Image, Width, Height)
dev_open_window_fit_image (Image, 0, 0, Width, Width, WindowID) set_display_font (WindowID, 12, 'Courier', 'true', 'false') dev_set_draw ('margin') dev_set_line_width (4) dev_display (Image)
* 定位检测区域
threshold (Image, Regions, 0, 45)
connection(Regions,ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 5432.13, 20949.6)
shape_trans (SelectedRegions, RegionTrans, 'outer_circle') area_center (RegionTrans, Area, Row, Column)
reduce_domain(Image,RegionTrans,ReducedImage)
*检测污点
mean_image (ReducedImage, ImageMean, 31, 31)
dyn_threshold (Image, ImageMean, RegionDynThresh, 7, 'dark') connection(RegionDynThresh,ConnctedDynRegion)
select_shape (ConnctedDynRegion, SelectedRegions, 'area', 'and', 10, 1000)
union1(SelectedRegions,RegionUnion)
dilation_circle(RegionUnion , RegionDilation , 3.5 ) skeleton(RegionDilation , Skeleton) connection(Skeleton, Errors) dev_display(Errors)
set_display_font(WindowID,16,'mono','true','false') * 计算半径
Radius :=sqrt( Area / 2 * 3.1415926) dev_display(RegionTrans) dev_set_draw ('fill')
gen_circle(dot,Row,Column,5) dev_display(dot)
ResultText := 'x =' + Column +'y = ' + Row + ' radius =' + Radius*2 set_tposition(WindowID,Row,Column) write_string(WindowID,ResultText)