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)
————————————————
原文链接:https://blog.csdn.net/LearningAlways/article/details/53588931
上述代码不适应新的Halcon版本,想查看新Halcon版本 对应代码和过程总结及函数说明请付积分查看
ev_clear_window()
*读取图片
read_image (Image, 'E:/程序/程序/halcon/识别粘连物体/粘连图像.JPG')
get_image_size (Image, Width, Height)
*自动阈值分割
binary_threshold (Image, Region, 'max_separability', 'dark', UsedThreshold)
*分割后选择的是所需的背景,所以求反一次
complement (Region, RegionComplement)
dev_display(Image)
*做一次闭运算
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_filter (ImageScaleMax, ImageGauss, 11)
*分水岭算法
watersheds_threshold (ImageGauss, Basins, 10)
*计数统计为29个
count_obj (Basins, Number)
*发现有小区域面积干扰,重新选择区域
select_shape (Basins, SelectedRegions, 'area', 'and', 5267.61, 119050)
*获取到正确的数目26
count_obj (SelectedRegions, Number1)
函数说明
- 读取图像 read_image (输出变量:图像Object,输入变量:图像地址)
- 获取对象尺寸get_image_size (输入变量:Image, 输出变量:Width, 输出变量:Height)
- 用二进制阈值来分割图像 binary_threshold (输入变量:Image , 输出变量:处理后的区域( Region ), 输入变量:Method:分割方法('max_separability':最大限度的可分性, 'smooth_histo':直方图平滑) ,输入变量: LightDark提取的是黑色部分 'dark'还是白色部分'light' , 输出变量: UsedThreshold返回binary_threshold 使用的阈值)
以下内容需要兑换: