HALCON示例程序inspect_bottle_mouth.hdev玻璃瓶口缺陷检测
示例程序源码(加注释)
-  定义变量并初始化 
 SmoothX := 501
 ThresholdOffset := 25
 MinDefectSize := 50
 PolarResolution := 640
 RingSize := 70
 get_system (‘store_empty_region’, StoreEmptyRegion)
 set_system (‘store_empty_region’, ‘false’)
 read_image (Image, ‘bottles/bottle_mouth_01’)
-  关于显示类函数解释 
 dev_update_off ()
 dev_close_window ()
 dev_close_window ()
 dev_open_window_fit_image (Image, 0, 0, 640, 512, WindowHandle1)
 set_display_font (WindowHandle1, 16, ‘mono’, ‘true’, ‘false’)
 dev_display (Image)
 dev_set_draw (‘margin’)
 dev_set_line_width (3)
 dev_open_window_fit_size (0, 648, RingSize, PolarResolution, 150, 512, WindowHandle)
 dev_set_draw (‘margin’)
 dev_set_line_width (3)
 dev_set_color (‘red’)
 for Index := 1 to 16 by 1
 read_image (Image, ‘bottles/bottle_mouth_’ + Index$’.02’)- 使用形态学检测瓶口
- 使用多个阈值来分割单通道图像
 auto_threshold (Image, Regions, 2)
- 选取灰度值最小的分割区域
 select_obj (Regions, DarkRegion, 1)
- 开运算
 opening_circle (DarkRegion, RegionOpening, 3.5)
- 闭运算
 closing_circle (RegionOpening, RegionClosing, 25.5)
- 填充孔洞
 fill_up (RegionClosing, RegionFillUp)
- 提取区域边界
 boundary (RegionFillUp, RegionBorder, ‘outer’)
- 膨胀边界
 dilation_circle (RegionBorder, RegionDilation, 3.5)
- 将膨胀边界提取出
 reduce_domain (Image, RegionDilation, ImageReduced)
- 通过拟合圆形找到瓶口中心
- 关于edges_sub_pix算子解释传送门
 edges_sub_pix (ImageReduced, Edges, ‘canny’, 0.5, 20, 40)
- 分割XLD轮廓,算子解释传送门
 segment_contours_xld (Edges, ContoursSplit, ‘lines_circles’, 5, 4, 2)
- union_cocircular_contours_xld - 计算属于同一个圆的轮廓的并集。
- union_cocircular_contours_xld(轮廓:合并的轮廓:两个圆弧的最大角距离,两个圆弧的最大重叠,连接线与圆弧切线之间的最大角度,两个圆弧之间间隙的最大长度,两个圆弧的最大半径差,两个圆弧的最大中心距离,是否合并没有拟合圆的小轮廓,迭代次数:)
 union_cocircular_contours_xld (ContoursSplit, UnionContours, 0.9, 0.5, 0.5, 200, 50, 50, ‘true’, 1)
- XLD轮廓长度
 length_xld (UnionContours, Length)
- sort_index - 对元组的元素进行排序并返回已排序元组的索引。取最长轮廓
 select_obj (UnionContours, LongestContour, sort_index(Length)[|Length| - 1] + 1)
- 拟合圆形
 fit_circle_contour_xld (LongestContour, ‘ahuber’, -1, 0, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
- 使用极坐标变换将瓶口圆环展开成矩形
- 画圆
 gen_circle (Circle, Row, Column, Radius)
- 膨胀
 dilation_circle (Circle, RegionDilation, 5)
- 腐蚀
 erosion_circle (Circle, RegionErosion, RingSize - 5)
- 求不同
 difference (RegionDilation, RegionErosion, RegionDifference)
- 缩减定义域
 reduce_domain (Image, RegionDifference, ImageReduced)
- polar_trans_image_ext - 将图像进行极坐标变换。
- polar_trans_image_ext(图片:极坐标变换图,弧中心行坐标,弧中心列坐标,起始角度,结束角度,起始半径,结束半径,宽度,高度,差值方法:)
 polar_trans_image_ext (ImageReduced, ImagePolar, Row, Column, 0, rad(360), Radius - RingSize, Radius, PolarResolution, RingSize, ‘nearest_neighbor’)
- 使用动态阈值寻找缺陷
 scale_image_max (ImagePolar, ImageScaleMax)
 mean_image (ImageScaleMax, ImageMean, SmoothX, 3)
 dyn_threshold (ImageScaleMax, ImageMean, Regions1, 55, ‘not_equal’)
 connection (Regions1, Connection)
 select_shape (Connection, SelectedRegions, ‘height’, ‘and’, 9, 99999)
- 消除噪声区域
 closing_rectangle1 (SelectedRegions, RegionClosing1, 10, 20)
 union1 (RegionClosing1, RegionUnion)
- 反极坐标变换
 polar_trans_region_inv (RegionUnion, XYTransRegion, Row, Column, 0, rad(360), Radius - RingSize, Radius, PolarResolution, RingSize, 1280, 1024, ‘nearest_neighbor’)
- 显示结果
 dev_set_window (WindowHandle1)
 dev_display (Image)
 dev_set_color (‘blue’)
 dev_display (RegionDifference)
 dev_set_color (‘red’)
 dev_display (XYTransRegion)
- 显示极坐标变换区域
 dev_set_window (WindowHandle)
- 围绕图像中心旋转图像
 rotate_image (ImagePolar, ImageRotate, 90, ‘constant’)
 dev_display (ImageRotate)
 count_obj (RegionUnion, Number)
 if (Number > 0)- 镜像区域
 mirror_region (RegionUnion, RegionMirror, ‘diagonal’, PolarResolution)
 mirror_region (RegionMirror, RegionMirror, ‘row’, PolarResolution)
 dev_display (RegionMirror)
 disp_message (WindowHandle1, ‘Not OK’, ‘window’, -1, -1, ‘red’, ‘false’)
 
- 镜像区域
 else 
 disp_message (WindowHandle1, ‘OK’, ‘window’, -1, -1, ‘forest green’, ‘false’)
 endif
 if (Index < 16)
 disp_continue_message (WindowHandle1, ‘black’, ‘true’)
 stop ()
 endif
 endfor
-  恢复系统参数 
 set_system (‘store_empty_region’, StoreEmptyRegion)
处理思路
这个例子是主要讲解了使用极坐标变换进行缺陷检测的例子。
 auto_threshold 、select_obj ,自动阈值分割。
 boundary提取区域边缘edges_sub_pix、segment_contours_xld、union_cocircular_contours_xld、length_xld、select_obj 、fit_circle_contour_xld 边界的提取、分割、合并、求长、选取、拟合。
 polar_trans_image_ext极坐标变换polar_trans_region_inv反极坐标变换
 mirror_region镜像区域
后记
大家有什么问题可以向我提问哈,我看到了第一时间回复,希望在学习的路上多多结交良师益友。