Best fit for image manipulation

1 view (last 30 days)
I have 2 sets of data, both are measurements of the same part, but the values of the measurements are time dependent. I am trying to line them up the best that I can so that the data sets can be subtracted and the difference can be seen. Currently, I am using imrotate to rotate the image and then "shifting" the first data set over the other using linear interpolation (interp2). I for every shifted position and every rotated position I subtract the two images, take the squared average, and use the minimum average to find the "best" fit.
This only works some of the time, when there isn't much change in the measurements
My question is: What is the best way to determine if they are properly lined up. I assumed that the mean square of the data is the best fit, but through trial, have found that it is not.
The error seen in the subtraction image is most likely due to it not being rotated enough.
Code Description:
  • 'data_new' and 'data_old' are the two data sets.
  • 'Search_Rotation' is how far the image is rotated and 'Step_Rotation' is how finely the data is rotated.
  • 'Search_Size' is how far in each direction the data is shifted while 'Step_Size' is how finely the data is shifted.
  • 'r_mtx' temporarily stores the data after being rotated, and 'f_mtx' stores the data after being shifted. Once the data is shifted, this data is compared to data_old, and the average of the squared points is taken and this value is stored in the array 'avg'. I use the minimum value in the array 'avg' and assume that it is the best fitting data and I retrieve the corresponding data set.
LineInterp.m is a function used to linearly interpolate the data. It is simpler and faster than interp2, so that is why I use it. The inputs are (data,xshift,yshift, ev)
  • 'data' is the data to be interpolated
  • 'xshift' is how much to shift the data within the matrix. (-) values shift the data left and (+) right
  • 'yshift' is how much to shift the data within the matrix. (-) values shift the data down and (+) up
  • 'ev' is extrapolation value. I assume all values outside the matrix to be 0.
data_new
data_old
Subtracted data after fit
Code:
Search_Size = 1;
Search_Rotation = 2;
Step_Size = 0.1;
Step_Rotation = 0.1;
[ys,xs] = size(data_new);
zs = Search_Rotation * 2 / Step_Rotation + 1;
ws = (Search_Size * 2 / Step_Size + 1) ^ 2;
avg = zeros(zs,ws);
count1 = 0;
for loop1 = -Search_Rotation:Step_Rotation:Search_Rotation
count2 = 0;
count1 = count1 + 1;
r_mtx = imrotate(data_new,loop1,'bilinear', 'crop');
for loop2 = -Search_Size:Step_Size:Search_Size
for loop3 = -Search_Size:Step_Size:Search_Size
count2 = count2 + 1;
f_mtx = LineInterp(r_mtx,loop2,loop3,0);
f_mtx = f_mtx - data_old;
avg(count1,count2) = mean2((f_mtx).^2);
end
end
end
I have not used too much Matlab, but try to code as best I can. Any ideas of how to approach this would be helpful. Thanks!
Andrew

Accepted Answer

Image Analyst
Image Analyst on 1 May 2015
There are some built-in registration methods that are better than the trial and error approach you're using. Look up registration in the help for the Image Processing Toolbox. I don't see how any of those other toolboxes you listed, like database, mapping, etc. would help or do any better. Image registration is "owned" by the Image Processing Toolbox. Look up posts by Alex Taylor - I think he wrote the routines.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!