title: MATLAB's fminsearch function
body: <p>I have two images I'm trying to co-register - ie, one could be of a ball in the centre of the picture, the other is of the same ball near the edge and I'm trying to find the numbed of pixels I have to move the second image so that the balls would be in the same place. (I'm actually using 3D MRI brain scans, but the principle is the same).</p>
  
  <p>I've written a function that will move the ball left, right, up or down by a given number of pixels as well as another function that compares the correlation of the ball-in-the-centre image with the translated ball-at-the-edge image. When the two balls are in the same place the correlation function will return 0 and a number larger than 0 for other positions.</p>
  
  <p>I'm trying to use <code>fminsearch</code> (<a href="http://www.mathworks.com/access/helpdesk_r13/help/techdoc/ref/fminsearch.html" rel="nofollow noreferrer">documentation</a>) to find the optimal translation for the correlation function's minimum (ie, the balls being in the same place) like so:</p>
  
  <pre><code>global reference_im unknown_im;
  
  starting_trans = [0 0 0];
  trans_vector = fminsearch(@correlate_images,starting_trans)
  </code></pre>
  
  <p>correlate_images.m:</p>
  
  <pre><code>function r = correlate_images(translate)
  global reference_im unknown_im;
  
  new_im = move_image(unknown_im,translate(1),translate(2),translate(3));
  
  % This bit is unimportant to the question
  % but you can see how I calculate my correlation
  r = 1 - corr(reshape(new_im,[],1),reshape(reference_im,[],1));
  </code></pre>
  
  <p>There are two problems, firstly <code>fminsearch</code> insists on passing float values for the translation vector into the <code>correlate_images</code> function. Is there any way to inform it that only integers are necessary? (I would save a large number of cpu cycles!)</p>
  
  <p>Secondly, when I run this program the resulting <code>trans_vector</code> is always the same as <code>starting_trans</code> - I assume this is because no minimum has been found, but is there another reason its just plain not working?</p>
  
  <p>Many thanks!</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I've discovered what I think is the reason the output <code>trans_vector</code> is always the same as <code>starting_trans</code>. The <code>fminsearch</code> looks at the starting value, then a small increment in each direction from there, this small increment is always less than one, which means that the result from the correlation will be a perfect match (as the <code>move_image</code> will return the same as the input image for sub-pixel movements). I'm going to continue working on convincing matlab to only fminsearch over integer values!</p>