Shop OBEX P1 Docs P2 Docs Learn Events
Contrast detection based autofocus doable at DIY level ? — Parallax Forums

Contrast detection based autofocus doable at DIY level ?

CuriousOneCuriousOne Posts: 931
edited 2013-03-08 08:49 in General Discussion
Hello.

I'm experimenting with diy long range observation camera. It consists of two servos (provide up/down/left-right movement) and a stepper motor, which moves the CCD panel forward or backward, to adjust the focus. Everything is controlled by basic stamp 2p module. The idea is, why not introduce contrast detection AF to system? Hand focusing is quite problematic, because of lens used, I have equivalent focal distance of 15000mm, which, converted to standard "zoom" terms, equals up to 500x of optical zoom. So, even at distance of 200 meters, depth of field is around 20cm ! This means constant re-adjusting of focus.

By looking at video signal with scope, you can see that sharper the image (more detailed), the higher is average frequency distribution in it. So my idea is as follows - use some high pass filter, to isolate higher frequencies of video signal, then convert it to voltage, measure it and use as reference for focus adjustment?

The algorithm may look as follows:

We are at current position of focus.
if no position changes were reported from movevement servos, skip focus adjustment, since it was adjusted already.
If there are some changes, then:

measure current signal.
do a little step forward, measure signal again. If level increased, do another step forward. Re-measure signal, If dropped, do a step backwards. Re-measure again, do it in the loop, until highest level of signal detected, then exit focusing routine, wait until aim of system is changed to re-focus.

Sure, this may lead to situation when camera will focus on not the object you wish to focus, but this can be worked out by software I think.

The question is, whenever this is doable using available hardware and programming languages (no asm), without using application specific ICs or hardware?

Comments

  • Heater.Heater. Posts: 21,230
    edited 2013-03-08 03:59
    CuriousOne,

    Interesting idea. I know nothing about such things but I gather that in a simple case you want to do this:
    1) Capture a line through the image. Say horizontal through the middle.
    2) Analyse the frequency components of the brightness as it varies along the line.
    3) Assume that at in focus image has lots of nice sharp edges and therefore relatively more high frequency components. (I guess this has to fail in some cases, like low contrast variation objects)

    One way to do this is to take the Fourier transform of the samples along the line which will give an array of amplitudes of different frequencies. You can then try an estimate from those amplitudes how much more or less there is in the higher end of the spectrum than the lower. Thus getting your focus estimate.

    Of course this might work better if you did a two dimensional Fourier transform of the entire set of pixels in the image. That might be a lot slower and unusable on a slow micro.

    The question then is, how do you get that image data into your computer in the first place?

    If you can get that line of pixel data into a Propeller we have a Fourier Transform object that will handle 1024 samples in 50ms or so.
  • CuriousOneCuriousOne Posts: 931
    edited 2013-03-08 04:05
    My idea was to use simple highpass filter to detect higher frequency components in signal, not to mess with FFT or other, deep math areas.
  • Heater.Heater. Posts: 21,230
    edited 2013-03-08 05:34
    No messing with deep maths is required. There are a couple of ready made FFTs for the Propeller:
    http://forums.parallax.com/showthread.php/128292-Heater-s-Fast-Fourier-Transform.

    http://propeller.wikispaces.com/FFT

    Just put your signal values in an array call the FFT function and when it's done the frequency components are in the array.

    What I'm curious about is how you are going to get the signal out of the camera.

    Let's say you had a video camera and could get to the video signal. Feeding that into an analog filter circuit seems like it might now work as the thing will be swamped by the synch pulses producing noise all over the spectrum.

    However if you can get that signal chopped up digitally you can skip the non-picture parts of the signal and process actual image data with FFT and/or whatever.
  • kwinnkwinn Posts: 8,697
    edited 2013-03-08 08:49
    Heater. wrote: »
    ................
    What I'm curious about is how you are going to get the signal out of the camera.

    Let's say you had a video camera and could get to the video signal. Feeding that into an analog filter circuit seems like it might now work as the thing will be swamped by the synch pulses producing noise all over the spectrum.

    However if you can get that signal chopped up digitally you can skip the non-picture parts of the signal and process actual image data with FFT and/or whatever.

    I have seen something like this done a while back using an analog switch to select the central portion of the video image and passing that to a high pass filter followed by an integrator. After each frame of video the voltage on the integrator was measured and used to control the focusing. IIRC they stored the voltage readings over the range of motion and used the peak reading as the focus position. There was an awful lot of analog and digital circuitry involved, although it might be much simpler to do with what is available today.
Sign In or Register to comment.