optrace
  • Examples
  • Installation
  • User Guide
  • GitHub Repo
/
  • 1. Example Gallery
  • 2. Installation
  • 3. Quickstart
  • 4. User Guide
    • 4.1. Overview
    • 4.2. Base Shapes (Surface, Line, Point)
    • 4.3. Elements (Lens, Ray Source, …)
    • 4.4. Geometry Groups
    • 4.5. Raytracer
    • 4.6. Refractive Indices
    • 4.7. Spectrum Classes
    • 4.8. Image Classes
    • 4.9. Focus Search
    • 4.10. Paraxial Analysis
    • 4.11. PSF Convolution
    • 4.12. Plotting Functions
    • 4.13. GUI Interaction
    • 4.14. GUI Automation
    • 4.15. Advanced Topics
  • 5. Implementation Details
    • 5.1. Property Sampling
    • 5.2. Tracing Procedure
    • 5.3. Heisenberg Uncertainty Ray Bending
    • 5.4. Image and Spectrum Rendering
    • 5.5. Ray Transfer Matrix Analysis
    • 5.6. PSF Convolution
    • 5.7. Focus Methods
    • 5.8. Color Management
    • 5.9. Miscellaneous
  • 6. API Reference
    • 6.1. Library Structure
    • 6.2. Global Module
    • 6.3. tracer Submodule
    • 6.4. tracer.color Submodule
    • 6.5. tracer.geometry Submodule
    • 6.6. tracer.geometry.surface Submodule
    • 6.7. tracer.image Submodule
    • 6.8. tracer.spectrum Submodule
    • 6.9. tracer.presets Submodule
    • 6.10. plots Submodule
    • 6.11. gui Submodule
  • 7. Development
    • 7.1. Testing
    • 7.2. Documentation
    • 7.3. Packaging
    • 7.4. Notes
    • 7.5. Changelog

On this page

  • 4.9.1. Focus Modes
  • 4.9.2. Limitations
  • 4.9.3. Usage
  • 4.9.4. Cost Plot
  • 4.9.5. Mathematical Formulation of the Methods
    • 4.9.5.1. RMS Spot Size
    • 4.9.5.2. Irradiance Variance
    • 4.9.5.3. Image Sharpness
    • 4.9.5.4. Image Center Sharpness
  1. optrace /
  2. 4. User Guide /
  3. 4.9. Focus Search

4.9. Focus Search¶

4.9.1. Focus Modes¶

The following focus methods are available:

RMS Spot Size

minimal variance of the lateral ray position

Irradiance Variance

highest irradiance variance

Image Sharpness

sharpest edges for the whole image

Image Center Sharpness

sharpest edges in the center region of the image

The methods use all available rays, for better results the scene should have been traced with much rays as possible. The methods are explained in more detail down below.

There are multiple applications for focus search, below you can find method recommendations.

Case 1: Perfect, nearly ideal focal point
  • Examples: Focus of an ideal lens. Paraxial illumination of a real lens

  • Preferred methods: RMS Spot Size. Irradiance Variance is also suitable, but has worse performance.

Below you can find an example. Both RMS Spot Size and Irradiance Variance find a similar focal position, differing only in 70 µm. Note the different scaling of the images.

Table 4.12 Comparison between “RMS Spot Size” (left) and “Irradiance Variance” (right) in linear (top) and logarithmic lightness values (bottom). Example Double Gauss. Focus search for ray source 0 only. 2 million rays.¶
../_images/focus_gauss_rms.webp
../_images/focus_gauss_var.webp
Case 2: Strong aberrations or no distinct focal point
  • Examples: Lens with large spherical aberration, multifocal lens

  • Preferred methods: Irradiance Variance.

In the following example there are noticeable amounts of spherical aberration. RMS Spot Size tries the minimize the radial distance of the outer rays, sacrificing a sharp core. Irradiance Variance correctly finds a suitable focal position. Note the logarithmic plots, that show the outer rays. In the right case they merely contribute to the image, but they have large impact on the RMS, why the RMS method fails.

Table 4.13 Comparison between “RMS Spot Size” (left) and “Irradiance Variance” (right) in linear (top) and logarithmic lightness values (bottom). Example Spherical Aberration.¶
../_images/focus_sphere_rms.webp
../_images/focus_sphere_var.webp
../_images/focus_sphere_rms_log.webp
../_images/focus_sphere_var_log.webp
Case 3: Finding the optimal image distance
  • Example: Actual image position (not just the paraxial) in a multi-lens setup.

  • Preferred methods: Image Sharpness. With large amounts of curvature of field Image Center Sharpness should be selected, to find a best-fit focus for the image center region.

For the image sharpness methods to work best, a source image with high contrast and sharp edges should be used. For instance, the grid or Siemens star presets, depicted in table Table 4.11.

In the following figure you can find an example for image sharpness focussing for a setup with large amounts of field of curvature. While in the left case more image regions are somewhat sharp, in the right case the sharpness is optimized for the center region.

Table 4.14 Comparison between “Image Sharpness” (left) and “Image Center Sharpness” (right) for a setup with large amounts of field of curvature. Example Image Render, Grid image, pupil of 1mm, 5 million rays.¶
../_images/focus_image_sharpness_grid.webp
../_images/focus_image_center_sharpness_grid.webp

