Manmohan Bishnoi
Manmohan Bishnoi

Reputation: 803

Crop final stitched panorama image

I am using OpenCV to stitch images incrementally (Left to Right). After the stitching process is complete I want to crop the result for final panorama.

Take this example panorama image: enter image description here

How do I crop the image to remove the repeating part shown inside RED box on right ?

Upvotes: 0

Views: 648

Answers (1)

Ha Bom
Ha Bom

Reputation: 2917

I misunderstood your question. Regarding to your problem, take a 30-width-pixel window to the most left of the image as reference image, then slide over the x-axis of the image from left to right with a window of 30 pixels and then compare it with the reference image by the Mean Squared Error (MSE), the smaller the MSE is, the more similar the 2 images are. Look at the code for more details.

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

The idea of comparing 2 images comes from this post: https://www.pyimagesearch.com/2014/09/15/python-compare-two-images/

Upvotes: 1

Related Questions