I have a similar problem. I take pictures of the moon, and I need to choose the sharpest ones for stacking.
As the images are very similar to each other, it is easy to compare.
I was shooting with a t7i and a Canon 55-250mm f5.6.
After doing a lot of research yesterday, I found some OpenCV routines for working with images.
I still made an experimental program, in C# (but it could be done in Python or others languages). I haven't been able to upload the codes anywhere yet but I'll leave some tips.
I found the result incredible for such a short experiment.
There is a method in the OpenCV library that calculates the sharpness (and focus comes with).
I first convert the temp image to grayscale and then calculate the sharpness.
I've only tested it with the final jpg, but it will probably work fine with RAW and other extensions as well.
See the most relevant part of my code:
//Convert the image to grayscale(just temp image for calc, the original image still the same)
//Calculate the sharpness
var laplacian = new Mat();
MCvScalar mean = new MCvScalar();
MCvScalar stddev = new MCvScalar();
CvInvoke.MeanStdDev(laplacian, ref mean, ref stddev);
return stddev.V0 * stddev.V0;
This "return" would already be the calculated sharpness, in decimals.
In my tests, of the 298 photos I took 146 were above average in sharpness and I saved them in a separate folder to reuse in stacking.
The stacking result in Registax is much better.
Average sharpness across all photos: 14.86047
Above average quantity: 146
Little sample of what kind of results can by achieved(note gaps for excluded images). The greater the number, best sharpness:
I generated two images, in one I stacked all 298 photos and in the other only the 146 images with above average sharpness. I passed these two images resulting from stacking in the sharpness evaluation and the result was this below. Something like 30% more sharpness in the final result. The greater the number, best sharpness:
In short, I really enjoyed the result and I recommend that anyone who is interested should look for more on the subject. I make myself available to help in any way I can.