小哥哥小姐姐觉得有用点个赞呗!
HALCON示例程序rim.hdev轮圈孔检测提取字符
示例程序源码(加注释)
- 关于显示类函数解释
dev_update_off ()
dev_close_window ()
dev_open_window (0, 0, 768, 576, ‘black’, WindowID)
set_display_font (WindowID, 16, ‘mono’, ‘true’, ‘false’)
read_image (Rim, ‘rim’)
dev_display (Rim)
disp_continue_message (WindowID, ‘black’, ‘true’)
stop () - 二值化
threshold (Rim, Dark, 0, 128)
dev_display (Rim)
dev_set_color (‘green’)
dev_set_draw (‘fill’)
dev_display (Dark)
disp_continue_message (WindowID, ‘black’, ‘true’)
stop () - 分割连通域
connection (Dark, DarkRegions) - 使用圆度与面积筛选区域
select_shape (DarkRegions, Circles, [‘circularity’,‘area’], ‘and’, [0.85,50], [1.0,99999])
dev_display (Rim)
dev_set_colored (12)
dev_display (Circles)
disp_continue_message (WindowID, ‘black’, ‘true’)
stop () - 使用圆形膨胀
dilation_circle (Circles, ROIOuter, 8.5) - 使用圆形腐蚀
erosion_circle (Circles, ROIInner, 8.5) - 求不同
difference (ROIOuter, ROIInner, ROI) - 合并区域
union1 (ROI, ROIEdges)
dev_display (Rim)
dev_set_draw (‘margin’)
dev_set_line_width (3)
dev_set_color (‘green’)
dev_display (ROIEdges)
disp_continue_message (WindowID, ‘black’, ‘true’)
stop () - 缩减定义域,求取边缘
reduce_domain (Rim, ROIEdges, RimReduced)
edges_sub_pix (RimReduced, Edges, ‘canny’, 4, 20, 40) - 筛选边缘
select_contours_xld (Edges, RelEdges, ‘length’, 30, 999999, 0, 0)
dev_display (Rim)
dev_set_colored (12)
dev_display (RelEdges)
disp_continue_message (WindowID, ‘black’, ‘true’)
stop () - 拟合椭圆
fit_ellipse_contour_xld (RelEdges, ‘ftukey’, -1, 2, 0, 200, 3, 2, Row, Column, Phi, Ra, Rb, StartPhi, EndPhi, PointOrder)
display_ellipses (Rim, Row, Column, Phi, Ra, Rb, WindowID)
disp_continue_message (WindowID, ‘black’, ‘true’)
stop () - 使用高斯平滑滤波
gauss_filter (Rim, RimGauss, 11) - 动态阈值分割区域
dyn_threshold (Rim, RimGauss, SmallAndDarkerRegion, 5, ‘dark’)
dev_display (Rim)
dev_set_draw (‘fill’)
dev_set_color (‘green’)
dev_display (SmallAndDarkerRegion)
disp_continue_message (WindowID, ‘black’, ‘true’)
stop () - 分割连通域
connection (SmallAndDarkerRegion, SmallAndDarker) - 通过面积筛选区域
select_shape (SmallAndDarker, CharCandidates, ‘area’, ‘and’, 40, 400) - 使用长短轴筛选区域
select_shape (CharCandidates, PossibleChar, [‘ra’,‘rb’], ‘and’, [10,5], [20,30])
dev_display (Rim)
dev_set_colored (12)
dev_display (PossibleChar)
disp_continue_message (WindowID, ‘black’, ‘true’)
stop () - 合并区域
union1 (PossibleChar, ROI) - 使用圆形元素进行闭运算
closing_circle (ROI, CharRegion, 17.5) - 分割连通域
connection (CharRegion, CharBlocks)
dev_display (Rim)
dev_set_draw (‘margin’)
dev_display (CharBlocks)
disp_continue_message (WindowID, ‘black’, ‘true’)
stop () - 使用面积对区域进行筛选
select_shape (CharBlocks, CharRegion, ‘area’, ‘and’, 400, 99999) - 对区域进行型转变换,求取最小外接矩形
shape_trans (CharRegion, ROIChar, ‘rectangle2’)
dev_display (Rim)
dev_set_color (‘green’)
dev_display (ROIChar)
disp_continue_message (WindowID, ‘black’, ‘true’)
stop ()
display_ellipses (Rim, Row, Column, Phi, Ra, Rb, WindowID) - 求取交集
intersection (CharCandidates, ROIChar, Characters)
dev_set_colored (12)
dev_display (Characters) - 求最小外接矩形
smallest_rectangle2 (ROIChar, RowChar, ColumnChar, PhiChar, Length1Char, Length2Char) - 创建标准转换矩阵
hom_mat2d_identity (HomMat2DIdentity) - 旋转
hom_mat2d_rotate (HomMat2DIdentity, rad(180) - PhiChar, RowChar, ColumnChar, HomMat2DRotate) - 平移
affine_trans_region (Characters, CharRotated, HomMat2DRotate, ‘constant’) - 进行仿射变换
affine_trans_image (Rim, RimRotated, HomMat2DRotate, ‘weighted’, ‘false’)
dev_open_window (0, 800, 192, 144, ‘black’, WindowChar) - 合并区域
union1 (CharRotated, CharRotatedAll) - 求区域最小外接矩形
smallest_rectangle1 (CharRotatedAll, Row1, Column1, Row2, Column2)
ZoomHeight := Row2 - Row1 + 1
ZoomWidth := Column2 - Column1 + 1
ZoomFactor := min([768 / ZoomWidth,576 / ZoomHeight])
ZoomRow := (Row1 + Row2) / 2
ZoomColumn := (Column1 + Column2) / 2
dev_set_part (ZoomRow - 576 / ZoomFactor / 2, ZoomColumn - 768 / ZoomFactor / 2, ZoomRow + 576 / ZoomFactor / 2 - 1, ZoomColumn + 768 / ZoomFactor / 2 - 1)
dev_display (RimRotated)
dev_set_draw (‘fill’)
dev_set_colored (12)
dev_display (CharRotated)
处理思路
这个例子是主要讲解了使用blob方法进行孔洞测量;使用动态阈值进行字符区域提取。
后记
大家有什么问题可以向我提问哈,我看到了第一时间回复,希望在学习的路上多多结交良师益友。