我在一个文件夹中有一堆图像,它们实际上只是一个图像的片段,这些图像被分成重叠的部分。如何以编程方式快速重组这些图像以创建原始图像?
我更喜欢使用 python 或数学的解决方案(或者是现有的应用程序),但我也对其他想法持开放态度(我对 Java 相当精通)。
我在一个文件夹中有一堆图像,它们实际上只是一个图像的片段,这些图像被分成重叠的部分。如何以编程方式快速重组这些图像以创建原始图像?
我更喜欢使用 python 或数学的解决方案(或者是现有的应用程序),但我也对其他想法持开放态度(我对 Java 相当精通)。
我意识到这个答案来得很晚,但是由于我花了一些时间在谷歌上搜索这个问题,我想我会使用 python 发布一个相当简单的解决方案。
import cv2
#works in version 4.2
cv2.__version__
#mode=0 photos, mode=1 scans
stitcher = cv2.Stitcher.create(mode=1)
img1 = cv2.imread("frame-0021.png")
img2 = cv2.imread("frame-0022.png")
result = stitcher.stitch((img1, img2))
cv2.imwrite("stitched.png", result[1])
好吧,我不再需要这样做来做我想做的事,但我会分享如果我用 python(psuedocode 和 python 的混合)编写它,我会怎么做。在这里,我假设后续图像的左上角始终是重叠点(在我的情况下确实如此)。如果您想检测任何角落的重叠,您将需要检测您所处的“角落”案例并为每个案例添加处理。
images = list of images to be stitched, loaded from directory
stitched_image = images[0]
for (image in images):
if first image then skip it (continue)
else combine_images(stitched_image, image)
def combine_images (stitched_image, image_to_add):
top_left_corner = top left corner of image_to_add
// top_left_corner dimensions need to be big enough that you don't have a false positive,
// but not so big that the overlap doesn't exist
coordinates = find_coordinates(stitched_image,top_left_corner)
new_width = max(stitched_image.width,image_to_add.width + coordinates.x)
new_height = max(stitched_image.height,image_to_add.width + coordinates.y)
new_image = new Image(new_width,new_height) // See note 1
new_image.paste(stitched_image,(0,0))
new_image.paste(image_to_add,(coordinates.x,coordinates.y))
stitched_image = new_image
def find_coordinates (image,sub_image):
// See note 2 below for how to implement
笔记:
可以使用 PIL 完成创建图像并粘贴到其中:http: //29a.ch/2009/5/14/concatenating-images-using-python
有关如何在图像中查找 sub_image(可能需要将图像转换为另一种表示形式)的信息,请参阅此问题:Finding a subimage inside a Numpy image。此外,对于任何熟练的程序员来说,手动检查像素矩阵中的像素以找到重叠部分一点也不难。如果您通过简单地首先在更有可能的区域中搜索来大致知道可能发生重叠的位置,则可以添加额外的优化。
您是否打算使用特定于 Java 的解决方案?如果您对其他事情持开放态度,我正在为一个项目做类似的事情,并为 Linux 提供了一个 bash 脚本集。
为此,我使用了
pto_gen
安装 hugin 后不存在,而是match-n-shift
在 Panotools 脚本中替换了我的。如果您想按顺序批量处理多个全景图,则必须想出一种对文件进行排序、执行和移动的方法。这是我编写脚本过程中有趣的部分。将图片拼接在一起很容易,确保它们之后去正确的地方有点棘手。
目前,使用具有 4 GB RAM 的 4 核 Xeon 系统,拼接 50 幅图像 360 度全景图需要大约 30-45 分钟。
在 Mathematica 中,您可以在重叠区域内使用 ImageCorrespondingPoints,然后使用 FindGeometricTransform 计算仿射变换,将一个图像带入另一个图像。请注意,图像和重叠区域的大小会影响转换的准确性。如果您正在做一些复杂的事情(例如组合卫星图像),您将需要一个整体几何模型来获得结果,然后将每个图像映射到它。在这种情况下,仿射变换可能是不够的。
你可以使用 Autopano 或类似的东西;如果你想自己动手,你可能会发现 SIFT 算法很有用http://www.janeriksolem.net/2009/02/sift-python-implementation.html?m=1
你想要的是一个创建全景图的工具。有各种出售的工具具有各种功能。需要考虑的事情是: