import numpy
import numpy as np
import scipy.ndimage
from scipy.optimize import minimize
def similarity_metric(image1, image2):
# Compute Mean Squared Error between two images
mse = np.mean((image1 - image2) ** 2)
return mse
def transform_image(image, matrix):
# Apply an affine transformation to an image
matrix[2, :] = [0, 0, 1]
return scipy.ndimage.interpolation.affine_transform(image, matrix)
def cost_function(params, *args):
# Extract the input images and the transformation matrix from the input arguments
image1, image2 = args[:2]
matrix = np.array(params).reshape((3, 3))
# Compute the similarity metric between the warped image and the target image
warped_image = transform_image(source_img, matrix)
cost = similarity_metric(warped_image, image2)
return cost
# Initialize transformation matrix to identity matrix
matrix = np.eye(3)
initial_guess = matrix.flatten()
# Optimize the cost function using the L-BFGS-B optimizer
max_iterations = 1000
bounds = [(-10, 10) for i in range(9)]
result = minimize(cost_function, initial_guess, args=(source_img, target_img, matrix),\
method='L-BFGS-B', bounds=bounds, options={'maxiter': max_iterations})
TM = np.array(result.x).reshape((3, 3)) # Transformation matrix
print(TM)
aligned_image = transform_image(source_img, TM)
But is showing the following error
I am trying to align two images by optimizing a transfer matrix using gradient descent approach. The transformation matrix is always trans_matrix = [[1. 0. 0.],[0. 1. 0.], [0. 0. 1.]] and does not gets updated.Which means it is not rotating (or aligning) at all. Note: I know OpenCV or Scikit learn might helpful. I want to do this kind of 'manually' and only using scipy and numpy.
Any kind of help would be appreciated.