Search code examples
imagematlabregistration

Calculating differences between many images


My problem is one related to image registration. I have a number of images in a .tif-file, all the same size. I read those into MATLAB as a 3D-array of matrices and try to optimize the overlay of features in those images solely by rotation. I tried using imabsdiff, but resorted to just doing it like shown below. In short, I input a vector containing as many angles as I have images in my stack. I rotate each image in the stack by each corresponding angle. Then, I calculate the absolute differences ([image1-image2] + [image2-image1]) which is what imabsdiff does, but faster. For this, I use two loop variables and compare each individual image to the whole stack, while leaving out comparison between identical images. Cost is the sum of differences between all images.

for oo = 1:slidecount
    centered_stack(:,:,oo) = imrotate(centered_stack(:,:,oo),
    angle_in(oo), 'bilinear', 'crop');
end

for pp = 1:slidecount
    image1 = centered_stack(:,:,pp);
    for qq = 1:slidecount
        if qq ~= pp % only do this if comparing different images
            image2 = centered_stack(:,:,qq);
            cost_temp(qq) = sum(sum(abs(image1 - image2))) +  
            sum(sum(abs(image2 - image1))); 
        else 
            cost_temp(qq) = 0;
        end
        cost_temp = sum(cost_temp);    
    end
    cost(pp) = cost_temp;
end

cost = sum(cost);

This then serves as a cost value for an optimization procedure. Can someone please tell me if there is a faster, maybe vectorized way to do this or something conceptually completely different? This approach gets very time consuming with many images. FFT based registration maybe? Thanks!


Solution

  • In your code you compare each pair of images twice:

    1. image_1 to image_2 (pp == 1, qq == 2)
    2. image_2 to image_1 (pp == 2, qq == 1)

    Is this intended? If you make second loop look like this:

    for qq = (pp+1):slidecount
    

    you will reduce the computation by the factor of 2. Also your condition for checking qq ~= pp will not be needed anymore.