title: Calculating differences between many images
body: <p>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.</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>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!</p>