Is there a way to prevent auto-focus from focusing, using any of the "standard" libraries, such as OpenCV, EmGU, DirectShow, etc?
I want auto-focus to find the optimal focus, then during video capture, prevent auto-focus from auto-focusing.
I know I can manually set the value, but that defeats the purpose of using auto-focus, and I have yet to find a way to obtain the optimal focus value as determined by the auto-focus.
You can detect when the image is focused during calibration phase (when you are finding the optimal focus) and save that configuration (focus distance). Then set the focus to the saved value and disable auto-focus before capturing phase. To find the optimal focus distance you can start with the most close (macro) focus distance and gradualy raise it to maximum, measuring how focused the image is.
This SO question has an answer that describes how to measure if the image is focused or not. You can use OpenCV Laplacian() (Emgu.CV) to achieve that.
Another interesting way to determine best focus is described in this article. The technique is used in the NASA Curiosity Mars Rover. The idea is to JPEG-compress the frames and use the size of jpegs as the measure of focus.
OpenCV imencode() (Emgu.CV) can be used to compress the image in JPEG.
If you want to focus on some specific stable object or area and you are able to calculate / recognize its fixed position, you should process only that area to determine best focus. In the first approach you can apply
Laplacianto cropped rectangular area or even use not rectangular mask for result "focus value" calculation if you know shape of the object. The same is for the second approach - compress only the region of interest you want to focus at. If you want it to process not rectangular areas and know the shape of the region, first set all pixels which do not cover the region you focus at to same color. It will make the algorithm not to take account of regions you do not need to be focused.