Halcon找圆系列(3)找金属表面的圆孔

隐身守侯 提交于 2020-08-10 21:05:19

我在Halcon学习网发了一个帖子:

请教工件的金属表面圆孔的识别与定位

http://www.ihalcon.com/read-15016-1.html

 

解决方案有了,源码如下:

dev_update_off ()
dev_close_window ()

*读图像
read_image (Image, 'D:/pic/202006042055044969.jpg')

*彩色转灰度图
count_channels (Image, Channels)
if (Channels == 3)
    rgb1_to_gray (Image, Image)
*真彩色转灰度图
elseif (Channels == 4)
    decompose4 (Image, ImageR, ImageG, ImageB, ImageA)
    compose3 (ImageR, ImageG, ImageB, MultiChannelImage)
    rgb1_to_gray (MultiChannelImage, Image)
endif

*获取图像宽高
get_image_size (Image, Width, Height)
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_display (Image)

*动态二值化
D := 30
mean_image(Image, Mean, D*2+1, D*2+1)
dyn_threshold(Image, Mean, Seg, 5, 'dark')
fill_up(Seg, SegFillUp)
connection(SegFillUp, segConnect)

*找圆
select_shape(segConnect, SelectedRegions, ['area','circularity'], 'and', [500, 0.7], [20000, 1])

*求出最小外接圆的半径和中心点
smallest_circle(SelectedRegions, Row, Column, Radius)

*求出圆的面积和中心点
area_center(SelectedRegions, Area, Row1, Column1)

*画圆
dev_display(Image)
gen_circle(Circles, Row, Column, Radius)
dev_display(Circles)

*数量统计
count_obj(SelectedRegions, Number)
if (Number < 1)
    disp_message (WindowHandle, '未找到', 'window', 12, 12, 'black', 'true')
    return()
endif

*结果显示
for i:=1 to Number by 1
   select_obj (SelectedRegions, ObjectSelected, i)
   r := Row[i - 1]
   c := Column[i - 1]
   rds:= Radius[i - 1]
   disp_message (WindowHandle, '圆心:Row:'+ r + ' Col:' + c, 'window', 12, 12, 'black', 'true')
    disp_message (WindowHandle, '半径:'+ rds, 'window', 52, 12, 'black', 'true')
endfor

stop()

 

 

---

参考官方例程

smallest_circle.hdev
measure_ring.hdev
dyn_threshold.hdev
ball.hdev


 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!