OpenCV Tutorials 21 - 高级拼接 API(Stitcher 类)

高级拼接 API(Stitcher 类)

在本小节中,我们将会了解到由OpenCV提供的Stitcher类进行高级拼接的API以及使用预配置的 Stitcher 配置来拼接使用不同相机型号的图像。

一、代码

def cv_show(name, img):
    cv.imshow(name, img)
    cv.waitKey(0)
    cv.destroyAllWindows()
复制代码
def compare(imgs):
    for i in range(len(imgs)):
        imgs[i]= cv.resize(imgs[i],(0,0), fx = 0.2, fy = 0.2)
    
    res = np.hstack(imgs)
    cv_show('Compare', res)
复制代码
#!/usr/bin/env python

'''
Stitching sample
================

Show how to use Stitcher API from python in a simple way to stitch panoramas
or scans.
'''

# Python 2/3 compatibility
from __future__ import print_function

import numpy as np
import cv2 as cv

import argparse
import sys

modes = (cv.Stitcher_PANORAMA, cv.Stitcher_SCANS)

# parser = argparse.ArgumentParser(prog='stitching.py', description='Stitching sample.')
# parser.add_argument('--mode',
#     type = int, choices = modes, default = cv.Stitcher_PANORAMA,
#     help = 'Determines configuration of stitcher. The default is `PANORAMA` (%d), '
#          'mode suitable for creating photo panoramas. Option `SCANS` (%d) is suitable '
#          'for stitching materials under affine transformation, such as scans.' % modes)
# parser.add_argument('--output', default = 'result.jpg',
#     help = 'Resulting image. The default is `result.jpg`.')
# parser.add_argument('img', nargs='+', help = 'input images')

# __doc__ += '\n' + parser.format_help()

def main():
#   args = parser.parse_args()
    images = ['stitching/boat1.jpg', 'stitching/boat2.jpg', 'stitching/boat3.jpg']
    # read input images
    imgs = []
    for img_name in images:
        img = cv.imread(cv.samples.findFile(img_name))
        if img is None:
            print("can't read image " + img_name)
            sys.exit(-1)
        imgs.append(img)

#     stitcher = cv.Stitcher.create(args.mode)
    stitcher = cv.Stitcher.create(cv.Stitcher_PANORAMA)
    status, pano = stitcher.stitch(imgs)

    if status != cv.Stitcher_OK:
        print("Can't stitch images, error code = %d" % status)
        sys.exit(-1)

#     cv.imwrite(args.output, pano)
    cv.imwrite('Result.png', pano)
    print("stitching completed successfully. %s saved!" % 'Result.png')
    print('Done')


if __name__ == '__main__':
    print(__doc__)
    main()
    cv.destroyAllWindows()
复制代码
Stitching sample
================

Show how to use Stitcher API from python in a simple way to stitch panoramas
or scans.

stitching completed successfully. Result.png saved!
Done
复制代码

download.png

二、解释

代码中最重要的部分如下:

imgs = []
# 传入拼接模式:全景图
stitcher = cv.Stitcher.create(cv.Stitcher_PANORAMA)
# 使用Stitcher对象的stich方法进行拼接
status, pano = stitcher.stitch(imgs)
复制代码

创建了一个新的stitcher 实例,并且 cv::Stitcher::stitch 将完成所有艰苦的工作。 cv::Stitcher::create 可以在其中一种预定义配置(参数模式)中创建缝合器。 有关详细信息,请参阅 cv::Stitcher::Mode。 这些配置将设置多个拼接器属性以在预定义的场景之一中运行。 在其中一种预定义配置中创建缝合器后,您可以通过设置任何缝合器属性来调整缝合。 如果您有 cuda 设备 cv::Stitcher 可以配置为将某些操作卸载到 GPU。 如果您更喜欢此配置,请将 try_use_gpu 设置为 true。 无论此标志如何,都将根据全局 OpenCV 设置透明地使用 OpenCL 加速。 拼接可能会因多种原因而失败,您应该始终检查一切是否顺利,并且生成的 pano 是否存储在 pano 中。 有关可能的错误代码,请参阅 cv::Stitcher::Status 文档。

