1
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.

Reinderien
  • 11,755
  • 5
  • 49
  • 77
  • How out-of-alignment are the two images? Can you show examples? – Reinderien Mar 02 '23 at 02:32
  • Are the two images guaranteed to have the same dimensions? – Reinderien Mar 02 '23 at 02:33
  • Are there any transformation types that are ruled out (rotation, scaling, translation, skew, etc.)? – Reinderien Mar 02 '23 at 02:37
  • Hi Reinderien, It is not aligning at all that means not updating my transformation matrix. Yes, they have the same dimension. Basically they are synthetic binary images rotated with about 70 degrees to the other. – Ashfiqur Rahman Mar 03 '23 at 03:06
  • I don't think this approach is viable at all. I've tried a number of variations on my end and none of them work. It's not a tractable problem for a naive optimizer. You need to use the traditional image registration methods: identify points of interest in both, and then calculate the matrix of correspondence. – Reinderien Mar 06 '23 at 01:04
  • Gradient descent gets stuck in local minima. With this type of problem you need to find a good start point, meaning your two images should be close to being registered. There is a much better approach if you are looking for a rigid registration (rotation, translation, scaling): Fourier-Mellin transform. It’s not iterative, and very efficient computationally. – Cris Luengo Mar 06 '23 at 03:47

0 Answers0