测量物体的间距

这段代码是一个图像处理脚本,用于在图像中检测和测量特定对象的特征,例如在机器视觉系统中测量物体的间距。具体来说,它试图在图像中找到并测量一组线(例如,可能是物体上的刻度线或标记),并将这些线的位置从图像坐标转换到实际的度量单位(毫米)

dev_close_window ()

  • Read the image and init the program.
    read_image (ImageOrig, ‘caliper/caliper_1’)
    rotate_image (ImageOrig, Image, 90, ‘constant’)
    get_image_size (Image, Width, Height)
    dev_open_window_fit_image (Image, 0, 0, 640, 640, WindowHandle)
    set_display_font (WindowHandle, 16, ‘mono’, ‘true’, ‘false’)
    dev_display (Image)
  • Define the rectangle within which the lines must be determined.
    Row := 370
    Column := 305
    Phi := rad(-101)
    Length1 := 220
    Length2 := 3
  • Create the measure and determine the gray value profile.
    gen_measure_rectangle2 (Row, Column, Phi, Length1, Length2, Width, Height, ‘bilinear’, MeasureHandle)
    measure_projection (Image, MeasureHandle, GrayValues)
  • Onedimensional line extraction
  • First, smooth the gray value profile and determine the first and second derivative.
    Sigma := 0.3
    create_funct_1d_array (GrayValues, Function)
    smooth_funct_1d_gauss (Function, Sigma, SmoothedFunction)
    derivate_funct_1d (SmoothedFunction, ‘first’, FirstDerivative)
    derivate_funct_1d (SmoothedFunction, ‘second’, SecondDerivative)
  • Determine the positions of the zero crossings of the first derivative, i.e.,
  • the positions at which the (smoothed) gray value profile has minima and maxima.
    zero_crossings_funct_1d (FirstDerivative, ZeroCrossings)
  • Select the positions of those minima (i.e., positions at which the second derivative is positive)
  • where the smoothed gray value profile has a high curvature, i.e., where the second derivative
  • has a large absolute value. This criterion ensures that only salient lines are selected.
    MinimumMagnitudeOfSecondDerivative := 8
    PositionOfSalientLine := []
    for i := 0 to |ZeroCrossings| - 1 by 1
    get_y_value_funct_1d (SecondDerivative, ZeroCrossings[i], ‘constant’, Y)
    if (Y > MinimumMagnitudeOfSecondDerivative)
    PositionOfSalientLine := [PositionOfSalientLine,ZeroCrossings[i]]
    endif
    endfor
  • Determine the image coordinates from the profile positions.
  • Note that internally, the center of the measure ROI is set to (floor(Row+0.5), floor(Column+0.5))
  • and the lengths are set to floor(Length1) and floor(Length2).
    RowStart := floor(Row + 0.5) + floor(Length1) * sin(Phi)
    ColStart := floor(Column + 0.5) - floor(Length1) * cos(Phi)
    RowLine := RowStart - PositionOfSalientLine * sin(Phi)
    ColLine := ColStart + PositionOfSalientLine * cos(Phi)
  • Display the determined lines.
    dev_display (Image)
    p_disp_edge_marker (RowLine, ColLine, Phi, Length2, ‘black’, 5)
    p_disp_edge_marker (RowLine, ColLine, Phi, Length2, ‘white’, 3)
    disp_message (WindowHandle, ‘Determined pitch lines’, ‘window’, -1, -1, ‘black’, ‘true’)
    disp_continue_message (WindowHandle, ‘black’, ‘true’)
    stop ()
  • Display a zoomed part of the above visualization.
    dev_open_window_fit_size (0, Width + 10, Width, Height, 640, 640, WindowHandleZoom)
    RowZoomCenter := int((RowLine[3] + RowLine[4]) / 2)
    ColZoomCenter := int((ColLine[3] + ColLine[4]) / 2)
    dev_set_part (RowZoomCenter - Height / 20, ColZoomCenter - Width / 20, RowZoomCenter + Height / 20, ColZoomCenter + Width / 20)
    dev_display (Image)
    p_disp_edge_marker (RowLine, ColLine, Phi, Length2, ‘black’, 5)
    p_disp_edge_marker (RowLine, ColLine, Phi, Length2, ‘white’, 3)
    disp_continue_message (WindowHandle, ‘black’, ‘true’)
    stop ()
    dev_close_window ()
  • Transform the image coordinates into a metric coordinate system.
  • Here, the camera parameters and the pose of the object are assumed
  • to be known. If this is not the case, the camera parameter and the pose
  • must be determined with HALCON’s camera calibration (see, e.g., the
  • Application Note on 3D Machine Vision).
    CamParam := [0.00846,-2591.63,7.2956e-6,7.3e-6,240.74,334.91,Width,Height]
    WorldPose := [-0.100432,-0.051077,0.257936,323.234,4.615,0.229,0]
    image_points_to_world_plane (CamParam, WorldPose, RowLine, ColLine, ‘mm’, X, Y)
  • Now, e.g., the distances between the measured lines can be calculated.
    Num := |X|
    distance_pp (X[0:Num - 2], Y[0:Num - 2], X[1:Num - 1], Y[1:Num - 1], Distance)
  • Display the distances.
    for i := 0 to |Distance| - 1 by 1
    disp_message (WindowHandle, Distance[i]$‘5.2f’ + ’ mm’, ‘image’, (RowLine[i] + RowLine[i + 1]) / 2 - 8, (ColLine[i] + ColLine[i + 1]) / 2 + 15, ‘white’, ‘false’)
    endfor
    disp_message (WindowHandle, ‘Determined pitch lines’, ‘window’, -1, -1, ‘black’, ‘true’)
  • Close the measure.
    close_measure (MeasureHandle)

在这里插入图片描述

以下是检测结果

在这里插入图片描述

以下是代码的主要步骤和功能:

图像读取和初始化:

read_image 读取原始图像。
rotate_image 将图像旋转9度。
get_image_size 获取图像的尺寸。
窗口设置:

dev_open_window_fit_image 根据图像尺寸打开显示窗口。
set_display_font 设置显示窗口中的字体。
定义测量区域:

定义矩形区域,其中包含需要检测的线。
创建测量对象和灰度值分析:

gen_measure_rectangle2 创建测量对象。
measure_projection 测量图像在指定区域的灰度值。
一维线提取:

通过高斯平滑和求导来处理灰度值,以便于检测线的位置。
零交叉检测:

zero_crossings_funct_1d 检测一阶导数的零交叉点,这些点是灰度值变化的极值点。
选择显著线:

选择具有高曲率的极小值点,这些点对应于图像中的显著线。
图像坐标转换:

将从灰度值分析中得到的线的位置转换为图像坐标。
显示结果:

dev_display 显示原始图像。
p_disp_edge_marker 在图像上标记检测到的线的位置。
放大显示:

显示图像的一部分,以放大显示检测到的线。
坐标转换:

image_points_to_world_plane 将图像坐标转换为实际的度量单位(毫米)。
计算距离:

distance_pp 计算检测到的线之间的距离。
显示距离:

在图像上显示计算得到的距离。
关闭测量:

close_measure 关闭测量对象,释放资源。
这段代码解决了在图像中自动检测和测量线的问题,并将这些线的位置从像素坐标转换为实际的物理尺寸。这在工业自动化、质量控制和机器视觉领域中非常有用,例如在生产线上测量产品尺寸或检查产品是否符合规格。通过自动化这些测量过程,可以提高效率、减少人为错误,并确保产品质量。

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_z3405211980

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值