关于OpenCV图像拼接的方法,如果不熟悉的话,可以先看看公众号整理的如下四篇文章介绍:
- OpenCV常用图像拼接方法(一):直接拼接(硬拼)
- OpenCV常用图像拼接方法(二):基于模板匹配拼接
- OpenCV常用图像拼接方法(三):基于特征匹配拼接
- OpenCV常用图像拼接方法(四):基于Stitcher类拼接
本篇文章是Stitcher类的扩展介绍,通过例程stitching_detailed.cpp的使用和参数介绍,帮助大家了解Stitcher类拼接的具体步骤和方法,先看看其内部的流程结构图(如下):
stitching_detailed.cpp目录如下,可以在自己安装的OpenCV目录下找到,笔者这里使用的OpenCV4.4版本,stitching_detailed.cpp具体源码如下目录,由于代码较多,这里不贴出来,大家找到位置自行查阅。
stitching_detail 程序运行流程
- 命令行调用程序,输入源图像以及程序的参数
- 特征点检测,判断是使用 surf 还是 orb,默认是 surf
- 对图像的特征点进行匹配,使用最近邻和次近邻方法,将两个最优的匹配的置信度 保存下来
- 对图像进行排序以及将置信度高的图像保存到同一个集合中,删除置信度比较低的图像间的匹配,得到能正确匹配的图像序列。这样将置信度高于门限的所有匹配合并到一个集合中
- 对所有图像进行相机参数粗略估计,然后求出旋转矩阵
- 使用光束平均法进一步精准的估计出旋转矩阵
- 波形校正,水平或者垂直
- 拼接
- 融合,多频段融合,光照补偿
stitching_detail 程序接口介绍
- img1 img2 img3 输入图像
- --preview 以预览模式运行程序,比正常模式要快,但输出图像分辨率低,拼接的分辨 率 compose_megapix 设置为 0.6
- --try_gpu (yes|no) 是否使用 CUDA加速,默认为 no,使用CPU模式
- /* 运动估计参数 */
- --work_megapix <--work_megapix <float>> 图像匹配时的分辨率大小,默认为 0.6
- --features (surf | orb | sift | akaze) 选择 surf 或者 orb 算法进行特征点匹配,默认为 surf
- --matcher (homography | affine) 用于成对图像匹配的匹配器
- --estimator (homography | affine) 用于转换估计的估计器类型
- --match_conf <float> 特征点匹配步骤的匹配置信度,最近邻匹配距离与次近邻匹配距离的比值,surf 默认为 0.65,orb 默认为 0.3
- --conf_thresh <float> 两幅图来自同一全景图的置信度,默认为 1.0
- --ba (no | reproj | ray | affine) 光束平均法的误差函数选择,默认是 ray 方法
- --ba_refine_mask (mask) 光束平均法设置优化掩码
- --wave_correct (no|horiz|vert) 波形校验水平,垂直或者没有 默认是 horiz(水平)
- --save_graph <file_name> 将匹配的图形以点的形式保存到文件中, Nm 代表匹配的数量,NI代表正确匹配的数量,C 表示置信度
- /*图像融合参数:*/
- --warp (plane|cylindrical|spherical|fisheye|stereographic|compressedPlaneA2B1|compressedPla neA1.5B1|compressedPlanePortraitA2B1|compressedPlanePortraitA1.5B1|paniniA2B1|paniniA1.5B1|paniniPortraitA2B1|paniniPor traitA1.5B1|mercator|transverseMercator) 选择融合的平面,默认是球形
- --seam_megapix <float> 拼接缝像素的大小 默认是 0.1
- --seam (no|voronoi|gc_color|gc_colorgrad) 拼接缝隙估计方法 默认是 gc_color
- --compose_megapix <float> 拼接分辨率,默认为-1
- --expos_comp (no|gain|gain_blocks) 光照补偿方法,默认是 gain_blocks
- --blend (no|feather|multiband) 融合方法,默认是多频段融合
- --blend_strength <float> 融合强度,0-100.默认是 5.
- --output <result_img> 输出图像的文件名,默认是 result,jpg 命令使用实例,以及程序运行时的提示:
上面使用默认参数,详细输出信息如下:
代码语言:javascript复制
E:PracticeOpenCVAlgorithm_SummaryImage_Stitchingx64Debug>05_Image_Stitch_Stitching_Detailed.exe ./imgs/boat1.jpg ./imgs/boat2.jpg ./imgs/boat3.jpg ./imgs/boat4.jpg ./imgs/boat5.jpg ./imgs/boat6.jpg
Finding features...
[ INFO:0] global C:buildmaster_winpack-build-win64-vc15opencvmodulescoresrcocl.cpp (891) cv::ocl::haveOpenCL Initialize OpenCL runtime...
Features in image #1: 500
[ INFO:0] global C:buildmaster_winpack-build-win64-vc15opencvmodulescoresrcocl.cpp (433) cv::ocl::OpenCLBinaryCacheConfigurator::OpenCLBinaryCacheConfigurator Successfully initialized OpenCL cache directory: C:UsersA4080599AppDataLocalTempopencv4.4opencl_cache
[ INFO:0] global C:buildmaster_winpack-build-win64-vc15opencvmodulescoresrcocl.cpp (457) cv::ocl::OpenCLBinaryCacheConfigurator::prepareCacheDirectoryForContext Preparing OpenCL cache configuration for context: NVIDIA_Corporation--GeForce_GTX_1070--411_31
Features in image #2: 500
Features in image #3: 500
Features in image #4: 500
Features in image #5: 500
Features in image #6: 500
Finding features, time: 5.46377 sec
Pairwise matchingPairwise matching, time: 3.24159 sec
Initial camera intrinsics #1:
K:
[534.6674906996568, 0, 474.5;
0, 534.6674906996568, 316;
0, 0, 1]
R:
[0.91843718, -0.09762425, -1.1678253;
0.0034433089, 1.0835428, -0.025021957;
0.28152198, 0.16100603, 0.91920781]
Initial camera intrinsics #2:
K:
[534.6674906996568, 0, 474.5;
0, 534.6674906996568, 316;
0, 0, 1]
R:
[1.001171, -0.085758291, -0.64530683;
0.010103324, 1.0520245, -0.030576767;
0.15743911, 0.12035993, 1]
Initial camera intrinsics #3:
K:
[534.6674906996568, 0, 474.5;
0, 534.6674906996568, 316;
0, 0, 1]
R:
[1, 0, 0;
0, 1, 0;
0, 0, 1]
Initial camera intrinsics #4:
K:
[534.6674906996568, 0, 474.5;
0, 534.6674906996568, 316;
0, 0, 1]
R:
[0.8474561, 0.028589081, 0.75133896;
-0.0014587968, 0.92028928, 0.033205934;
-0.17483309, 0.018777205, 0.84592116]
Initial camera intrinsics #5:
K:
[534.6674906996568, 0, 474.5;
0, 534.6674906996568, 316;
0, 0, 1]
R:
[0.60283858, 0.069275051, 1.2121853;
-0.014153662, 0.85474133, 0.014057174;
-0.29529575, 0.053770453, 0.61932623]
Initial camera intrinsics #6:
K:
[534.6674906996568, 0, 474.5;
0, 534.6674906996568, 316;
0, 0, 1]
R:
[0.41477469, 0.075901195, 1.4396564;
-0.015423983, 0.82344943, 0.0061162044;
-0.35168326, 0.055747174, 0.42653102]
Camera #1:
K:
[1068.953598931666, 0, 474.5;
0, 1068.953598931666, 316;
0, 0, 1]
R:
[0.84266716, -0.010490002, -0.53833258;
0.004485324, 0.99991232, -0.01246338;
0.53841609, 0.0080878884, 0.84264034]
Camera #2:
K:
[1064.878323247434, 0, 474.5;
0, 1064.878323247434, 316;
0, 0, 1]
R:
[0.95117813, -0.015436338, -0.3082563;
0.01137107, 0.99982315, -0.014980057;
0.308433, 0.010743499, 0.95118535]
Camera #3:
K:
[1065.382193682081, 0, 474.5;
0, 1065.382193682081, 316;
0, 0, 1]
R:
[1, -1.6298145e-09, 0;
-1.5716068e-09, 1, 0;
0, 0, 1]
Camera #4:
K:
[1067.611537959627, 0, 474.5;
0, 1067.611537959627, 316;
0, 0, 1]
R:
[0.91316396, -7.9067249e-06, 0.40759254;
-0.0075879274, 0.99982637, 0.017019274;
-0.4075219, -0.018634165, 0.91300529]
Camera #5:
K:
[1080.708135180496, 0, 474.5;
0, 1080.708135180496, 316;
0, 0, 1]
R:
[0.70923853, 0.0025724203, 0.70496398;
-0.0098195076, 0.99993235, 0.0062302947;
-0.70490021, -0.01134116, 0.70921582]
Camera #6:
K:
[1080.90412660159, 0, 474.5;
0, 1080.90412660159, 316;
0, 0, 1]
R:
[0.49985889, 3.5938341e-05, 0.86610687;
-0.00682831, 0.99996907, 0.0038993564;
-0.86607999, -0.0078631733, 0.49984369]
Warping images (auxiliary)...
Warping images, time: 0.0791121 sec
Compensating exposure...
Compensating exposure, time: 0.72288 sec
Finding seams...
Finding seams, time: 3.09237 sec
Compositing...
Compositing image #1
Multi-band blender, number of bands: 8
Compositing image #2
Compositing image #3
Compositing image #4
Compositing image #5
Compositing image #6
Compositing, time: 13.7766 sec
Finished, total time: 29.4535 sec
输入图像boat1.jpg、boat2.jpg、boat3.jpg、boat4.jpg、boat5.jpg、boat6.jpg如下(可以在OpenCV安装目录下找到D:OpenCV4.4opencv_extra-mastertestdatastitching)
结果图:
参数warp_type 设置为"plane",效果图如下:
参数warp_type 设置为"fisheye",效果图如下(旋转90°后):
其他的参数可以根据自己需要修改,如果要自己完成还需要详细了解拼接步骤再优化。更多OpenCV、Halcon等相关学习资讯请关注公众号:OpenCV与AI深度学习