方法使用的是论文
基于HALCON图像处理的粘连零件颗粒计数方法研究
测试使用图片
dev_clear_window()
*读取图片
read_image (Image, 'C:/Users/Administrator/Desktop/new/QQ图片20161212193015.jpg')
get_image_size (Image, Width, Height)
*自动阈值分割
bin_threshold (Image, Region)
*分割后选择的是所需的背景,所以求反一次
complement (Region, RegionComplement)
dev_display(Image)
* dev_display(RegionComplement)
*做一次闭运算
closing_circle(RegionComplement, RegionClosing, 3.5)
*空洞填充
fill_up_shape (RegionClosing, RegionFillUp, 'area', 1, 100)
*欧式距离函数的距离变换
distance_transform (RegionFillUp, DistanceImage, 'octagonal', 'true', Width, Height)
*int4转byte
convert_image_type (DistanceImage, ImageConverted, 'byte')
*图像取反
invert_image (ImageConverted, ImageInvert)
*增加对比度
scale_image_max (ImageInvert, ImageScaleMax)
*高斯平滑处理
gauss_image (ImageScaleMax, ImageGauss, 11)
*分水岭算法
watersheds_threshold (ImageGauss, Basins, 10)
*计数统计为29个
count_obj (Basins, Number)
*发现有小区域面积干扰,重新选择区域
select_shape (Basins, SelectedRegions, 'area', 'and', 5267.61, 119050)
*获取到正确的数目25
count_obj (SelectedRegions, Number1)