输入图片1
输入图片2
附Halcon代码
* Initializations
Times := []
read_image (Image1, 'D:/DeepSoftWare/HalconExample/HALCON-22.05-Progress/examples/images/3d_machine_vision/mosaic/bga_r_01.png')
read_image (Image2, 'D:/DeepSoftWare/HalconExample/HALCON-22.05-Progress/examples/images/3d_machine_vision/mosaic/bga_r_06.png')
concat_obj (Image1, Image2, Images)
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')
get_image_size (Image1, Width0, Height0)
get_image_size (Image2, Width, Height)
*水平=1,垂直=0
VerticalAndHorizontal:=0
if (VerticalAndHorizontal)
*水平
if ( Height0#Height)
zoom_image_size (Image1, Image1, Width0, Height, 'constant')
concat_obj (Image1, Image2, Images)
endif
tile_images_offset (Images, TiledImage, [0, 0], [0, Width0+20], [-1, -1], [-1, -1], [-1, -1], [-1, -1], Width0+Width+20, Height)
else
*垂直
if ( Width0#Width )
zoom_image_size (Image1, Image1, Width, Height0, 'constant')
concat_obj (Image1, Image2, Images)
endif
tile_images_offset (Images, TiledImage, [0, Height0+20], [0, 0], [-1, -1], [-1, -1], [-1, -1], [-1, -1], Width, Height0+Height+20)
endif
select_obj (Images, ImageF, 1)
select_obj (Images, ImageT, 2)
*
dev_clear_window ()
dev_set_window (WindowHandle)
dev_clear_window ()
proj_match_points_ransac_pyramid (ImageF, ImageT, RowFAll, ColFAll, RowTAll, ColTAll, ProjMatrix, Points1, Points2)
*
* Display point correspondences
gen_cross_contour_xld (PointsF, RowFAll, ColFAll, 6, rad(45))
if (VerticalAndHorizontal)
gen_cross_contour_xld (PointsT, RowTAll , ColTAll+Width+20, 6, rad(45))
else
gen_cross_contour_xld (PointsT, RowTAll + Height+20, ColTAll, 6, rad(45))
endif
RowF := subset(RowFAll,Points1)
ColF := subset(ColFAll,Points1)
if (VerticalAndHorizontal)
RowT := subset(RowTAll,Points2)
ColT := subset(ColTAll,Points2)+Width+20
else
RowT := subset(RowTAll,Points2) + Height+20
ColT := subset(ColTAll,Points2)
endif
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)
*
* 生成马赛克图像
gen_projective_mosaic (Images, MosaicImage, 1, 1, 2, ProjMatrix, [2, 1], 'false', MosaicMatrices2D)
* 马赛克图像显示
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_continue_message (WindowHandle1, 'black', 'true')
stop ()
*
子函数
*此程序使用图像金字塔进行计算
*两幅图像之间的射影变换。
*如果UseRigidTransformation设置为true,结果仅限于刚性变换(代替射影变换)
UseRigidTransformation := true
* Parameters for the Harris point detector
SigmaGrad := 0.7
SigmaSmooth := 2
Alpha := 0.04
Threshold := 50
* 为两个输入图像生成图像金字塔
gen_gauss_pyramid (ImageF, ImageFPyramid, 'constant', 0.5)
gen_gauss_pyramid (ImageT, ImageTPyramid, 'constant', 0.5)
* 在一开始,没有一个近似的预测是已知的
HomMat2DGuide := []
*计算每个金字塔层的射影变换
*从图像金字塔中选择图像
select_obj (ImageFPyramid, ImageFLevel, 1)
select_obj (ImageTPyramid, ImageTLevel, 1)
*在两个图像中提取兴趣点
points_harris (ImageFLevel, SigmaGrad, SigmaSmooth, Alpha, Threshold, RowsF, ColsF)
points_harris (ImageTLevel, SigmaGrad, SigmaSmooth, Alpha, Threshold, RowsT, ColsT)
*从点对应关系计算投影
if (|HomMat2DGuide| == 0)
*在金字塔的最高级别上,使用proj_mathc_points_ransac
get_image_size (ImageFLevel, Width, Height)
*通过寻找点之间的对应关系,计算两个图像之间的射影变换矩阵。
proj_match_points_ransac (ImageFLevel, ImageTLevel, RowsF, ColsF, RowsT, ColsT, 'ncc', 10, 0, 0, Height, Width, [rad(-40),rad(40)], 0.5, 'gold_standard', 2.5 * pow(2,4 - 1), 42, ProjMatrix, Points1, Points2)
else
* 在较低的级别上,使用来自上层的近似
*输入proj_match_points_ransac_guided
*计算两个图像之间的射影变换矩阵,根据已知的射影变换矩阵的近似值找到点之间的对应关系。
proj_match_points_ransac_guided (ImageFLevel, ImageTLevel, RowsF, ColsF, RowsT, ColsT, 'ncc', 10, HomMat2DGuide, 10 * pow(2.0,4.0 - 1), 0.5, 'gold_standard', 2.5 * pow(2.0,4.0 - 1), 42, ProjMatrix, Points1, Points2)
endif
if (UseRigidTransformation)
* 利用找到的点对应关系来计算刚变换
*与vector_to_rigid
*注意,proj_match_points_ransac_guided的转换结果
*在这种情况下,*被忽略。
RowF := subset(RowsF,Points1)
ColF := subset(ColsF,Points1)
RowT := subset(RowsT,Points2)
ColT := subset(ColsT,Points2)
vector_to_rigid (RowF + 0.5, ColF + 0.5, RowT + 0.5, ColT + 0.5, ProjMatrix)
ProjMatrix := [ProjMatrix,0, 0, 1]
endif
*用于下一个较低的金字塔水平,投影有
*以适应新的比额表。
hom_mat2d_scale_local (ProjMatrix, 0.5, 0.5, HomMat2DGuide)
hom_mat2d_scale (HomMat2DGuide, 2, 2, 0, 0, HomMat2DGuide)
return ()