An application for auto-focus

The project i am implementing at present is using a LIDAR unit to detect bees leaving and entering their hive.
The LIDAR has a narrow beam angle on it so I am using it to measure a distance in front of a camera so that when the LIDAR detects something within a minimum and maximum range then the camera takes a picture.
Volia, pictures of bees.
I read one of your posts about MTF Calculations which tended to go over my head a bit but the general gist seemed to be whether the OPENMV could implement auto-focus functionality.
If it can then I can use this instead of a LIDAR unit. If it can be detected that something is in focus then it must be a known distance away from the camera. If the picture the camera is seeing is in focus then save that picture.
Volia, again pictures of bees.
Note that the LIDAR and auto-focus methods do not attempt to focus the camera they both detect whether something has entered a defined volume of space in front of the camera.
The camera I am using at present has an 85 mm lens on it and it is focused at about 30 cms in front of the camera. This gives me a depth of field of about +/- 5cms which gives reasonable pictures of the bees.
Okay so I now have a trade off between aperture and speed but the OpenMV unit does not implement aperture control and so another story.
Why bees, well these pictures are then used to detect whether the bee is carrying a Verroa mite. But that is another story.

Anyway the question is.
Is it possible within OpenMV to implement the auto-focus functionality. There are a large number of auto-focus cameras out there but none that I have found where I can use the functionality in this way

Hi, You can buy lenses that have the ability to change focus:

https://www.dhgate.com/product/m12-mount-1-3-inch-2mp-ir-cctv-ip-camera/399335135.html?utm_source=pla&utm_medium=GMC&utm_campaign=close_to_u2017&utm_term=399335135&f=bm%7c399335135%7c007003%7cGMC%7c812091101%7cpla%7cclose_to_u2017%7cUS%7c007003012%7cc%7c%7c2%7c&gclid=CjwKCAjw2dvWBRBvEiwADllhnwmigJ9w8QWAgqmQI4dU247uIIFnZf6sEEn8_h4Su2I-md49vKQf4xoCWSsQAvD_BwE

As for the MTF method. It’s not in the firmware right now… but, I can get it into the release after next which is about to come out.

(Or you can use the firmware I posted with that method on the forums).

After an afternoon of searching the internet it looks like the keywords I need are “Trap Focus” and “Focus Priority”.
It appears that some higher end cameras offer these facilies but not the ability to be left unattended taking photographs.
This is what my project requires as the intention is not to get individual images but to automatically acquire hundreds or even thousands of images depending on the activity at the hive entrance.
Later analysis of the images is what is intended at present but if the M7 (or later) can detect the Verroa mite (they are small reddish brown mites that cling to the backs of bees) as well then that will be ideal.

So to re-frame my question. Does anyone know of an algorithm that can be implemented on the M7 (or later) that will enable the “Trap Focus” functionality?

Hi, seeing if there’s a blurry object in the field of view isn’t I think the best way to detect an object. Instead, you may wish to look at adaptive frame differencing and etc.

The MTF calculation kinda looks at the full image so it’s not going to find some out of focus object.

But, let’s backup. First, for what you want to do you need to define the algorithm steps. First, you want to detect a blurry object. That means you need to see where that object is in the frame. So, we need some type of localization method to find said blurry object in frame. Once that’s done you can do the MTF calculation on just that ROI to determine how out of focus that area is. But, you have to find the ROI first. Any idea on how you’d want this to work?

Okay I understand where you are coming from as far as the ROI is concerned. I had this problem initially in that I found it easy to obtain pictures simply by taking the difference between sequential frames.
I used the code here as the basis for my code

This works okay but at that time I had a more wide angle lens (12mm M12) which gave me images containing numerous bees. I can extract the bee parts of the image but they were far to small e.g. 100x40 pixels.
So I moved on to a different camera with an an 85mm zoom lens (Nikon FX lens) and used the LIDAR to trigger image capture in a very small volume of space.
The camera is set to manual focus and focused in the centre of the target volume of space (get focus algorithm to work first and worry about DOF, aperture and shutter speed later).
Now I get big and clear images of the bees. Okay so I do not get all the bees leaving and entering the hive (the entrance is about 15 cm by 1.5 cm) but I get a-lot of them.
However the images I get are almost entirely bee so to speak and so the ROI is of less concern.
I am looking at possible ways of eliminating the LIDAR. The cost of the LIDAR is not that much (Seeedstudio Grove - TF Mini LiDAR, ToF (Time of Flight) Distance Sensor - Seeed Studio, £40.00 to £50.00) and I use a Raspberry Pi at present to control it and the tethered camera (yes it work just fine with the Raspberry Pi).
However in my opinion the system would be easyily and more widely adopted if the LIDAR and Raspberry Pi program was not required.
Hence my interest in the M7 and the possibility of using it, by detecting focused images, for this application

Okay, I think I see what you want to do…

Um, so,t he feature is not present right now in the firmware. But, I can get it in really quickly after this next release. Basically, you’d like to just use the get_fft_histogram() method on an ROI near the center of the camera. This will then give you a histogram plot of the frequency magnitudes. When in focus you’ll get a nice peak… otherwise no such peak. A threshold on the histogram then gives you your answer.

Anyway, I think you an do this at 30 FPS or so.

Sound about right?

Looks good to me.
As an aside, is Discrete Cosine Transform implemented or to be implemented.
From my reading this is faster than an FFT. However if you are confident about 30 fps then it does not really matter.

We just have general purpose 2D FFT code right now. However, there’s DCT code on the system too. It’s just for JPEG right now though.

https://github.com/openmv/openmv/blob/master/src/omv/img/fft.c

Cannot see it in the documentation anywhere

It’s not a user callable method…

Is it possible to get at the statistics?

Hi, so, I made an algorithm for a poster a while back that provided a 2D FFT of the image. The binary for that is on the forums if you search for “MTF”. I don’t have time to re-release that again right now however. I have to work on the KickStarter.

Hello, I am trying to get a numerical value of the standard deviation of the histogram function, preferably from the colour

Hi, I answered your question in another post.

Thankyou here also sir.

do you have a patreon account and i could spare you a few dollars, you have solved a six month problem for me.

PayPal is kwagyeman@openmv.io