Determine Image Focus

I need to determine how well-focused or “blurry” an image is. There are of course ways of doing this involving FFTs and Laplace transforms but they’re a little over my head and beyond the ROI for me for this project, although it’d be a great feature to add if it can be done using tools I’m familiar with, like MBS.

This article opencv - Calculating sharpness of an image - Stack Overflow and several others on Stack Overflow discuss various methods. It can almost certainly be done with OpenCV, but it doesn’t look like Xojo OpenCV is getting a lot of love these days and again there looks to be a significant learning curve involved.

I would think that somewhere in ImageMagickMBS there is a way to do it - anyone have experience or recommendations?

Hi Julia,

There shouldn’t be too much of a learning curve with Xojo OpenCVC. Please give the project a try and let us know what you’re able to do. Xojo OpenCVC is basically a Xojo project with a single test window you can look at to see how an image is loaded, processed, and displayed. It’s still a bit of a work in progress but unfortunately I haven’t been able to spend any time on it this past year so i’m rusty (and so swamped I probably can’t get into answering any in-depth questions), but I’d love to know what you think.

We’re in the midst of packing because we’re finally moving into a new office in a few weeks. I won’t be working on any xojo stuff until at least February or March, most likely, when we’re all settled in and back to normal. But what you’re describing is essentially what we plan to do for an auto-focus system on our film scanner (comparing multiple scanned files to determine which camera/lens position has the best focus)

I’m pretty sure all that you need should be implemented in the current iteration of Xojo OpenCVC, I think.

I found a few mentions on the 'net that standard deviation can be a useful metric of focus quality. It turns out to be very easy to get std dev with GraphicsMagickMBS, so I checked it on a few test images and the result is not useful - there is almost no difference in std dev between my out-of-focus test images and in-focus ones (in fact, some of the sharper images have lower std dev than the blurry ones). Oh well, I didn’t really expect it to be that easy :slight_smile:

@Perry_Paolantonio ,

did you have the time to look at the crash I reported some months ago? My app does batch resizing of images and Open CV is much faster than the solution I have now.

Our plan was to use canny edge detection. more edges should equal more focus. That’s grossly oversimplified, but it should work.

No, sorry - we’ve been completely swamped with other stuff and I haven’t been able to do anything with Xojo OpenCVC for months. I did look into it at the time, but our operating systems are different, and I think the issue might be related to your use of MIME encoded images. The test application with the generic test images we’ve been using works fine, so that leads me to believe it’s something to do with the images you’re feeding it, maybe.

Thanks for the info. My images are garden variety email attachments. Why should there be anything special about them?

I’m going by memory but I recall there was something about your images that was different.

Can you open the project and load the default image using the “Load Image” button? After you do that it will display the dimensions and you can use the Show button to display it. I took a peek at the code and in the LoadImage method you can uncomment the second line that defines folder item ff. I had to change the filters though - I’m guessing that line was incomplete. I am able to load up any TIFF, PNG, or JPEG I’ve got handy, and I tried several. Change the line to something like:

Var ff As FolderItem=FolderItem.ShowOpenFileDialog("image/jpeg")

And see if that loads up one of the email images.

I’m testing on 2022r2 on Big Sur. I really can’t get into it any more than that because I’m packing today, but the project definitely works with the default StarryNight image, and with any image I specify if I uncomment the ff folderitem line and point it to a file of the correct type.