例程实现电路板上接头个数的计算,图片使用halcon自带的。
使用了blob分析方法,涉及的函数为blob分析中经典组合的几个。
下面是具体代码
halcon工业视觉用blob分析方法计算电路板接头个数
- read_image (Image, '1.png')
- get_image_size (Image, Width, Height)
- dev_close_window ()
- dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
- dev_set_line_width (3)
- dev_set_draw ('margin')
- dev_display (Image)
- stop()
- Num := 4
- for I := 0 to Num-1 by 1
- file := I
- d'+'.png'
- read_image (Bond, file)
- dev_display (Bond)
- *计算图像最大最小灰度值
- min_max_gray (Bond, Bond, 0, Min, Max, Range)
- *用计算出的最大灰度值进行分割
- threshold (Bond, Bright, Max - 80, 255)
- shape_trans (Bright, Die, 'rectangle2')
- dev_display (Die)
- *提取出图像中模块
- reduce_domain (Bond, Die, DieGrey)
- min_max_gray (Die, Bond, 0, Min, Max, Range)
- *提取出连接头所在黑色区域
- threshold (DieGrey, Wires, 0, Min + 30)
- *填充区域内满足一定条件的洞
- fill_up_shape (Wires, WiresFilled, 'area', 1, 100)
- *开运算,去除毛刺等。粗略定位接头
- opening_circle (WiresFilled, Balls, 9.5)
- connection (Balls, SingleBalls)
- *筛选出非接头区域
- select_shape_std (SingleBalls, Rect, 'rectangle1', 90)
- *计算出接头与非接头区域差值,表示为接头区域
- difference (SingleBalls, Rect, IntermediateBalls)
- gen_empty_region (Forbidden)
- expand_gray (IntermediateBalls, Bond, Forbidden, RegionExpand, 4, 'image', 6)
- opening_circle (RegionExpand, RoundBalls, 15.5)
- sort_region (RoundBalls, FinalBalls, 'first_point', 'true', 'column')
- smallest_circle (FinalBalls, Row, Column, Radius)
- Num := |Radius|
- dev_display (Bond)
- dev_display (RoundBalls)
- dev_disp_text ('接头个数:'+Num
- d', 'window', 20, 20, 'black', [], [])
- stop ()
- endfor
复制代码
|