前言
通常情况下轴检测时,通常会检测轴的各个阶段的长度。但是由于各种原因,在轴断面的区域现实不明显,无法正确提取,这时候需要根据轴断面的突出部分进行检测,但是由于部分轴的粗轴和细轴区域的宽度差距相当接近,所以就需要通过另外的处理,将轴的断面进行单独提取
1.halcon程序
* Image Acquisition 01: Code generated by Image Acquisition 01
list_files ('D:/2024Work/work/4.12断层检测', ['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 1read_image (Image, ImageFiles[Index])*gen_rectangle1 (ROI_0, 707.927, 631.228, 1072.01, 1398.08)*gen_rectangle1 (ROI_0, 735.583, 1488.93, 1089.37, 2298.93)gen_rectangle1 (ROI_0, 761.136, 824.068, 1021.81, 1090.71)reduce_domain (Image, ROI_0, ImageReduced)*矫正图像binary_threshold (ImageReduced, Region, 'max_separability', 'dark', UsedThreshold)erosion_circle (Region, RegionErosion,1)smallest_rectangle2 (RegionErosion, Row, Column, Phi2, Length1, Length2)vector_angle_to_rigid (Row, Column, Phi2, Row, Column, 1.5707963, HomMat2D)affine_trans_image (ImageReduced, ImageAffineTrans, HomMat2D, 'constant', 'false')reduce_domain (ImageAffineTrans, ROI_0, ImageReduced2)*获取断面下半部分binary_threshold (ImageReduced2, Region2, 'smooth_histo', 'dark', UsedThreshold1)*将上下断面进行联合shape_trans (Region2, RegionTrans, 'convex')*取区域的最大内接矩形inner_rectangle1 (RegionTrans, Row1, Column1, Row2, Column2)*创建矩形区域。根据实际情况,可以对内接矩形的左右列坐标进行修改,使得较粗的部分可以较多*较细的部分尽可能的少gen_rectangle1 (Rectangle1, Row1, Column1-4, Row2, Column2-3)*求取差值,将轴区域与内接矩形区域求差值,可得粗轴外轮廓difference (Region2, Rectangle1, RegionDifference)*剔除细轴多余的区域opening_circle (RegionDifference, RegionOpening1, 1)connection (RegionOpening1, ConnectedRegions)select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 200.96, 10000)union1 (SelectedRegions, RegionUnion)*进行横向闭运算,将粗轴进行相连接。closing_rectangle1 (RegionUnion, RegionClosing, 1000000000, 1)opening_circle (RegionClosing, RegionOpening, 5)connection (RegionOpening, ConnectedRegions1) select_shape (ConnectedRegions1, SelectedRegions1, 'area', 'and', 10833.3, 363400.6)shape_trans (SelectedRegions1, RegionTrans1, 'convex')*获取断面上半部分*做差值提取到断面上半部分,剔除多余的噪点difference (Region2, RegionTrans1, RegionDifference2)connection (RegionDifference2, ConnectedRegions3) select_shape (ConnectedRegions3, SelectedRegions3, 'area', 'and', 5196.22, 500000)opening_circle (SelectedRegions3, RegionOpening2, 3.5)*上下部分*对上下部分进行膨胀取交集dilation_circle (RegionOpening2, RegionDilation2, 1)dilation_circle (RegionTrans1, RegionDilation3, 3)intersection (RegionDilation2, RegionDilation3, RegionIntersection1)*结果显示skeleton (RegionIntersection1, Skeleton)junctions_skeleton (Skeleton, EndPoints, JuncPoints)get_region_points (EndPoints, Rows1, Columns1)gen_cross_contour_xld (Cross, Rows1[0], Columns1[0], 50, 0)gen_cross_contour_xld (Cross1, Rows1[1], Columns1[1], 50, 0)dev_display (ImageAffineTrans)dev_display (Cross)dev_display (Cross1)stop ()
endfor
2.程序解析
2.1图像矫正
在进行检测前,需要将图像矫正在垂直状态,在几何图像中是最大内接矩形是不容易判断的,例如在圆中的最大内接矩形是无限个。所以halcon并没有提供带角度的最大内接矩形,只是提供了垂直角度下的最大内接矩形。所以,我们需要对图像进行矫正到垂直。
*矫正图像binary_threshold (ImageReduced, Region, 'max_separability', 'dark', UsedThreshold)erosion_circle (Region, RegionErosion,1)smallest_rectangle2 (RegionErosion, Row, Column, Phi2, Length1, Length2)vector_angle_to_rigid (Row, Column, Phi2, Row, Column, 1.5707963, HomMat2D)affine_trans_image (ImageReduced, ImageAffineTrans, HomMat2D, 'constant', 'false')reduce_domain (ImageAffineTrans, ROI_0, ImageReduced2)
2.2获取断面粗轴部分
本文中以,断面的下半部分作为轴的粗轴部分。首先是将上下断面区域进行联合,取区域的内接矩形,做差值后,就可以提取出粗轴的部分。
*获取断面下半部分binary_threshold (ImageReduced2, Region2, 'smooth_histo', 'dark', UsedThreshold1)*将上下断面进行联合shape_trans (Region2, RegionTrans, 'convex')*取区域的最大内接矩形inner_rectangle1 (RegionTrans, Row1, Column1, Row2, Column2)*创建矩形区域。根据实际情况,可以对内接矩形的左右列坐标进行修改,使得较粗的部分可以较多*较细的部分尽可能的少gen_rectangle1 (Rectangle1, Row1, Column1-4, Row2, Column2-3)*求取差值,将轴区域与内接矩形区域求差值,可得粗轴外轮廓difference (Region2, Rectangle1, RegionDifference)*剔除细轴多余的区域opening_circle (RegionDifference, RegionOpening1, 1)connection (RegionOpening1, ConnectedRegions)select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 200.96, 10000)union1 (SelectedRegions, RegionUnion)*进行横向闭运算,将粗轴进行相连接。closing_rectangle1 (RegionUnion, RegionClosing, 1000000000, 1)opening_circle (RegionClosing, RegionOpening, 5)connection (RegionOpening, ConnectedRegions1) select_shape (ConnectedRegions1, SelectedRegions1, 'area', 'and', 10833.3, 363400.6)shape_trans (SelectedRegions1, RegionTrans1, 'convex')
2.3获取断面细轴部分
将整个轴的区域减去粗轴的区域,获得的就是断面区域
difference (Region2, RegionTrans1, RegionDifference2)connection (RegionDifference2, ConnectedRegions3) select_shape (ConnectedRegions3, SelectedRegions3, 'area', 'and', 5196.22, 500000)opening_circle (SelectedRegions3, RegionOpening2, 3.5)
2.4获取断面并显示
对上下部分进行膨胀,膨胀后取交集,即可获取到断面区域的。可以根据实际情况条件膨胀的参数使得结果在所需要的位置
*上下部分*对上下部分进行膨胀取交集dilation_circle (RegionOpening2, RegionDilation2, 1)dilation_circle (RegionTrans1, RegionDilation3, 3)intersection (RegionDilation2, RegionDilation3, RegionIntersection1)*结果显示skeleton (RegionIntersection1, Skeleton)junctions_skeleton (Skeleton, EndPoints, JuncPoints)get_region_points (EndPoints, Rows1, Columns1)gen_cross_contour_xld (Cross, Rows1[0], Columns1[0], 50, 0)gen_cross_contour_xld (Cross1, Rows1[1], Columns1[1], 50, 0)dev_display (ImageAffineTrans)dev_display (Cross)dev_display (Cross1)
总结
断面检测的核心在于对区域的内接矩形和差值的运用。通过内接矩形剔除细轴的区域,在不断通过差值进行计算即可。