如何在 OpenCV Python Stitcher Class 中设置全景模式?

How to Set Panorama Mode in OpenCV Python Stitcher Class?

基于this question/answer,一直在尝试在python中使用OpenCV的Stitcherclass。我的代码和答案基本一样。

import cv2

stitcher = cv2.createStitcher(False)
foo = cv2.imread("D:/foo.png")
bar = cv2.imread("D:/bar.png")
result = stitcher.stitch((foo,bar))

cv2.imwrite("D:/result.jpg", result[1])

问题是我想将模式从全景更改为扫描。 In the c++ documentation, create方法有mode的输入。然而,Python 中的 createStitcher class 只需要一个输入——是否尝试 gpu。有没有办法在Python中指定模式?

当我尝试 createStitcherScans 时,出现错误

stitcher = cv2.createStitcherScans(False)
"AttributeError: 'module' object has no attribute 'createStitcherScans'"

我确实发现 this GitHub issue 似乎相关,关于 Python 绑定遗漏了一些东西。但这超出了我的理解,我不确定如何编辑 opencv 代码以正确执行此操作。我尝试将其添加到 stitching.hpp:

typedef Stitcher::Mode Mode;

但是什么也没发生。 createStitcher(1, False) 仍然给我属性错误。任何帮助将不胜感激。

我遇到了同样的问题。看完documentation it looks like the python wrapper function is not mapping to create(), but rather createDefault() which does not have a mode input, only the try_use_gpu input. I don't have experience with wrappers, but one could try to figure it out by reading OpenCV's info on how the python bindings work.

根据我目前在 python 中的缝合工作,似乎 createDefault() 在 PANORAMA 模式下创建了一个缝合器,因为它扭曲了我的图像。我不能花时间查看包装纸,但如果我最终花个人时间来修复它,我会在这里更新我的答案。

这已在 Open CV 4 中解决(至少在我 运行 的 4.1.0 中)。您现在可以将 mode 指定为 cv2.Stitcher.create:

的关键字参数
stitcher = cv2.Stitcher.create(mode = 1)

似乎还没有移植不同模式的枚举标签,但它适用于数字。 PANORAMA0SCANS1,定义为 here

请注意,如果您正在 Google Colab 上工作,那么您可以像这样更改 Open CV 的版本:

!pip3 install opencv-python==4.1.0.25

查看 Python 在 GitHub samples/python/stitching.py, you can use Stitcher_PANORAMA or Stitcher_SCANS. You can always check the test code of any module, like modules/stitching/misc/python/test 上的官方测试代码。

stitcher = cv.Stitcher.create(cv.Stitcher_PANORAMA)