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)
CvInvoke.CvtColor(originalImage, gray,
Emgu.CV.CvEnum.ColorConversion.Bgr2Gray);
//Calculate the sharpness
var laplacian = new Mat();
CvInvoke.Laplacian(gray, laplacian,
Emgu.CV.CvEnum.DepthType.Cv64F);
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
Maximum: 20.89885
Minimum: 9.91646
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:
IMG_0946: 14.955297037037036
IMG_0948: 18.22463111111111
IMG_0950: 14.944277037037036
IMG_0952: 16.061483703703704
IMG_0957: 14.979610370370372
IMG_0963: 16.130337037037037
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:
IMG_0940_ONLY_146_SHARPEST: 306,70520267333194
IMG_0940_ALL_298_IMAGES: 232,35437676592045
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.