Search code examples
vb.netimageautofocus

Scan images for any in focus points


Good afternoon,

I am hoping someone might be able to point me in the right direction, I am about to start work on an X/Y/Z photo rig, which stacks images across the Z axis to form one 'in focus' image.

I have two options, the first is to do a video and pull frames out of it using something like FFMPEG, or the second option is to take a number of images then join them into one image.

I am leaning towards the first option (e.g. the video) because although the quality is lower, each field of view is fairly small so it will get turned into a higher quality pano at the end.

My question is though around processing the FFMPEG stack of images, the way the focus stacking works is that it pulls an in focus part of the image, with other in focus parts from other images to form one image, the issue is though with a video there may be parts that are not in focus at all which I would like to discard, my thought was if I end up with an array of filenames to scan, I could scan through them and pick the ones in focus programatically, however I am not sure how people determine the focus, or the level of focus even would be ideal!

So long story short, does anyone have any vb.net / .net tips on how to determine the level of focus within a color image (e.g. frame of a video) in terms of a numerical value, which I could then use to determine the level of focus?

Many thanks!


Solution

  • For those that are interested, I solved this using a technique known as Sobel Edge detection and a library called AForge.Net, basically you feed the library a bitmap and it goes through the image, converts it to grayscale and tries to determine the edges.

    After this had been achieved, I looked at the standard deviation of the edges and if you plot an image from out of focus, through to optimal focus, and then out of focus again it sort of represents a bell curve. Finding the maximum equates (in my case) to the most 'in-focus' part of the image, whilst if you take a percentage of the curve it can capture more of the 'contains focus' parts of the image.

    Hopefully this helps someone else :).