Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autofocus algorithm for USB microscope

I'm trying to design an auto-focus system for a low cost USB microscope. I have been developing the hardware side with a precision PAP motor that is able to adjust the focus knob in the microscope, and now I'm in the hard part.

I have been thinking about how to implement the software. The hardware have two USB ports, one for the microscope camera and another for the motor. My initial idea is to write an application in C# that is able to get the image from the microscope and to move the motor forth and backwards, so far so good :)

Now I need a bit of help with the auto-focus, how to implement it? There is any good algorithm for this? Or maybe an image processing library that will help my in my task?

I have been googleling but with no success... I'll appreciate any help/idea/recommendation!

Many thanks :)

EDIT: Thanks guys for your answers, i'll try all the options and get back here with the results (or maybe more questions).

like image 287
SubniC Avatar asked Nov 19 '10 12:11

SubniC


3 Answers

The most important piece is code which tells you how much out of focus the image is. Since an unfocused image loses high frequency data I'd try something like the following:

long CalculateFocusQuality(byte[,] pixels)
{
  long sum = 0;
  for(int y = 0; y<height-1; y++)
    for(int x=0; x<width-1; x++)
    {
      sum += Square(pixels[x+1, y] - pixels[x, y]);
      sum += Square(pixels[x, y] - pixels[x, y+1]);
    }
  return sum;
}

int Square(int x)
{
  return x*x;
}

This algorithm doesn't work well if the image is noisy. In that case you could to downsample it, or use a more complex algorithm.

Or another idea is calculating the variation of the pixel values:

long CalculateFocusQuality(byte[,] pixels)
{
  long sum = 0;
  long sumOfSquares = 0;
  for(int y=0; y<height; y++)
    for(int x=0; x<width; x++)
    {
      byte pixel=pixels[x,y];
      sum+=pixel;
      sumofSquares+=pixel*pixel;
    }
  return sumOfSquares*width*height - sum*sum;
}

These functions work on monochromatic images, for RGB images just sum the values for the channels.

Using this function change the focus trying to maximize CalculateFocusQuality. Increase the stepsize if several attempts in a row improved the quality, and decrease it and reverse the direction if the step reduced the quality.

like image 180
CodesInChaos Avatar answered Nov 15 '22 11:11

CodesInChaos


Autofocusing a microcoscope is a long standing topic in optical research.
You can learn a bit about the involved algorithms here.

The problems involved are not only how to meassure defocus, but also how to move the optical axis in an optimal way, and how to correct algorithmically the residual aberrations.

HTH!

like image 36
Dr. belisarius Avatar answered Nov 15 '22 09:11

Dr. belisarius


just some of my experiences trying to solve similar task. On my system a 200x magnificatin is used. Stepper resolutin in Z-direction 0.001um.

The problems I've faced: -Shaking. The image on theoretically better position could be evaluated worse because of suddenly shaking. As the API of my system didn't allow to move z-axix and make images in parallel, I had to move in steps and capture sequenttially. Each move-stop caused shaking. Interestingly, the shaking were more severe while moving down than moving up.

-Mechanical imprecision. Making a scan and moving to theoretically best position may bear an error, because stepper-position in controller may be not the same as the mechanical position.

-Exposure: Depending on the application, the brightness of the image may vary, so that exposure should be adjusted. Depending on focus-evaluation algorithm (whether brightness is involved in the calculation or not) the exposure may be required to be fixed. That results in the chicken-egg problem - how to setup exposure, if image brightness is unknown and how to focus, if required exposure is unknown.

Finally, to avoid mechanical problems I've (re)stored best image found while focusing and returned it at the end. Concerning the algorithm for focus-value, the best was looking for edges combined with entire number of colors (histogram width). But of cause, it depends on the type of image you process.

Regards, Valentin Heinitz

like image 1
Valentin H Avatar answered Nov 15 '22 11:11

Valentin H