【问题标题】:Crop final stitched panorama image裁剪最终拼接的全景图像
【发布时间】:2019-01-01 13:15:11
【问题描述】:

我正在使用 OpenCV 逐步拼接图像(从左到右)。 拼接过程完成后,我想裁剪最终全景图的结果。

以全景图像为例:

如何裁剪图像以删除右侧红色框内显示的重复部分?

【问题讨论】:

  • 裁剪看起来很简单,只需获取包含非重复部分的 ROI。也许您正在寻找一种检测重复的方法,以便进行裁剪?
  • 是的,检测重复部分,然后裁剪,形成完美的圆柱全景图像。知道如何实现这一目标吗?

标签: opencv image-processing image-stitching


【解决方案1】:

我误解了你的问题。关于你的问题,取图像最左边的一个 30 像素宽的窗口作为参考图像,然后用一个 30 像素的窗口从左到右在图像的 x 轴上滑动,然后将其与均方误差 (MSE) 的参考图像,MSE 越小,两幅图像越相似。查看代码了解更多详情。

import matplotlib.pyplot as plt
import numpy as np
import cv2

img = cv2.imread('1.png')
# img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 
h=img.shape[0]
w=img.shape[1]

window_length = 30 # bigger length means more accurate and slower computing.

def mse(imageA, imageB):
    # the 'Mean Squared Error' between the two images is the
    # sum of the squared difference between the two images;
    # NOTE: the two images must have the same dimension
    err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)
    err /= float(imageA.shape[0] * imageA.shape[1])
    
    # return the MSE, the lower the error, the more "similar"
    # the two images are
    return err
    
reference_img = img[:,0:window_length]

mse_values = []

for i in range(window_length,w-window_length):
    slide_image = img[:,i:i+window_length]
    m = mse(reference_img,slide_image)
    mse_values.append(m)    

#find the min MSE. Its index is where the image starts repeating
min_mse = min(mse_values)
index = mse_values.index(min_mse)+window_length

print(min_mse)
print(index)

repetition = img[:,index:index+window_length]   

# setup the figure
fig = plt.figure("compare")
plt.suptitle("MSE: %.2f"% (min_mse))

# show first image
ax = fig.add_subplot(1, 2, 1)
plt.imshow(reference_img, cmap = plt.cm.gray)
plt.axis("off")

# show the second image
ax = fig.add_subplot(1, 2, 2)
plt.imshow(repetition, cmap = plt.cm.gray)
plt.axis("off")

cropped_img = img[:,0:index]

cv2.imshow("img", img)    
cv2.imshow("cropped_img", cropped_img)    
# show the plot
plt.show()        
        
cv2.waitKey()
cv2.destroyAllWindows() 

比较两张图片的想法来自这篇文章:https://www.pyimagesearch.com/2014/09/15/python-compare-two-images/

【讨论】:

  • 不是简单的作物。我需要裁剪图像开始重复的位置。这将包括检测重复模式,然后确定裁剪位置的宽度
  • @ManmohanBishnoi 是的,我误解了你的 OP。我编辑了我的答案。
猜你喜欢
  • 1970-01-01
  • 2011-12-12
  • 2014-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-22
  • 2015-08-18
相关资源
最近更新 更多