16

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).

Community
  • 1
  • 1
SubniC
  • 9,807
  • 4
  • 26
  • 33

4 Answers4

8

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.

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
  • Thanks CodeInChaos, i will try your snnipet and tell you how it works :) – SubniC Nov 21 '10 at 10:10
  • There's a small typo in your algorithms: your y-loop is comparing x with the height. Also, wouldn't the second algorithm give a higher score for an all-white image than for an alternating white-black checkerboard pattern? This seems wrong, since the checkerboard is "more in focus". – Mike Nov 07 '12 at 19:45
  • @noroom Thanks. Neither algorithm was doing what I intended it to do. Should be fixed now. Please let me know if you find another mistake. – CodesInChaos Nov 07 '12 at 20:10
7

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!

Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
  • Hi Belisarius, very interesting paper, i will try to get some information that can help me. regards. – SubniC Nov 19 '10 at 12:56
1

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

Valentin H
  • 7,240
  • 12
  • 61
  • 111
0

There's some information on Wikipedia

Technically it can be implemented as high pass filter and some system which conscientiously moves lens around the point where filter output is highest. Digital processing is not required

Also, 5 out of the first 6 matches I get from Googling for "autofocus algorithm" seem to have relevant and useful information (although in one or two cases the full details of papers requires payment)

The Archetypal Paul
  • 41,321
  • 20
  • 104
  • 134
  • Hello Paul, thanks for your time answering, I already know the wikipedia page, never ask befor search. Maybe i'm not so clear about google :) i looked for it but i didn't find any algorithm i could understand, my mathematical background is not so strong, because of that i'm asking here for help. – SubniC Nov 19 '10 at 12:54
  • I'll try to learn it, if i can't i will find some to help me. But for the beginning is really nice the people here to get you heading the right direcction :) – SubniC Nov 19 '10 at 13:04
  • 1
    It befuddles me how you'd implement a high pass filter without 'digital processing'. It's not like you can hang a capacitor on the USB signal. – Hans Passant Nov 19 '10 at 14:25
  • @Hans Passant: That's a quote from the wikipedia article which of course doesn't restrict itself to USB. It states that you can use an analog highpass filter on the output of an analog camera to implement auto-focusing. – CodesInChaos Nov 19 '10 at 14:44
  • Hehe, breaking into a museum to get the camera is what will get him into trouble :) – Hans Passant Nov 19 '10 at 14:50