Reputation:
This is part of some code I've been working on to quickly align images. It works well, but the syntax is ugly. Is there a better way to write this?
def shift_int(image, base, y, x):
"""
Quickly shift an image with respect to a base and return a parameter that
is minimized when the images are well aligned, and not biased towards
large shifts
image -- The input image that is shifted
base -- The second image to match
y -- An offset along axis 0
x -- An offset along axis 1
"""
new_image = image.copy()
new_base = base.copy()
if y > 0:
new_image = new_image[:-y]
new_base = new_base[y:]
if y < 0:
new_image = new_image[-y:]
new_base = new_base[:y]
if x > 0:
new_image = new_image[:,:-x]
new_base = new_base[:,x:]
if x < 0:
new_image = new_image[:,-x:]
new_base = new_base[:,:x]
return np.mean((new_im-new_base)**2)
Upvotes: 1
Views: 44
Reputation: 2028
h, w = np.shape(new_image)
new_image = new_image[max(0, -y):min(h, h-y),max(0, -x):min(w, w-x)]
h, w = np.shape(new_base)
new_base = new_base[max(0, y):min(h, h+y),max(0, x):min(w, w+x)]
Upvotes: 1