# Use List convolve for aligning images

I am trying to align images of point spread functions using a convolution. Before doing it on real data, I wanted to try on simulated data. I have no problem with the 1-D case where I create 2 Gaussian Distributions with different mean values, and same standard deviation. I would like to do the same for the 2-D case, where I have 2 Gaussians with same standard deviation but centered differently. I expect to get the corresponding shift to make one align perfectly with the other.

Bonus points to do it in Fourier space by the product of two matrices :)

Ultimately I want to be able to do the following

1. Segment out spots from an image
2. Align all spots to one another using a convolution
3. Average All spots together

Sample data for the 1-D case:

tst1[x_] := PDF[NormalDistribution[0, 1], x]
tst2[x_] := PDF[NormalDistribution[1, 1], x]
lst = Table[{tst1[x], tst2[x]}, {x, -10, 10, 0.01}];
xAxis = Table[x, {x, -10, 10, 0.01}];
lst1 = Transpose[{xAxis, lst[[All, 1]]}];
lst2 = Transpose[{xAxis, lst[[All, 2]]}];
ListPlot[{lst1, lst2}, PlotRange -> Full]


Then Compute the convolution to get the offset, and multiply by sample spacing. Correct the X positions on second gaussian to align with first

ListConvolve[lst1, lst2]*0.01


returns -1

aligned = lst2 /. {x_, y_} :> {x - 1, y};
ListPlot[{lst1, aligned}, PlotRange -> Full]


Now how To do this with a 2D Gaussian?

a = Table[
Exp[ -( ((i)^2/(2 1^2)) + ((j)^2/(2 1^2)))], {i, -10, 10,
1}, {j, -10, 10, 1}] // N ;

b = Table[
Exp[ -( ((i - 2)^2/(2 1^2)) + ((j - 2)^2/(2 1^2)))], {i, -10, 10,
1}, {j, -10, 10, 1}] // N ;

MatrixPlot[a]
MatrixPlot[b]
ListConvolve[a, b]


returned value of convolution {{0.425256}}

I thought I should get some result that tells me how much to shift image by in 2 dimensions (like the 2 D case, but for some reason I only get one number). I think there is something about 2D correlations I am not understanding

• I don't think the 1D case works yet as you imagine. For example: multiply both PDFs by 2... by your logic, you ought to get the same shift for the output, but instead you get 2 times the shift. WHat you need to do with correlations is to calculate the correlation for many shifts and then find the Max -- this will be the one where they line up best. – bill s Aug 1 '18 at 23:53