(This page describes how to use LoCA Focus Analyser, a web-page-based tool that provides information about a microscope or camera's ability to bring colour channels into simultaneous focus.)
Fig 1. Edge-spread vs position along field of view (which, because the target is inclined, is equivalent to 'edge-spread vs focal distance'). In this case, the three colour channels come to best focus, ie., minimum edge-spread, at different focal distances, indicating the presence of longitudinal chromatic aberration.
Longitudinal chromatic aberration is an optical defect that causes focal distance and sharpness to vary with light frequency (colour). LoCA Focus Analyser is a tool to assess focus and longitudinal chromatic aberration of a (stereo)microscope or camera (macro) lens.
LoCA Focus Analyser works by analysing a photomicrograph of a inclined straight-edged target (eg., a razor blade edge). The target is brought to focus near the middle of the field of view. Since the target is inclined with respect to the optical axis (see figure 3), each colour channel will be at best possible focus somewhere along the target edge. Ideally all channels would come to best-focus at the same point along the edge, ie., at a single point of focus, but due to flaws that is usually not the case; often channels come to best-focus at different distances, and to different degrees. For each colour channel, Focus Analyser charts how the degree of focus varies across the field of view and makes evident how well they align.
For example, the results shown in figure 1 from a stereomicroscope indicate that the colour channels of that particular microscope do not come to best focus (y-axis) at the same focal distance (x-axis), and that green is focused better than red or blue.
To do the analysis, all it takes is a single photomicrograph of a inclined straight-edge, prepared as described below.
The goal is to produce a photomicrograph like the one shown in figure 2, with the following characteristics, in order of importance:
Fig 2. Desired image: Straight edge, uniform light and dark regions, inclined, best-focused near the middle, naturally white balanced, slanted at ~5 degrees.
Finding an edge that is straight and uniform, even when viewed through a microscope or macro lens, presents a challenge. The best I've come up with is to use a (single sided) straight-edge razor blade, one's like these. To make one side of the razor blade featureless and dark, I immerse it briefly and repeatedly in a candle flame to deposit thin carbon coatings, just enough to make it uniformly dark, taking care not to create lumps (it's easy to wipe off a deposit and try again).
The blade is then photographed either with top or back lighting. Beware of lint and dust settling on the blade.
With top lighting, the blade can be supported a centimeter or so above a uniform white surface (the gap causes the lower white surface to be out of focus and thus more uniform). The inclined and elevated target (eg., figure 3) is then illuminated obliquely so there is no shadow visible to the microscope or camera. A wedge can be used to increase the angle with the optical axis. I cut a wedge from wood, or one could mount the target flat and instead angle the camera (measured, perhaps, using a smartphone inclinometer/bubble-level app).
For stereomicroscopes, you can simply position the blade parallel to the stage and rely upon the published convergence angle of that stereomicroscope to provide an angle with respect to the optical path (however, as in figure 3b, I wanted a larger angle, so I augmented the scope's convergence angle with a wedge; the total angle in figure 3b is thus the convergence angle + the wedge angle).
![]() Fig 3a. Blacked razor blade, inclined with respect to the optical axis |
![]() Fig 3b. Blacked razor blade, inclined with respect to the optical axis (red line) |
Even with back lighting, a carbon coating helps reduce reflected ambient light, improving uniformity in the dark region.
Fig 4. Example of a desirable histogram, showing good separation, no clipping, and good colour balance.
The dark region can be either at the top or bottom, but the edge must go across the width of the frame (ie., left-right, not top-bottom). It's best if the edge crosses the center of the frame (the optical axis) so that any lateral chromatic aberration will be parallel to the edge and thus not have much affect.
Using bright, close-to-white-light illumination provides the strongest 'signal' (versus noise) for analysis.
To record the field of view, take a photo of a scale (eg., a ruler) with the same equipment configuration used when photographing the inclined target.
LoCA Focus Analyser analyses edge-spread -- how abruptly the image of the edge makes its transition from light to dark -- so it's important that the image not have been algorithmically 'sharpened', either by the camera's in-built software or during post-processing (eg., conversion from RAW). Unfortunately, some degree of sharpening is often introduced even by low-level demosaicing algorithms, and therefore even 'turning off' sharpening in most photographic raw processing software packages will not prevent sharpening -- there will still likely be significant pixel-level alterations. (This can be explored using Focus Analyser, by comparing the analyses of images prepared using a raw processor vs the method below.)
Fortunately, unaltered raw pixel data can be obtained from raw files using dcraw, a widely-used free software tool created by Dave Coffin. dcraw can extract pixel values from most raw file types and record them in a PGM file, a high-fidelity, lossless format that can be provided to Focus Analyser. dcraw is available for most operating systems and easy to install. Here's a procedure to set up dcraw on Windows:
Now you should be able to select one or more raw files in File Explorer, right-click, and have them processed by dcraw, generating pgm files.
Focus Analyser will also accept PNG and JPG files, but the fidelity of the pixel values from those formats is likely to be lower because: JPG files usually introduce encoding artifacts; browsers accept only 8-bit PNG or JPG files (whereas PGM files generated as describe above can provide Focus Analyser with 16-bit values); and Webkit browsers (Chrome and Firefox) seem to alter PNG values (as if the images are stored internally in a compressed format). So it's best to use PGM files, which carry up to 16 bits per pixel.
PGM files are not compressed and are typically large (maybe double the size of your raw file), but Focus Analyser works locally in your computer so even large files load fairly rapidly. No image data is sent over the internet.
With a PGM file generated by dcraw from your raw file, you're now ready to analyse it using the Focus Analyser web page.
Analyse your photomicrograph by opening the LoCA Focus Analyser web page and clicking the file selection field near the top of the page to select and load your image file. Depending on the file size, the image should appear in the web page within a few seconds.
If your image file is a PGM created by 'dcraw -D -4 -t 0' as recommended, the file will have 16-bit values that are 'linear' (as recorded by the camera sensor). However, if you created the image file some other way, you need to tell Focus Analyser if the values are 'linear' or 'gamma compressed' (see linear RGB). If you indicate the file is gamma compressed, Focus Analyser will convert the values to linear.
Focus Analyser will try to automatically locate the edge, but if necessary, you can use the edge locator controls (yellow) to tell the web page where the edge is located. The yellow rectangles are coarse movement controls; the region they mark is shown in full-zoom in tall, narrow adjacent windows containing yellow triangular controls. After using the coarse controls, use the triangles for fine tuning.
When the analysis line is near alignment with the edge in your image, results should appear in the charts near the bottom of the web page.
Fig 5. Use the draggable controls in the 'edge locator window' to tell the software where the edge is located.
The region of your image marked by the blue rectangle (figure 5) is displayed at full zoom (figure 6) in the window below the edge locator window.
Fig 6. Full-zoom view of the region marked by the blue rectangle in the edge locator window.
At the top-right of the page, there are two fields to enter information about your image and target. 'FOV' is the horizontal field of view, in millimeters, of your image (easily obtained by photographing a mm scale with the microscope set as when photographing the target). 'Incline' is the angle of the target with respect to the optical axis, in degrees. If you are using a stereomicroscope, likely the optical axis of the phototube is at angle of about 6 degrees to the stage. The angle can be measured directly if it's a Greenough design, or can be measured by looking at an object having a vertical surface of known height and seeing how much horizontal image space the vertical surface occupies, then using trigonometry.
Knowing the FOV and the incline angle allows the web page to calculate z-axis positions based on x-axis positions, using trigonometry. Z-axis values are relative to the lower end of the target.
After entering values into those fields, press 'tab' or click somewhere outside the field to cause the value to be recognized.
An ideal target makes the transition from black to white abruptly, and a perfect optical system would transmit an image of that abrupt edge to the sensor. However, in real optical systems, even at best focus, that sharp edge will appear in the image as slightly blurred and spread, as illustrated in figure 7b, due to diffraction, optical defects (eg., aberrations), camera noise, digitization, etc. Rather than jumping abruptly from black to white like the target, the image makes the transition gradually. 'Edge spread' refers to the distance over which the image makes the transition. An edge spread of 0 pixels or microns would be ideal.
7a) Target (with an abrupt transition from dark to light):![]() |
![]() |
7b) Ideal image profile:![]() 7c) Realistic image profile: ![]() |
Fig 7 a) The ideal target makes the transition from black to white in zero distance. b) An ideal image also makes the transition abruptly. c) A real image of an abrupt transition is spread out, even at best focus, due to diffraction, optical defects (eg., aberrations), noise. In this example, the 'edge spread', from where the image crosses the 10%-90% levels (horizontal blue lines), is 19 pixels (the distance between the vertical lines).
All other things being equal, the edge spread distance will be smallest where the focus is best. At the point of best focus, any remaining edge spread is a measure of the quality of the optical system, eg., the resolution of a microscope or the sharpness of a camera lens.
The edge spread is conventionally measured between the 10% and 90% levels, as indicated by the horizontal lines in figure 7b; the spread is the horizontal distance between the two vertical lines, determined by where the image's pixel values cross the 10% and 90% levels.
The more distance you can put between the dark and light regions, the more distinct the edge becomes from 'noise'. That's why a good separation between histogram peaks, on all three colour channels, is helpful (figure 4).
Fig 8. Colour filter array (CFA), in a Bayer mosaic pattern.
Most digital cameras have a colour filter array (CFA) in a Bayer mosaic pattern over their sensors. Figure 8 shows a typical arrangement, RG/GB, which refers to the layout of the filters in the 2 x 2 square of pixels at the top-left corner of the array.
Behind each colour filter is a sensor. The presence of the filter means the sensor measures the amount of light only of that filter's colour. There are as many sensors as there are pixels in the final image. For example, if the final image is 5184 x 3456 pixels (18 MP), that's also how many sensors pixels there are.
Each sensor pixel has only one value, usually stored in 12 to 14 bits. However, each final image pixel is composed of three values (red, green, and blue), usually 8-bits each (8 bits can stored values ranging from 0 to 255). Final image pixels are built from sensor pixels by software in the camera (or, if using raw files, by software in a computer) in processes that include white balancing, demosaicing (using via some form of interpolation), and colour space conversion.
There are many interpolation algorithms. A simple way to build the RGB values would be as follows: For the pixel in figure 8 tagged with the black dot, the obvious choice for its red channel value is the value from that sensor, behind that red filter. A reasonable setting for the green value for that pixel would be an average of all the neighbouring green-filtered sensor pixels, and for the blue, an average of the neighbouring blue-filtered sensor values.
A more sophisticated and commonly-used algorithm is called Adaptive Homogeneity-Directed (AHD), which considers the trend of nearby sensor values to pick RGB values that are better than just averaging. Some algorithms perform better along straight edges; some have the effect of 'sharpening'. Some raw processing software allows some control over the choice of interpolation algorithm, for example, RawTherapee, a popular open-source raw processing tool.
Each camera sensor always just records how much light it receives through its filter; it's a photon counter. The density of the R, G, and B filters need not be tuned to yield equal sensor counts in white light; for example, the Canon EOS 600D seems to have filter densities better matched to blue sky than white light. 'White balancing' is done afterwards, in software, by scaling the three channel values in a way that produces a desired colour balance.
Colour space transformation/conversion maps the raw RGB colours to the desired colour space (eg., sRGB or Abode RGB). Each colour (R, G, and B) is mostly determined by the corresponding raw RGB values, but depending on the camera's colour filters, each is somewhat influenced by values of the other two colour channels. For example, the final image R value might be given by a formula such as 1.9 R - 0.8 G + 0.1 B (where these are the raw values).
Camera manufacturers add information ('metadata') in the raw file about the camera settings, which can be used (or ignored) by the external software. Typically each camera manufacturer has their own way of formating raw file information, and unfortunately they typically don't say much about it or their sensors (or any manipulations they may do); people 'reverse engineer' it. Dave Coffin has collected a lot of this reverse-engineering information in dcraw.
The PGM files produced by 'dcraw -D -4 -t 0' are simple and straight-forward, carrying just the raw sensor values. Most importantly for Focus Analyser, no interpolation, demosaicing, or colour conversion has been performed upon the raw data, meaning as much information as is available about how the light landed on the sensors is made available to Focus Analyser via the PGM-format file. PGM file contents can be viewed using the PGM File Viewer utility.
Fig 9. Creating RGB image pixels from RG/GB sensor pixels: For each 2 x 2 block of RG/GB sensor pixels, R & B are assigned to all four pixels, while G1 and G2 are assigned to just the two pixels on their row.
From the raw sensor values, Focus Analyser constructs RGB pixel values using this simple algorithm: For each 2 x 2 square of RG/GB colour filtered sensors, all four RGB pixels in that square get the red and blue values of the single red- and blue-filtered sensor in the 2 x 2 square; the top two RGB pixels get their green values from the top green sensor, and the bottom two RGB pixels get their green value from the bottom green sensor. This is illustrated in figure 9.
Thus, going down each column of the RGB image, the red and blue values change every two pixels, and the green values change every one pixel; the green channel has twice the vertical resolution of the red and blue channels (simply because there are twice as many green sensors in the camera). The difference in colour resolution is visible in the Edge Profile chart (eg., figure 9b).
This method of constructing RGB images doesn't always make the most pleasing image (mostly because there is no effort toward white balancing or colour conversion), but it is good for Focus Analyser because it preserves information recorded by the sensor about how the lens performed.
Focus Analyser can work with either RG/GB or GR/BG (the same as RG/GB but shifted laterally one pixel). For other sensor arrangements, you can try converting your raw file to PNG (but the analysis may be less meaningful) (or send me a note using the comment link at the bottom of this page).
Although 16-bit PGM files have room for 16 bits, most cameras produce 12 to 14 bits. The values are left-shifted by Focus Analyser to fill the top-most bits so that the images display more brightly and so that the histogram is more meaningful. The bit-shift is reported in the histogram title (figure 12). Active bits (which can depend upon illumination level) are reported under the edge spread chart (figure 11).
The 'Edge profile chart' (example: figure 10a below) shows the pixel values across the edge at the cursor position (middle window), for the 81 pixels within the gap in the cursor line. The scale strip at the left side of the chart gives a feel for the pixel values, ranging from whitest to blackest, in each channel. By default, the chart displays 'white balanced' values; it scales the pixels for each channel such that each channel's 10%/90% levels are the same. This has no effect on the edge spread distance (the positions of the vertical lines) but makes it easier to compare channels. The white-balancing can be turned off by unchecking the box below the chart; figure 10b shows the edge in figure 9a but without balancing.
![]() Fig 10a. White-balanced and normalized. |
![]() Fig 10b. RGB pixel values as found in the image file, unnormalized and not white balanced (ie., left 'as shot'). |
The edge position being profiled is controlled by the cursor slider in the middle window.
Focus Analyser's chart of 'Edge spread distance' (example below, figure 11) shows how the edge spread distance, for each colour channel (red, green, blue), varies across the field of view. Because the target was inclined (with respect to the optical axis), the position along the x-axis also controls the focal distance. For example, in figure 3b, because of the incline (caused by the wood wedge plus the angle of the microscope optical path), the left side of the razor blade is closer to the camera than the right side.
Ideally, all colour channels should come to focus at the same focal distance. Since the incline maps focal distance to horizontal position, that means ideally the three curves should overlap and come to their lowest value (minumum edge spread, ie., best focus) at the same horizontal position in this chart of edge spread vs horizontal position. In figure 8, the three colour channels do not come to best focus at the same horizontal position; thus that optical system exhibits longitudinal chromatic aberration. In addition, the chart shows that the blue channel never reaches the degree of focus (edge spread) reached by the red or green channel.
Fig 11. This chart shows how edge spread varies across the field of view (FOV), which can be related to relative focal distance if information is provided to the web page about the FOV and angle of incline.
The web page uses information you provide (at the top-right of the page) about the FOV width (in millimeters) and incline angle (in degrees) to calculate focal distances (relative to the lower end of the target in the FOV). Once the FOV and incline angle are entered, the web page provides the x- and z-axis positions, in μm, at the top of the vertical crosshair line (which can be dragged over the chart). Thus the z-axis difference between points of best focus in each colour channel can be determined. Focus Analyser simplifies by assuming an infinite distance to the camera and mapping x-axis position to focal distance (z-axis) by using trigonometry: z = x * tan(inclineAngle).
The lowest point on each curve is the point at which the edge spread was smallest, ie., the edge was imaged most sharply. The edge spread remaining at point of best focus is a measure of the resolution in that colour channel. In figure 11, the best (smallest) edge spread of the green channel was 9.2 pixels, corresponding to a resolution of about 28 μm; that optical system can probably resolve points of light ~28 μm apart, in green illumination. To find values on the chart, click-drag on the chart to move the cursor around. The minima for the curves are listed below the chart, providing [zOffset, resolution] for each channel, with offsets relative to the sharpest channel ([xOffset, pxSpread] is displayed if FOV and incline information has not been provided).
The small grey tick marks on the horizontal cursor line are at plus/minus 100 μm in z-axis terms. They can be used to get a feel for depth of field. For example, in figure 11, the bottom of the green curve fits easily within the plus/minus 100 μm ticks, and thus that optical system comfortable has a depth of field of plus/minus 100 μm in green illumination. One can use the curves to see how much defocusing would be associated with what depth of field, and how it would vary with colour channel. For the optical system in figure 11, the curves show that it would be difficult to obtain sharp images in white light in all channels without colour fringes.
By default, the chart values are smoothed using a Gaussian smoothing window 41 pixels wide. The smoothing window can be changed using the field at the base of the chart. Camera sensor noise contributes much of the jitter.
The chart drawing is suppressed/aborted if abnormal values are found -- usually caused by the analysis line not coinciding with the edge in the image. The chart should appear when you use the 'edge locator' handles (described above) to position the analysis line over the edge in the image.
An almost-white watermark is displayed in the bottom-left corner of the chart consisting of version information and the date/time of the analysis. Use an image editor to make it visible.
The edge spread is best revealed by an image that uses nearly the sensor's full range without saturation ('clipping'), eg., from almost-white to almost-black, in each colour channel. This requires white illumination. If a particular colour channel has a smaller range than the others, it may have more susceptibility to noise, which may result in a less defined curve. The white balance can be assessed using the 'Edge profile chart'.
It's usually difficult to illuminate a target uniformly across the entire field of view (FOV), especially at lower zoom levels; also, optical systems may produce vignetting. The 'Illumination levels' chart (figure 12) shows the 'white' and 'black' pixel values, obtained from the extremes of the analysis 'gap' in figure 6, across the FOV. This information is used to set the 10% and 90% levels for the edge analysis at each point across the FOV.
Figure 12 also shows an example of histograms generated by Focus Analyser from the loaded image. The horizontal scale is 256 screen-pixels wide (corresponding to all possible values of 8 bits). Each vertical histogram line reflects the number of image-pixels whose most significant 8 bits correspond to that value on the histogram axis. The vertical scale is logarithmic (each vertical is log(count + 1), scaled by the total number of image pixels). With 'black and white' targets such as used with Focus Analyser, histogram outliers are often 'stuck pixels' (dcraw, as invoked as described above, doesn't mask out dirty or stuck pixels).
Fig 12. Illumination levels across the field of view (FOV) at the 'black' and 'white' ends of the analysis region ('gap' in figure 6).
Lens characteristics likely vary slightly with distance from their optical axis, so Focus Analyser results may vary slightly depending upon where the point of best focus is within a field of view. You could get a feel for this by analysing images focused on peripheral points of the target edge versus the center.
To keep the relationship conceptually simple, Focus Analyser treats z-axis distance (focal distance) as being linearly related to x-axis displacement (across the frame), as if the sensor were infinitely distant. The actual distance is probably in the order of tens of centimeters, so a more realistic (radial) mapping would flatten the curves a bit.
LoCA Focus Analyser is something I made for my own use. I found it to be interesting and useful, so I made this web version so that it could be shared. I'm not a physicist or microscopist, so please be aware that this tool could have bugs and/or errors; don't rely upon it. It's a Creative Commons contribution; there are no warranties, etc.
As a convenience, Focus Analyser stores parameters you enter (eg., field of view) in your browser's local storage (on your computer, like a cookie), using the image's file name as the lookup key. You can remove the information for a single file or all files using links on the Focus Analyser page.
The edge-spread chart has a background watermark, in near-white font, recording the Focus Analyser version and URL, and time/date. The information can be viewed using a photo tool (eg., Photoshop).
LoCA Focus Analyser is intended as a qualitative tool. For a bit more quantitative information, one could use MTF analysis, at least upon the portion of the edge image that is in good focus (with an inclined target, at least one is certain that such a region exists, whereas with an orthogonal target there is a chance that the focus wasn't optimal anywhere).
MTF analysis software usually can be directed to work with just region of an image. You'll have to decide (using information provided by Focus Analyser, for example) if that region is wide enough for the MTF analysis to be meaningful. Here are some MTF analysis packages that I'm aware of:
Web sites associated with the tools list above have interesting reading, and I've got a page (Measuring the resolution of a stereomicroscope: MTF method) describing how I've used MTF analysis in photomicrography.
MTF analyses begin with determining the 'edge spread function' (edge profile) for an edge in an image. Rather than the simple method of scanning pixel columns used by LoCA Focus Analyser, MTF software collects data from many columns along a slanted straight edge. The relationship between Focus Analyser's edge spread measurement and those of MTF software is explored here. In general, Focus Analyser's estimate of the edge spread is about 10% smaller than that found by QuickMTF.
If the edge spread chart doesn't show full curves, make sure the analysis line is aligned with the edge on your image (curve drawing is suppressed if the data has too many issues).
If the curves don't look roughly as in figure 8, perhaps the 'white' and 'black' regions of your target photo are not uniformly 'white' or 'black'; perhaps they contain too much detail. For example, if you didn't carbon-coat a razor blade, likely it will have visible sharpening marks, which introduce too much variation in the 'black' region. If the non-uniformity is in the 'white' region, maybe try putting the white background out of focus using a spacer between the razor blade and the background. Focus Analyser does some smoothing to reduce such noise, but if there is too much, the analysis is adversely affected.
If the program displays an error message, I'd appreciate if you would send a copy of the error text using the web form at the link at the bottom of this page. Please mention which browser and device type you were using. (Focus Analyser's 'native' environment is Chrome on a desktop computer.)
Here are some blogs and web pages with related content:
Comments or suggestions are welcomed.
Sample screen shot of a LoCA Focus Analyser analysis (2017-Jan).