ImgPath := 'C:/Users/zjm/Desktop/图像拼接/新建文件夹/'
ImgName := 'bga_r_'
dev_update_off ()
dev_close_window ()
dev_open_window (0, 0, 640, 480, 'white', WindowHandle1)
set_display_font (WindowHandle1, 14, 'mono', 'true', 'false')
CamParam := [0.0121693,-2675.63,7.40046e-006,7.4e-006,1920/2,1080/2,1920,1080]
*根据指定的径向畸变系数畸变系数修改了内部摄像机参数。
change_radial_distortion_cam_par ('adaptive', CamParam, 0, CamParOut)//自适应
gen_empty_obj (Images)
for J := 1 to 8 by 1
read_image (Image, ImgPath +J+'.jpg')
* 改变图像的径向失真。
change_radial_distortion_image (Image, Image, Image, CamParam, CamParOut)
concat_obj (Images, Image, Images)
dev_display (Image)
disp_message (WindowHandle1, 'Image ' + J$'d', 'window', 20, 10, 'green', 'false')
wait_seconds (0.5)
endfor
disp_continue_message (WindowHandle1, 'black', 'true')
*显示用于计算投影的点匹配图像之间的变换,我们将把所有的图像放大显示
*平铺的图像,在图像之间留有一定的空间,以便扩展图片很容易看到
dev_set_window_extents (-1, -1, 1920*4, 1080*2)
*在窗口平铺显示
tile_images_offset (Images, TiledImage, [0,0,0,0,1080,1080,1080,1080], [0,1920,1920*2,1920*3,0,1920,1920*2,1920*3], [-1,-1,-1,-1,-1,-1,-1,-1], [-1,-1,-1,-1,-1,-1,-1,-1], [-1,-1,-1,-1,-1,-1,-1,-1], [-1,-1,-1,-1,-1,-1,-1,-1], 1920*4, 1080*2)
dev_clear_window ()
dev_display (TiledImage)
disp_message (WindowHandle1, 'All 10 images', 'window', 20, 10, 'green', 'false')
disp_continue_message (WindowHandle1, 'black', 'true')
stop ()
*现在我们计算出对图像和这两个图像之间的点匹配。图像对之间的变换。
* dev_clear_window ()
* dev_display (TiledImage)
disp_message (WindowHandle1, 'Point matches', 'window', 20, 10, 'green', 'false')
* 定义配置。From图片与To图对应起来进行拼接,规则(下,下右,右)
From := [1,2,3]
To := [2,3,4]
Num := |From|
*我们需要变量来累积投影变换矩阵,匹配点的坐标和匹配的数量。每个图像对点。
ProjMatrices := []
Rows1 := []
Cols1 := []
Rows2 := []
Cols2 := []
NumCorrespondences := []
* 现在我们可以确定图像对之间的变换。
for J := 0 to Num - 1 by 1
F := From[J]
T := To[J]
select_obj (Images, ImageF, F)
select_obj (Images, ImageT, T)
* 提取两个图像中的点。
SigmaGrad := 1
SigmaSmooth := 3
Alpha := 0.04
Threshold := 0
points_harris (ImageF, SigmaGrad, SigmaSmooth, Alpha, Threshold, RowFAll, ColFAll)
points_harris (ImageT, SigmaGrad, SigmaSmooth, Alpha, Threshold, RowTAll, ColTAll)
* 取两个图像中的点。
*获取的结果为图像右上方的坐标点,F和T为初始图和匹配目标图FR\FC和TR\TC为
if (F > 4)
FShiftR := 1100
FShiftC := (F - 5) * 1940
else
FShiftR := 0
FShiftC := (F - 1) * 1940
endif
if (T > 4)
TShiftR := 1100
TShiftC := (T -5) * 1940
else
TShiftR := 0
TShiftC := (T - 1) * 1940
endif
*初始图和匹配图偏移量
RowMove := (FShiftR - TShiftR) / 2.7
ColMove := (FShiftC - TShiftC) / 2
* 执行匹配
*掩码窗口的大小为MaskSize*MaskSize
MaskSize := 21
*半高度匹配的搜索窗口为20
RowTolerance := 20
*半宽度的匹配搜索窗口为20。
ColTolerance := 20
*旋转角度范围
Rotation := 0
*灰度值匹配的阈值。
MatchThreshold := 50
*转换一致性检查的阈值。
DistanceThreshold := 0.4
*为随机数生成器种子。
RandSeed := 4364537
*通过寻找点之间的对应关系,计算两个图像之间的投影变换矩阵。
*ssd”,则使用灰色差的平方之和,“sad”表示绝对差的和,“ncc”表示归一化的互相关
proj_match_points_ransac (ImageF, ImageT, RowFAll, ColFAll, RowTAll, ColTAll, 'sad', MaskSize, RowMove, ColMove, RowTolerance, ColTolerance, Rotation, MatchThreshold, 'gold_standard', DistanceThreshold, RandSeed, ProjMatrix, Points1, Points2)
* 积累的变换矩阵/将获取的变换矩阵进行叠加保存至ProjMatrice
ProjMatrices := [ProjMatrices,ProjMatrix]
Rows1 := [Rows1,subset(RowFAll,Points1)]
Cols1 := [Cols1,subset(ColFAll,Points1)]
Rows2 := [Rows2,subset(RowTAll,Points2)]
Cols2 := [Cols2,subset(ColTAll,Points2)]
*将每次匹配后的特征点个数保存起来
NumCorrespondences := [NumCorrespondences,|Points1|]
endfor
disp_continue_message (WindowHandle1, 'black', 'true')
*
* 设置可能的转换类型和堆栈顺序
PossibleTransformations := ['projective','affine','similarity','rigid']
*参考图像的索引。
StartImage := 3
*马赛克图像的叠加顺序。
StackingOrder := [1,2,3,4,5,6,7,8]
*输入图像的域是否被转换
TransformRegion := 'false'
* 最后,我们可以从投影变换中生成调整后的拼接图像。
* for i := 0 to |PossibleTransformations| - 1 by 1
* Transformation := PossibleTransformations[i]
*将多张图片进行捆绑用于生成马赛克图片
bundle_adjust_mosaic (8, StartImage, From, To, ProjMatrices, Rows1, Cols1, Rows2, Cols2, NumCorrespondences, 'projective', MosaicMatrices2D, Rows, Cols, Error)
*生成马赛克图
gen_bundle_adjusted_mosaic (Images, MosaicImage, MosaicMatrices2D, StackingOrder, TransformRegion, TransMat2D)
stop ()
* endfor
Halcon马赛图拼接(多图平移)
最新推荐文章于 2024-03-22 15:56:41 发布