The past couple of days I have spent researching algorithms for use in FocusMac. The basis for the implementation comes from here:
http://users.bsdwebsolutions.com/~larryweber/ITSPaper.htm
Reading through this paper, the main concept that is required for focusing is a way to compute the Half Flux Diameter (HFD) of a star image. Searching the 'net, I came across a couple of simple algorithms for computing the HFD. However, these algorithms do not produce an accurate value when the star image gets very small (i.e. close to focus). This is because the algorithms do not perform the computation with subpixel accuracy. That is, a given pixel is effectively treated as being fully inside the HFD circle, or fully outside. In practice, an HFD of something like 2.36 pixels around the centroid of a star will clearly describe a circle that passes through a ring of pixels that lie partially inside and partially outside of the circle. To accurately compute the HFD, it is necessary to precisely account for how much of the area of each pixel falls within the HFD circle. Thus, I needed an algorithm to compute the area of overlap between a circle and an arbitrary rectangle. Googling for this turned up the following link:
http://idlastro.gsfc.nasa.gov/ftp/pro/idlphot/pixwt.pro
I have translated this algorithm into Objective C and done some basic tests and it seems to perform very well and produces quite a precise measurement of pixel weight. I will now be able to use this as the basis of another algorithm for computing the actual HFD using an iterative numerical method approach. The result should be a very accurate measure of the HFD of a star image.
Of course, having an algorithm to compute the HFD of a star isn't much good if you've just got an image from the camera and it's full of stars and you don't know where they are exactly. So another key set of algorithms will be needed to process raw images coming from the camera to identify all of the stars and find the brightest one, or perhaps allow the user to select one. This is the kind of thing that auto-guiders have to do and so it is no surprise that Google turned up this link:
http://www.mso.anu.edu.au/taros/DC-TAROS-03-AG003.pdf
Here there are a few different algorithms described for finding stars in an image and computing their centroids. From the tests described in the document, it seems that the most accurate algorithm for centroids was the Intensity Weighted Position algorithm. However, the testing described only referred to images containing one star and so the accuracy may vary with different images. This is something I plan to experiment with. Currently I am thinking of using a combination of techniques. It seems like the IWP algorithm is good for computing the centroid of a star, but it doesn't really offer a good way to find the stars in a larger image. The Bright Star algorithm seems better suited to this task. So my plan is to subtract the sky background using the first part of the IWP algorithm, then search for the rough position of stars using the Bright Star algorithm. Finally, I will compute the centroids of the stars found using the IWP algorithm and auto-select the star with the highest total flux as the one to use for focusing. At some point I will probably also provide a way for the user to override this automatic selection.
Once I have managed to implement and test all of these algorithms, I'll be able to start interfacing with my focuser and start trying to produce some V-curves as described in the FocusMax algorithms. I'm looking forward to this part, but the journey to get there will also be quite interesting!