Halcon视觉检测——环形零件检测区域获取

目标:获取环形零件下半圆待检测区域。
待测图像如图,
Image01:
这里写图片描述
Image02:
这里写图片描述
Image03:
这里写图片描述

代码:

list_files ('待测图像/Image', ['files','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
for Index := 0 to |ImageFiles| - 1 by 1
           read_image (Image, ImageFiles[Index])
           * Image Acquisition 01: Do something
try
           FindROI (Image, RIO0, 200)      
           dev_display(Image)
           dev_set_color('green')
           dev_set_draw('margin')
           dev_display(RIO0)
           stop()   
catch (Exception)
dev_clear_window ()
disp_message (3600, Exception, 'window', 50, 12, 'red', 'true')
dev_get_exception_data (Exception, 'error_msg', ErrorMsg)
disp_message (3600, 'Error message: ' + ErrorMsg, 'window', 250, 12, 'red', 'true')
disp_continue_message (3600, 'black', 'true')
stop ()

endtry

endfor

stop()

FindROI ()函数:
FindROI (Image, RIO, Offset)

get_image_size (Image, Width, Height)
threshold (Image, Region1, 20, 150)
connection (Region1, ConnectedRegions2)
select_shape_std (ConnectedRegions2, SelectedRegions1, 'max_area', 30)
closing_circle (SelectedRegions1, RegionClosing, 10)
erosion_circle (RegionClosing, RegionErosion, 10)
gen_contour_region_xld (RegionErosion, Contours, 'border')
gen_region_contour_xld (Contours, Region, 'margin')
dilation_circle (Region, RegionDilation1, 20)
skeleton (RegionDilation1, Skeleton)
gen_contour_region_xld (Skeleton, Contours1, 'border')
fit_circle_contour_xld (Contours1, 'atukey', -1, 2, 0, 10, 1, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
gen_circle_contour_xld (OutCircle2, Row[0], Column[0], Radius[0]-50, 0, 4 * acos(0), 'positive', 1)
gen_region_contour_xld (OutCircle2, Region2, 'filled')
reduce_domain (Image, Region2, ImageReduced1)
threshold (ImageReduced1, Region3, 0, 20)
connection (Region3, ConnectedRegions1)
select_shape_std (ConnectedRegions1, SelectedRegions2, 'max_area', 30)
closing_circle (SelectedRegions2, RegionClosing2, 10)
gen_contour_region_xld (RegionClosing2, Contours4, 'border')
fit_circle_contour_xld (Contours4, 'atukey', -1, 2, 0, 10, 1, Row1, Column1, Radius1, StartPhi1, EndPhi1, PointOrder1)
gen_circle_contour_xld (OutCircleSmall, Row1[0], Column1[0], Radius1[0]+5, 0, 4 * acos(0), 'positive', 1)
* *对应Offset为195,其他图库对应为220
gen_circle_contour_xld (OutCircleBig, Row1[0], Column1[0], Radius1[0]+Offset, 0, 4 * acos(0), 'positive', 1)
gen_region_contour_xld (OutCircleSmall, CircleSmall, 'filled')
gen_region_contour_xld (OutCircleBig, CircleBig, 'filled')
difference (CircleBig, CircleSmall, RegionIntersection2)
gen_region_line (RegionLines, Row1[0], Column1[0]-Width/2, Row1[0], Column1[0]+Width/2)
difference (RegionIntersection2, RegionLines, RegionDifference)
connection (RegionDifference, ConnectedRegions)
select_shape (ConnectedRegions, RIO, 'row', 'and', Row1[0], 1000)
dev_display (Image)
dev_set_color ('yellow')
dev_display (RIO)
return ()

检测结果:
这里写图片描述


这里写图片描述

  • 0
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值