裁剪最终拼接的全景图像

时间:2019-01-01 13:15:12

标签: opencv image-processing image-stitching

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

以以下示例全景图为例: enter image description here

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

1 个答案:

答案 0 :(得分: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() 

enter image description here enter image description here

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