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

目标:获取环形零件下半圆待检测区域。
待测图像如图,
Image01:
Halcon视觉检测——环形零件检测区域获取_第1张图片
Image02:
Halcon视觉检测——环形零件检测区域获取_第2张图片
Image03:
Halcon视觉检测——环形零件检测区域获取_第3张图片

代码:

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 ()

检测结果:
Halcon视觉检测——环形零件检测区域获取_第4张图片


Halcon视觉检测——环形零件检测区域获取_第5张图片

你可能感兴趣的:(Halcon)