首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >裁剪最终拼接的全景图像

裁剪最终拼接的全景图像
EN

Stack Overflow用户
提问于 2019-01-01 21:15:12
回答 1查看 348关注 0票数 0

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

以本例全景图为例:

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

EN

回答 1

Stack Overflow用户

发布于 2019-01-02 09:48:32

我误解了你的问题。关于您的问题,将图像最左侧的30个宽度像素的窗口作为参考图像,然后使用30个像素的窗口从左向右滑过图像的x轴,然后通过均方误差( MSE )将其与参考图像进行比较,MSE越小,两个图像越相似。有关更多详细信息,请查看代码。

代码语言:javascript
复制
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/

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53995738

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档