temporalecologylab / TreeRings

Project developing methods and tools needed to collect tree ring dendrology data
2 stars 0 forks source link

Automatic control, focus, and background classification #31

Open afong3 opened 2 months ago

afong3 commented 2 months ago

The automatic control relies on information gathered from the following focusing algorithm. A Realistic sample setup results in the in focus distance to be at varying Z-values across the cookie. To combat the Realistic sample setup, at every image coordinate, multiple images are taken from different heights. For each image in the set of n-images, a normalized variance score is calculated - the maximum value is taken as the most in focus image of the stack.

This is done because 1) The surface of the cookie is not guaranteed to be perfectly level 2) It is impossible to perfectly level the cookie w.r.t. the camera

image

This works great if the range of the varying heights of the cookie are within the bounds of the height range used in the focus algorithm. Unfortunately, the bigger the cookie gets, the greater the levelling error and sanding errors become - requiring a height range so large that it would take a prohibitively long amount of time to capture all the images. This is where the automatic control comes in.

The index ( i ) of the most in focus image in the set is very important. If i == n / 2, this means the height range will have the best chance of including an in focus image in the next location. If i == n, this means the z-height was too high and the most in focus image will potentially be outside of the next set. If i == 0, this means the z-height was too low and, again, the most in focus image may not be in the next set.

So, the PID algorithm constantly tries to achieve i == n / 2 by using the z-height as the control variable.

image

Informing the PID algorithm of the in focus index is only important when there is a subject that should be in focus. While traversing the cookie, there are multiple field of view options for the camera. FOV1 contains entirely background, FOV2 contains a mix of background and cookie, and FOV3 contains only cookie. It is very important to be able to identify FOV1 from FOV2 and FOV3 for the purpose of automatic control. In short, if FOV1 is detected, it is crucial that no information is fed to the PID controller otherwise the system will try to focus itself to the background and derail the focus when the system returns back to the cookie.

The current method to classify FOV's is to analyze the distribution of normalized variance scores previously calculated in the focusing algorithm. When comparing the set normalized variance scores from an in focus FOV2 or FOV3 with an out of focus FOV1, they are generally separable with a simple threshold of each set's standard deviation. Empirically I've found that a blank background (such as cardboard NOT the 3D printed grid) increases the efficacy of this threshold. Also larger fields of view (lower zoom) seems to converge these normalized variance scores. The efficacy decreases but I haven't been able to think of another solution yet.