4.9.2. Limitations¶

Limitations include:

  • due to restrictions of the search region the search can’t find a focus that lies between the maximum and minimum z-value of a surface

  • rays absorbed in the search region by the raytracer outline are handled as not absorbed

  • in more complex cases only a local minimum is found

  • see the limitations of each method below.

4.9.3. Usage¶

For focus search you will need to trace the Raytracer geometry. The focus_search function is then called by passing the focus mode and a starting position. The search takes place around the starting point, with the search region between the largest z-position of the last aperture, filter, lens or ray source and the smallest z-position of the next aperture, filter, lens or outline.

res, fsdict = RT.focus_search("RMS Spot Size", 12.09)

focus_search returns two results, where the first one is a scipy.optimize.OptimizeResult object with information on the root finding. The found z-position is accessed with res.x. The second return value includes some additional information, for instance needed for the cost plot, see Focus Search Cost Function Plots.

By default, rays from all sources are used to focus_search. Optionally a source_index parameter can be provided to limit the search to a specific ray source.

res, fsdict = RT.focus_search("RMS Spot Size", 12.09, source_index=1)

If the output dictionary fsdict should include sampled cost function values, the parameter return_cost must be set to True:

res, fsdict = RT.focus_search("RMS Spot Size", 12.09, return_cost=True)

This is required when plotting the cost function using focus_search_cost_plot, see Focus Search Cost Function Plots. It is deactivated by default to increase the performance of methods "RMS Spot Size", "Irradiance Variance"

4.9.4. Cost Plot¶

Note

Generally it is recommended to plot the cost function of the optimization so one can see if there are multiple minima and how distinct the found value is. The TraceGUI has an option for plotting the cost function.

See Focus Search Cost Function Plots.

4.9.5. Mathematical Formulation of the Methods¶

4.9.5.1. RMS Spot Size¶

Minimizing the position variance \(\sigma^2\) of lateral ray positions \(x\) and \(y\) at axial position \(z\). All positions are weighted with their power \(P\) when calculating the weighted variance \(\sigma^2_P\). The Pythagorean sum is applied using both variances to get a simple quantity \(R_\text{v}\) for optimization.

(4.22)¶\[\text{minimize}~~ R_\text{v}(z) := \sqrt{\sigma^2_{x,P}(z) + \sigma^2_{y,P}(z)}\]

This procedure is simple and performant. However, the disadvantage of this method is that it minimizes the position variance of all beams. For example, if there is a strong outlying halo, the method also tries to keep it as small as possible, which can lead to a compromise between the halo and the size of the actual focus.

4.9.5.2. Irradiance Variance¶

Renders a power histogram for rays at position \(z\). This histogram is divided by pixel area to get an irradiance image \(E(z)\) The approach then calculates the variance of the pixel values and finds the \(z\) with the largest variance.

The most outside rays define the image dimensions, the absolute image size therefore varies along the beam path. This can be an issue when few rays are far away from the optical axis, since the resolution suffers because of these marginal rays.

The variance is large when there are bright areas in the image (with much power per area) or if there is a large variance between pixels, which should be the case if unblurred structures are present. For a minimization, the variance is inverted. For a more smooth cost function and a better data range the square root of the variance is used.

(4.23)¶\[\text{minimize}~~ I_\text{v}(z) := \frac{1}{\sqrt[4]{\sigma_E^2(z)}}\]

4.9.5.3. Image Sharpness¶

The power image \(P(x, y, z)\) is transformed into the Fourier domain, creating a Fourier power image \(p_f\) with image frequencies \(f_x\) and \(f_y\). Using the Pythagorean theorem we can join the frequency components into a radial frequency. The radial frequency of each pixel is scaled with the corresponding pixel power. We want to maximize this product, which is large when there are many high frequency components in the original image \(P_z\) or when high frequency components have a high power.

(4.24)¶\[P_f(f_x, f_y, z) = \mathcal{F}\left\{ P(x, y, z)\right\}\]
(4.25)¶\[\text{minimize}~~ F_\text{p}(z) := \frac{1}{ \sqrt{\sum_{x,y} P_f(f_x, f_y, z) \left( f^2_x + f_y^2 \right)}}\]

For a minimization, the term is normalized by the pixel count \(N\) and inverted. This method is independent of the image size, as only the power image and not the irradiance map is employed.

A disadvantage of this method is that it tries to maximize the sharpness of the whole image. Only a compromise solution is found for images with spatial varying blur.

4.9.5.4. Image Center Sharpness¶

To put more emphasis on the image center, the following weighing function is applied:

(4.26)¶\[\begin{split}w_r = \begin{cases} (1 - r^2)^2 &~~\text{for}~ r \leq 1\\ 1 & ~~\text{for}~ r > 1\\ \end{cases}\end{split}\]

Here, \(r\) is the normalized image radius with values between 0 and 1, describing the radial position on the pixel grid. The Fourier transform is then:

(4.27)¶\[P_{f,w}(f_x, f_y, z) = \mathcal{F}\left\{ P(x, y, z)~ w_r \right\}\]

From here on, the steps are the same as for the method Image Sharpness.

Previous
4.8. Image Classes
Next
4.10. Paraxial Analysis
© 2025, Damian Mendroch     Impressum     Legal Notice

Made with Sphinx and Shibuya theme.