案例代码:
dev_update_off ()
dev_get_window (WindowHandle)
dev_set_line_width (3)
read_image (Image, './punched_holes.png')
*边缘提取
binary_threshold (Image, Region, 'max_separability', 'light', UsedThreshold)
boundary (Region, RegionBorder, 'inner')
dilation_circle (RegionBorder, RegionDilation, 5)
reduce_domain (Image, RegionDilation, ImageReduced)
edges_sub_pix (ImageReduced, Edges, 'canny', 2, 20, 40)
sort_contours_xld (Edges, SortedContours, 'character', 'true', 'row')
*拟合
fit_rectangle2_contour_xld (SortedContours, 'tukey', -1, 0, 0, 3, 2, Row, Column, Phi, Length1, Length2, PointOrder)
gen_rectangle2_contour_xld (Rectangle, Row, Column, Phi, Length1, Length2)
dev_display (Image)
count_obj (Rectangle, Number)
for Index := 1 to Number by 1
*获取轮廓上的点
select_obj (SortedContours, ObjectSelected, Index)
get_contour_xld (ObjectSelected, CheckRow, CheckCol)
select_obj (Rectangle, ObjectSelected1, Index)
get_contour_xld (ObjectSelected1, RectRow, RectCol)
*计算检测轮廓上的点到拟合矩形轮廓上的角点的距离,从而判断是否是拐点,拐点的距离差会比较大
D1:=[sqrt(pow(CheckRow-RectRow[0],2))+sqrt(pow(CheckCol-RectCol[0],2))]
D2:=[sqrt(pow(CheckRow-RectRow[1],2))+sqrt(pow(CheckCol-RectCol[1],2))]
D3:=[sqrt(pow(CheckRow-RectRow[2],2))+sqrt(pow(CheckCol-RectCol[2],2))]
D4:=[sqrt(pow(CheckRow-RectRow[3],2))+sqrt(pow(CheckCol-RectCol[3],2))]
*取检测轮廓上的点到拟合矩形轮廓上的角点的最小距离,归为了该拐点区域
MinD:=min2(min2(D1,D2),min2(D3,D4))
*计算两轮廓上对应点的距离
dist_rectangle2_contour_points_xld (ObjectSelected, 0, Row[Index-1], Column[Index-1], Phi[Index-1], Length1[Index-1], Length2[Index-1], Distances)
*判断点是否满足:sgn(x)为符号函数;x>0,值为1;x=0,值为0,x<0,值为-1.
*判断点是否满足:sgn(x)为符号函数;x>0,值为1;x=0,值为0,x<0,值为-1.
mark:=max2(0, sgn(MinD-8))
MaxD:=max(mark*Distances)
if (MaxD > 1.5)
disp_message (WindowHandle, 'NG', 'image', Row[Index-1], Column[Index-1], 'red', 'true')
else
disp_message (WindowHandle, 'OK', 'image', Row[Index-1], Column[Index-1], 'green', 'true')
endif
stop ()
endfor
运行效果图: