测量镙丝钉的例子measure_screw注释

* This program inspects the dimensions of a screw

dev_update_off ()
* 镙纹测量
* step: acquire image

read_image (Image, 'screw_thread')
get_image_pointer1 (Image, Pointer, Type, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width/2, Height/2, 'light gray', WindowID)
dev_set_part (0, 0, Height-1, Width-1)
dev_set_line_width (2)
dev_set_color ('blue')
dev_set_draw ('margin')
dev_display (Image)
set_display_font (WindowID, 14, 'courier', 'true', 'false')
disp_continue_message (WindowID, 'black', 'true')
stop ()

* step: initialize visualization

dev_set_draw ('fill')
dev_set_line_width (2)
dev_set_color ('black')

* step: get orientation via region processing

*二值化
threshold (Image, Region, 0, 100)
*区域方向
orientation_region (Region, OrientationRegion)
*区域中心点和面积
area_center (Region, Area, RowCenter, ColumnCenter)

* step: rotate screw region to become vertical
* 把目标变换正
vector_angle_to_rigid (RowCenter, ColumnCenter, OrientationRegion, RowCenter, ColumnCenter, rad(90), HomMat2DRotate)
affine_trans_region (Region, RegionAffineTrans, HomMat2DRotate, 'false')
dev_clear_window ()
disp_message (WindowID, 'Region (transformed)', 'window', -1, -1, 'black', 'false')
dev_display (RegionAffineTrans)
disp_continue_message (WindowID, 'black', 'true')
stop ()

* step: get width of the screw via region processing

*闭运算,填充的作用
closing_circle (RegionAffineTrans, RegionToProcess, 4.5)
dev_clear_window ()
disp_message (WindowID, 'Region (transformed)', 'window', -1, -1, 'black', 'false')
dev_display (RegionToProcess)
* 查询一个区域的扫描宽度编码,返回每行对应的列起点ColumnBegin和终点ColumnEnd,行存于RowRegionRuns中
get_region_runs (RegionToProcess, RowRegionRuns, ColumnBegin, ColumnEnd)
NumberLines := |RowRegionRuns|
*取中间的区域.
ColumnBeginSelected := ColumnBegin[90:(NumberLines-90)]
ColumnEndSelected := ColumnEnd[90:(NumberLines-90)]
*长度
Diameter := ColumnEndSelected-ColumnBeginSelected+1

* statistics
*平均值
meanDiameter := mean(Diameter)
*最小值
minDiameter := min(Diameter)
*最大值
maxDiameter := max(Diameter)

* visualization
dev_set_color ('white')
gen_region_line (ScanLine1, RowRegionRuns[90], ColumnBegin[90], RowRegionRuns[90], ColumnEnd[90])
gen_region_line (ScanLine2, RowRegionRuns[NumberLines-90], ColumnBegin[NumberLines-90], RowRegionRuns[NumberLines-90], ColumnEnd[NumberLines-90])
dev_display (ScanLine1)
dev_display (ScanLine2)
disp_cross (WindowID, RowRegionRuns[90], ColumnBegin[90], 20, 0)
disp_cross (WindowID, RowRegionRuns[90], ColumnEnd[90], 20, 0)
disp_cross (WindowID, RowRegionRuns[NumberLines-90], ColumnBegin[NumberLines-90], 20, 0)
disp_cross (WindowID, RowRegionRuns[NumberLines-90], ColumnEnd[NumberLines-90], 20, 0)
disp_message (WindowID, 'Width of object', 'image', 10, 720, 'black', 'false')
disp_message (WindowID, 'per row (between ', 'image', 60, 720, 'black', 'false')
disp_message (WindowID, 'the white lines):', 'image', 110, 720, 'black', 'false')
disp_message (WindowID, 'mean width: '+meanDiameter + ' pixels', 'image', 210, 720, 'black', 'false')
disp_message (WindowID, 'minimum width: '+minDiameter + ' pixels', 'image', 260, 720, 'black', 'false')
disp_message (WindowID, 'maximum width: ' +maxDiameter + ' pixels', 'image', 310, 720, 'black', 'false')
disp_continue_message (WindowID, 'black', 'true')
stop ()

* step: get transformed contours and corresponding regression lines

* create ROI
**************亚像素轮廓方法计算**********************
*边界
boundary (Region, RegionBorder, 'inner_filled')
*膨胀
dilation_circle (RegionBorder, RegionDilation, 7.5)
*剪切图片
reduce_domain (Image, RegionDilation, ImageReduced)
dev_clear_window ()
dev_display (ImageReduced)
disp_message (WindowID, 'ROI for contour extraction', 'window', -1, -1, 'white', 'false')
disp_continue_message (WindowID, 'black', 'true')
stop ()

* extract contours and regression lines using Laplace
sigma := 3
*高斯导数滤波用作 平滑滤波器(使用分水岭算法watershed得到contours)
derivate_gauss (ImageReduced, DerivGauss, sigma, 'laplace')
*根据子像素准确性从一幅图像中提取零相交
zero_crossing_sub_pix (DerivGauss, Edges)
*选择XLD
select_contours_xld (Edges, SelectedEdges, 'contour_length', 3000, 99999, -0.5, 0.5)
*变换
affine_trans_contour_xld (SelectedEdges, ContoursAffinTrans, HomMat2DRotate)
*计算每个区域的坐标(连续/栏)的包围矩形
smallest_rectangle1_xld (ContoursAffinTrans, Row11, Column11, Row21, Column21)
*剪切XLD
clip_contours_xld (ContoursAffinTrans, ClippedContours, Row11+90, 0, Row21-90, Width)
*直线段轮廓拟合直线
fit_line_contour_xld (ClippedContours, 'regression', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist1)
dev_clear_window ()
dev_display (ClippedContours)
disp_message (WindowID, 'Contours (transformed and clipped)', 'window', -1, -1, 'black', 'false')
disp_continue_message (WindowID, 'black', 'true')
stop ()

