下述代码是使用 HALCON 软件编写的,HALCON 是一款用于机器视觉和图像分析的高级软件。代码的目的是创建一个由两张图像拼接而成的图像马赛克(mosaic),并计算不同金字塔级别下图像匹配所需的时间。
ImgPath := ‘3d_machine_vision/mosaic/’
ImgName := ‘bga_r_’
Times := []
Colors := [‘red’,‘coral’,‘yellow’,‘lime green’]
read_image (Images, ImgPath + ImgName + [‘01’,‘06’])
dev_update_off ()
dev_close_window ()
dev_open_window_fit_size (0, 0, 640, 980, 320, 490, WindowHandle)
dev_open_window_fit_size (0, 330, 490, 490, 1000, 490, WindowHandle1)
set_display_font (WindowHandle, 14, ‘mono’, ‘true’, ‘false’)
set_display_font (WindowHandle1, 14, ‘mono’, ‘true’, ‘false’)
- The internal camera parameters of the used camera
- (necessary to eliminate radial distortions)
CamParam := [0.0121693,-2675.63,7.40046e-006,7.4e-006,290.491,258.887,640,480]
change_radial_distortion_cam_par (‘adaptive’, CamParam, 0, CamParOut)
change_radial_distortion_image (Images, Images, Images, CamParam, CamParOut) - To show the point matches that are used to compute the
- transformation between the images, we will show both images in a
- tiled image with some space between the images so that the extents
- of the images are easily visible.
tile_images_offset (Images, TiledImage, [0,500], [0,0], [-1,-1], [-1,-1], [-1,-1], [-1,-1], 640, 980) - Now we can determine the transformations between the image pairs.
From := 1
To := 2
select_obj (Images, ImageF, From)
select_obj (Images, ImageT, To) - Repeat the calculation 4 times with a different number of pyramid levels
for NumLevels := 1 to 4 by 1
*
dev_clear_window ()
dev_set_window (WindowHandle)
dev_clear_window ()
dev_display (TiledImage)
disp_message (WindowHandle, [‘Calculate point matches’,‘with ’ + NumLevels + ’ pyramid levels’,‘Please wait …’], ‘window’, 20, 10, ‘black’, ‘true’)
*- Calculate the projection between the two images
- Check the procedure’s comments for details
count_seconds (S1)
proj_match_points_ransac_pyramid (ImageF, ImageT, NumLevels, RowFAll, ColFAll, RowTAll, ColTAll, ProjMatrix, Points1, Points2)
count_seconds (S2)
Times := [Times,S2 - S1] - Display point correspondences
gen_cross_contour_xld (PointsF, RowFAll, ColFAll, 6, rad(45))
gen_cross_contour_xld (PointsT, RowTAll + 500, ColTAll, 6, rad(45))
RowF := subset(RowFAll,Points1)
ColF := subset(ColFAll,Points1)
RowT := subset(RowTAll,Points2) + 500
ColT := subset(ColTAll,Points2)
gen_empty_obj (Matches)
for K := 0 to |RowF| - 1 by 1
gen_contour_polygon_xld (Match, [RowF[K],RowT[K]], [ColF[K],ColT[K]])
concat_obj (Matches, Match, Matches)
endfor
dev_display (TiledImage)
dev_set_color (‘blue’)
dev_display (Matches)
dev_set_color (‘green’)
dev_display (PointsF)
dev_display (PointsT)
disp_message (WindowHandle, [|RowF| + ’ point matches’,‘Time used: ’ + (S2 - S1)$’.3’ + ’ s’], ‘window’, 20, 10, ‘black’, ‘true’) - Generate the mosaic image
gen_projective_mosaic (Images, MosaicImage, 1, From, To, ProjMatrix, [2,1], ‘false’, MosaicMatrices2D) - Display mosaic image
get_image_size (MosaicImage, Width, Height)
dev_set_window (WindowHandle1)
dev_resize_window_fit_image (MosaicImage, 0, 330, [400,700], 700)
dev_clear_window ()
dev_display (MosaicImage)
disp_message (WindowHandle1, ‘Projective mosaic (used ’ + NumLevels + ’ pyramid levels)’, ‘window’, 20, 10, ‘black’, ‘true’)
disp_continue_message (WindowHandle1, ‘black’, ‘true’)
stop ()
endfor
- Display execution times
dev_set_window (WindowHandle)
dev_close_window ()
MaxTime := max(Times)
BaseRow := 380
RectHeight := 300
disp_message (WindowHandle1, [‘Time in s:’,‘(#levels used)’], ‘image’, BaseRow + 20, 10, ‘black’, ‘true’)
for Index := 0 to |Times| - 1 by 1
gen_rectangle1 (Rectangle, BaseRow - RectHeight * Times[Index] / MaxTime, 200 + Index * 100, BaseRow, 280 + Index * 100)
disp_message (WindowHandle1, [Times[Index]$‘.3’,‘(’ + (Index + 1) + ‘)’], ‘image’, BaseRow + 20, 200 + 100 * Index, ‘black’, ‘true’)
dev_set_color (Colors[Index])
dev_set_draw (‘fill’)
dev_display (Rectangle)
endfor
disp_finished_message (WindowHandle1, ‘black’, ‘true’)
bga_r_01.png
bga_r_06.png
bga_r_01.png
bga_r_02.png
bga_r_03.png
bga_r_04.png
以下是代码的主要步骤和功能:
定义图像路径和名称:
ImgPath 定义了图像的存储路径。
ImgName 定义了图像的基本名称。
初始化变量:
Times 用于存储不同金字塔级别下图像匹配所需的时间。
Colors 定义了用于显示不同级别时间的矩形的颜色。
读取图像:
使用 read_image 函数读取路径和名称组合的图像。
关闭和打开显示窗口:
dev_close_window 关闭当前窗口。
dev_open_window_fit_size 打开并调整窗口大小以适应图像。
设置显示字体:
set_display_font 设置窗口的显示字体。
相机参数:
CamParam 包含相机的内部参数,用于消除径向畸变。
图像畸变校正:
change_radial_distortion_image 函数用于校正图像的径向畸变。
图像拼接:
tile_images_offset 函数将两张图像拼接在一起,以便在计算图像之间的变换时更容易看到图像的范围。
图像匹配:
使用 proj_match_points_ransac_pyramid 函数在不同金字塔级别下计算图像匹配。
显示匹配点:
使用 gen_cross_contour_xld 函数生成交叉轮廓来显示匹配点。
生成马赛克图像:
gen_projective_mosaic 函数根据计算出的投影矩阵生成马赛克图像。
显示马赛克图像:
调整窗口大小以适应马赛克图像,并显示它。
显示执行时间:
计算并显示不同金字塔级别下图像匹配所需的时间。
绘制时间条形图:
使用 gen_rectangle1 函数绘制时间条形图,并使用不同的颜 ** 分不同级别的时间。
显示完成消息:
disp_finished_message 函数在窗口中显示完成消息。
代码中的注释提供了详细的步骤说明,有助于理解每个函数的作用和整个图像处理流程。