三、相机模型

目前在拼接管道中实现了 2 个相机模型。

  1. Homography 模型期望在 cv::detail::BestOf2NearestMatcher 中实现的图像之间进行透视变换

    1. cv::detail::HomographyBasedEstimator
    2. cv::detail::BundleAdjusterReproj
    3. cv::detail::BundleAdjusterRay
  2. 仿射模型期望具有 6 DOF 或 4 DOF 实现的仿射变换 cv::detail::AffineBestOf2NearestMatcher

    1. cv::detail::AffineBasedEstimator
    2. cv::detail::BundleAdjusterAffine
    3. cv::detail::BundleAdjusterAffinePartial
    4. cv::AffineWarper

Homography 模型可用于创建由相机捕获的照片全景图,而基于仿射的模型可用于拼接由专用设备捕获的扫描和对象。

  • 注意:上面的示例需要 POSIX 平台,在 Windows 上,您必须明确提供所有文件名(例如,boat1.jpgboat2.jpg...),因为 Windows 命令行不支持 * 扩展。

四、拼接详情(python opencv >4.0.1)

如果您想研究拼接管道的内部结构,或者您想尝试详细的配置,您可以使用 C 或 python 中可用的stitching_detailed 源代码:raw.githubusercontent.com/opencv/open…

拼接详细程序使用命令行获取拼接参数。存在许多参数。上面的例子展示了一些可能的命令行参数:

boat5.jpg boat2.jpg boat3.jpg boat4.jpg boat1.jpg boat6.jpg –work_megapix 0.6 –features orb –matcher homography –estimator homography –match_conf 0.3 –conf_thresh 0.3 –ba ray –ba_refine_mask xxxxx –save_graph test.txt –wave_correct no –warp fisheye –blend multiband –expos_comp no –seam gc_colorgrad

download.jpg

对成对图像采用单应矩阵单应矩阵进行匹配,同时采用估计器进行变换估计

特征匹配步骤的可信度为0.3:-match _ conf 0.3。如果您在匹配图像时遇到困难,可以降低此值

对于同一幅全景图像,两幅图像的阈值相同,置信度为0。:-conf _ thresh 0.3如果匹配图像有困难,可以减小这个值

光束法平差成本函数是 ray-ba ray

光束法平差的精化掩码是 xxxxx (- ba _ refine _ mask xxxxx) ,其中x表示细化各自的参数,‘_'表示不细化。细化一个,并具有以下格式: fx、 skew、 ppx、 aspect、 ppy

将用 DOT 语言表示的匹配图保存到 test.txt (- Save _ graph test.txt) : 标签描述: Nm 是匹配数,Ni 是 inliers 数,c 是置信度

download.jpg

执行波浪效应校正置为无(–wave_correct no)

曲面类型为鱼眼 (–warp fisheye)

混合方法是多波段 (–blend multiband)

不使用曝光补偿法(–expos_comp no)

焊缝估计器是基于最小图割的焊缝估计器(–seam gc_colorgrad)

你也可以在命令行中使用这些参数:

boat5.jpg boat2.jpg boat3.jpg boat4.jpg boat1.jpg boat6.jpg –work_megapix 0.6 –features orb –matcher homography –estimator homography –match_conf 0.3 –conf_thresh 0.3 –ba ray –ba_refine_mask xxxxx –wave_correct horiz –warp compressedPlaneA2B1 –blend multiband –expos_comp channels_blocks –seam gc_colorgrad

得到如下图像:

download.jpg

对于使用扫描仪或无人机(仿射运动)捕获的图像,您可以在命令行上使用这些参数:

newspaper1.jpg newspaper2.jpg –work_megapix 0.6 –features surf –matcher affine –estimator affine –match_conf 0.3 –conf_thresh 0.3 –ba affine –ba_refine_mask xxxxx –wave_correct no –warp affine

download.jpg

猜你喜欢

转载自juejin.im/post/7105316601104187400