* step: get deviations of the contours from the regression lines

*生成右XLD线段
gen_contour_polygon_xld (RegressContour0, [RowBegin[0], RowEnd[0]], [ColBegin[0], ColEnd[0]])
*选择右对象
select_obj (ClippedContours, EdgeContour0, 1)
*查询XLD坐标点
get_contour_xld (EdgeContour0, RowEdge0, ColEdge0)
*计算与边缘的最大最小值,平均值。
distance_pc (RegressContour0, RowEdge0, ColEdge0, DistanceMin0, DistanceMax0)
minDistance0 := min(DistanceMin0)
maxDistance0 := max(DistanceMin0)
meanDistance0 := mean(DistanceMin0)

gen_contour_polygon_xld (RegressContour1, [RowBegin[1], RowEnd[1]], [ColBegin[1], ColEnd[1]])
select_obj (ClippedContours, EdgeContour1, 2)
get_contour_xld (EdgeContour1, RowEdge1, ColEdge1)
distance_pc (RegressContour1, RowEdge1, ColEdge1, DistanceMin1, DistanceMax1)
minDistance1 := min(DistanceMin1)
maxDistance1 := max(DistanceMin1)
meanDistance1 := mean(DistanceMin1)
dev_clear_window ()

* numerical visualization
dev_set_color ('gray')
dev_display (EdgeContour0)
dev_display (EdgeContour1)
dev_set_color ('black')
disp_message (WindowID, 'Contours (transformed and clipped)', 'image', 10, 10, 'black', 'false')
dev_display (RegressContour0)
dev_display (RegressContour1)
disp_message (WindowID, 'Maximum distances', 'image', 10, 720, 'black', 'false')
disp_message (WindowID, 'between contours', 'image', 60, 720, 'black', 'false')
disp_message (WindowID, 'and regression lines:', 'image', 110, 720, 'black', 'false')

disp_message (WindowID, 'max (contour/edge 0): ', 'image', 210, 720, 'black', 'false')
disp_message (WindowID, maxDistance0$'.4' + ' pixels', 'image', 260, 720, 'black', 'false')

disp_message (WindowID, 'max (contour/edge 1): ', 'image', 360, 720, 'black', 'false')
disp_message (WindowID, maxDistance1$'.4' + ' pixels', 'image', 410, 720, 'black', 'false')

disp_message (WindowID, '0', 'image', RowEnd[0] + 20, ColEnd[0] -9, 'black', 'false')
disp_message (WindowID, '1', 'image', RowEnd[0] + 20, ColEnd[0] -9, 'black', 'false')

* graphical visualization in zoom window
dev_set_color ('white')
dev_set_draw ('margin')
gen_rectangle1 (Rectangle1, 500, 620, 550, 680)
dev_display (Rectangle1)
dev_open_window (0, Width/2+2, 300, 300, 'light gray', WindowHandleZoom)
dev_set_part (500, 620, 550, 680)
dev_set_color ('black')
dev_display (EdgeContour0)
dev_display (RegressContour0)
get_contour_xld (RegressContour0, RowContour0, ColContour0)
for i := 800 to 950 by 1
    projection_pl (RowEdge0[i], ColEdge0[i], RowContour0[0], ColContour0[0], RowContour0[1], ColContour0[1], RowProj0, ColProj0)
    gen_contour_polygon_xld (Contour, [RowEdge0[i], RowProj0], [ColEdge0[i], ColProj0])
    dev_display (Contour)
endfor
disp_continue_message (WindowID, 'black', 'true')
stop ()
dev_close_window ()
dev_set_window (WindowID)

* step: get distance between the two regression lines

distance_pp (RowBegin[1], ColBegin[1], RowEnd[0], ColEnd[0], Distance1)
distance_pp (RowBegin[0], ColBegin[0], RowEnd[1], ColEnd[1], Distance2)
if (abs(Distance1-Distance2)<1)
    * 
    * visualization
    dev_clear_window ()
    dev_set_color ('gray')
    dev_display (EdgeContour0)
    dev_display (EdgeContour1)
    dev_set_color ('black')
    disp_message (WindowID, 'Contours (transformed and clipped)', 'image', -1, -1, 'black', 'false')
    dev_display (RegressContour0)
    dev_display (RegressContour1)
    disp_message (WindowID, 'Mean distance', 'image', 10, 720, 'black', 'false')
    disp_message (WindowID, 'between regression', 'image', 60, 720, 'black', 'false')
    disp_message (WindowID, 'lines:', 'image', 110, 720, 'black', 'false')
    disp_message (WindowID, ((Distance1+Distance2)/2)$'.4' + ' pixels', 'image', 210, 720, 'black', 'false')
    dev_set_draw ('fill')
    dev_set_color ('white')
    disp_arrow (WindowID, RowEnd[1] +100, ColEnd[1], RowBegin[0] +100, ColBegin[0], 2)
    disp_arrow (WindowID, RowBegin[0] +100, ColBegin[0], RowEnd[1] +100, ColEnd[1], 2)
else
    dev_set_color ('black')
    disp_message (WindowID, 'Contours (transformed and clipped)', 'image', -1, -1, 'black', 'false')
    dev_display (RegressContour0)
    dev_display (RegressContour1)
    disp_message (WindowID, 'RegressContours', 'image', 10, 720, 'black', 'false')
    disp_message (WindowID, 'are not parallel', 'image', 60, 720, 'black', 'false')
endif
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值