An application for auto-focus

General discussion about topics related to OpenMV.
bogflap
Posts: 10
Joined: Mon Apr 16, 2018 10:03 am

An application for auto-focus

Postby bogflap » Wed Apr 18, 2018 10:15 am

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
User avatar
kwagyeman
Posts: 1771
Joined: Sun May 24, 2015 2:10 pm

Re: An application for auto-focus

Postby kwagyeman » Thu Apr 19, 2018 2:04 am

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

https://www.dhgate.com/product/m12-moun ... SsQAvD_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).
Nyamekye,
bogflap
Posts: 10
Joined: Mon Apr 16, 2018 10:03 am

Re: An application for auto-focus

Postby bogflap » Thu Apr 19, 2018 5:17 am

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?
User avatar
kwagyeman
Posts: 1771
Joined: Sun May 24, 2015 2:10 pm

Re: An application for auto-focus

Postby kwagyeman » Thu Apr 19, 2018 11:37 am

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?
Nyamekye,
bogflap
Posts: 10
Joined: Mon Apr 16, 2018 10:03 am

Re: An application for auto-focus

Postby bogflap » Mon Apr 23, 2018 5:04 am

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
https://www.pyimagesearch.com/2015/05/2 ... nd-opencv/
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 (https://www.seeedstudio.com/Seeedstudio ... -2996.html, £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
User avatar
kwagyeman
Posts: 1771
Joined: Sun May 24, 2015 2:10 pm

Re: An application for auto-focus

Postby kwagyeman » Tue Apr 24, 2018 1:37 am

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?
Nyamekye,
bogflap
Posts: 10
Joined: Mon Apr 16, 2018 10:03 am

Re: An application for auto-focus

Postby bogflap » Tue Apr 24, 2018 4:31 am

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.
User avatar
kwagyeman
Posts: 1771
Joined: Sun May 24, 2015 2:10 pm

Re: An application for auto-focus

Postby kwagyeman » Wed Apr 25, 2018 2:29 am

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.
Nyamekye,
User avatar
kwagyeman
Posts: 1771
Joined: Sun May 24, 2015 2:10 pm

Re: An application for auto-focus

Postby kwagyeman » Wed Apr 25, 2018 2:29 am

Nyamekye,

Return to “General Topics”

Who is online

Users browsing this forum: No registered users and 1 guest