这段代码是一个图像处理脚本,用于在图像中检测和测量特定对象的特征,例如在机器视觉系统中测量物体的间距。具体来说,它试图在图像中找到并测量一组线(例如,可能是物体上的刻度线或标记),并将这些线的位置从图像坐标转换到实际的度量单位(毫米)
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 关闭测量对象,释放资源。
这段代码解决了在图像中自动检测和测量线的问题,并将这些线的位置从像素坐标转换为实际的物理尺寸。这在工业自动化、质量控制和机器视觉领域中非常有用,例如在生产线上测量产品尺寸或检查产品是否符合规格。通过自动化这些测量过程,可以提高效率、减少人为错误,并确保产品质量。