opencv - 裁剪最终拼接的全景图像

标签 opencv image-processing image-stitching

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

以这个示例全景图像为例:
enter image description here

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

最佳答案

我误解了你的问题。关于你的问题,取图像最左边的一个 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/

关于opencv - 裁剪最终拼接的全景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53995738/

相关文章:

c++ - OpenCV SVD 返回与 MATLAB 不同的结果

python - 我该如何处理基本矩阵?

iphone - 使用 OpenGL ES 应用亮度和对比度

c++ - 图像部分的Opencv高度/宽度

image-processing - 将 Vector<Point> 转换为 Mat

python - 如何在 numpy 中将 OpenCV cvMat 转换回 ndarray ?

c++ - OpenCV 中心单应

Image Stitching参数好像没有人能解释一下,请大家帮忙理解一下

matlab - 在matlab中实现去马赛克功能

R : what library should I use? 中的图像处理/颜色检测