title: Autofocus routine detecting very small differences in blur
body: <p>I'm developing an autofocus routine for positioning on the micrometer scale, so I need to find very small differences in focus/blur between images. Luckily, the image pattern will always be the same (these are 256x256 center crops of the original 2 MP images):</p>
  
  <p><img src="https://i.stack.imgur.com/vmGkX.jpg" alt="0 µm off"> <img src="https://i.stack.imgur.com/nQARh.jpg" alt="50 µm off"></p>
  
  <pre><code>         Perfect focus         |           50 µm off
  </code></pre>
  
  <p>Finding the better focused image of the two above is not a problem, I guess most algorithms will do. But I really need to compare images with a lot less difference in focus, like the ones below:</p>
  
  <p><img src="https://i.stack.imgur.com/y1To6.jpg" alt="5 µm off"> <img src="https://i.stack.imgur.com/faTSG.jpg" alt="10 µm off"></p>
  
  <pre><code>           5 µm off            |           10 µm off
  </code></pre>
  
  <p>An alternative to stepping closer and closer to optimal focus is to find two images that have the same amount of blur on opposite sides of the focus plane. For example, one could save an image from -50 µm and then try to find an image around +50 µm where the blur is equal. Lets say that the image was found at +58 µm, then the focus plane should be positioned at +4 µm.</p>
  
  <p>Any ideas for a suitable algorithm?</p>