---
title: Separating Particles from the Background
url: https://oosioo.com/en/p/%EB%B0%B0%EA%B2%BD%EA%B3%BC-%EC%9E%85%EC%9E%90-%EA%B0%80%EB%A5%B4%EA%B8%B0
date: 2026-10-09T11:36:34+00:00
author: SYSOP
summary: To measure particles in a photo of coffee grounds, you first have to decide where the background ends. I calculated how much oblique light’s shadows, the gap between pixel values and amounts of light, and Otsu’s method can sway the threshold, and used one public example photo to check what flattening the background does.
---
# Separating Particles from the Background

In the last post, when I worked out how a single particle turns into pixels, I fixed three numbers before starting. The background has brightness 1, the particle has brightness 0, and any pixel darker than 0.5 in between counts as particle. The math was clean, but a real photo has none of those three numbers written on it. The paper's brightness varies from place to place, coffee isn't pitch black, and the pixel values a phone saves aren't even proportional to the amount of light.

So to measure particles in a photo, you first have to answer this question: where does the background end and the particle begin? The brightness that draws this boundary is called the threshold. This post looks at how much a single threshold can sway a measurement. In turn: how you set up the lighting, on what brightness scale you measure the threshold, and who picks it.

## Köhler's evenly lit field of view

Microscopes in the late 19th century used a mirror to gather light from a gas lamp or oil lamp onto the sample. Lit this way, the field of view was uneven. The shape of the light source was imaged right onto the sample, so some places were bright and others dark. That was bearable to the eye, but in photomicrographs with long exposures the blotches stayed in the picture.

![Black-and-white portrait photo. An older man with a white beard and round metal-rimmed glasses, in a suit, looking straight ahead](/uploads/0b9ab3e125261de0.webp =360)

*August Köhler (1866–1948). Photo: ZEISS Microscopy / Wikimedia Commons (CC BY-SA 2.0)*

In 1893, the German August Köhler published a new illumination method for photomicrography (Köhler 1893). Instead of imaging the light source onto the sample, it deliberately fills the whole field of view with defocused light. This keeps the shape of the lamp filament out of the photo and lights the field evenly. Köhler later worked at Carl Zeiss, and his method is still the standard illumination for optical microscopes today.

What makes Köhler illumination matter for measurement is the background. Only when the background has the same brightness everywhere can differences in the sample's brightness be read as differences in the sample. Coffee-ground photos are in the same situation. If the background is even, one threshold can cut the whole photo; if it isn't, the same threshold means something different in each place.

## The background of one photo

coffeegrindsize, made by the astronomer Jonathan Gagné, is nearly the only public grind-size measurement program you can use at home (Gagné, coffeegrindsize). Give it a photo of grounds scattered on white paper and it plots the particle-size distribution. The way it separates particles is simple. It uses only the photo's blue channel, takes the middle brightness of the whole photo (the median) as the background brightness, and treats pixels darker than 58.8% of that as coffee. Gagné explains in the manual why it uses only the blue channel: coffee is brown, and brown has almost no blue light, so the contrast against a white background is largest there.

The manual is also thorough about how to take the photo. Use a plain white background, make the background brightness the same in every direction, and avoid shadows around the particles. Gagné himself said he places a single light slightly at an angle over the table and shoots from the side opposite the light, so his own shadow doesn't fall on the photo. The manual includes bad examples and passable ones. Below is an example that is "usable but not great."

![Photo of brown coffee grounds evenly scattered on white paper. A Canadian quarter and a handwritten label sit on the right. The top center glows white with reflected light, and a broad, soft shadow falls across the bottom left](/uploads/761bd4371c972d24.webp =760)

*An example photo from the coffeegrindsize manual. Gagné notes to avoid the glare at the top and the shadow at the bottom when analyzing. Photo: Jonathan Gagné, coffeegrindsize ([MIT License](https://github.com/jgagneastro/coffeegrindsize/blob/master/LICENSE))*

To the eye, it's slightly blotchy white paper. I took this photo's blue channel, erased the coffee particles, and left only the background brightness.

![On the left, a map of the photo's background brightness: brightest at the top center, darkening downward, with a dark shadow band at the bottom left. The table and coin appear dark at the far right. The graph on the right plots the average background brightness of each row from top to bottom: it starts around 190 at the top, slowly falls to near 150, then drops sharply below 70 at the very bottom. A red vertical line is drawn at 85, and in the bottom stretch the curve crosses to its left](/uploads/f4a2884b370c5515.webp =760)

*The background was estimated by erasing the particles and averaging over a wide area. Photo: Jonathan Gagné (MIT). Analysis: this series*

The paper's background brightness (0–255) exceeds 220 at the top, where the light reflects, and drops into the 60s at the bottom left, in the shadow. The same white paper was recorded nearly 3.5 times brighter in one place than another. The median of the whole photo is 144, and the threshold at 58.8% of that is 85. But in the bottom band, the paper itself is darker than 85. In that band, even paper with no coffee on it is all judged to be coffee.

In fact, cutting the bottom 300 rows with this threshold marks 94% of the area as coffee. The opposite happens in the glare zone at the top. Because the background is bright, the blurred edges of particles are brightened too and don't fall below the threshold, so the particles come out small. That's why Gagné wrote to avoid the glare and the shadow when analyzing.

## Light from above, or from below?

There are two broad ways to light coffee grounds for a photo. One is reflected light: light from above or the side, photographing the light reflected off the grounds and the paper. Gagné's method belongs here. The other is backlight: put the grounds on a light-emitting panel such as a light pad and light them from below. Shot with backlight, each particle becomes a shadow blocking the light, a silhouette.

Reflected light has a lot to watch out for. The coffee's color sets the contrast, so contrast changes with the roast. If the light is oblique, every particle casts a shadow. A shadow's length is the particle's height divided by the tangent of the light's elevation angle, so lighting a 300 µm-tall particle from 30° stretches its shadow to more than 500 µm. Put the light directly overhead and the shadows disappear, but directly overhead is where the phone and your hand are.

I photographed four virtual particles of known size under four lighting setups. Particle height was set to 0.6 times the diameter, and the light reflected by coffee to 8% of the paper's. Inside a shadow, the room's other light (ambient light) fills in. A pixel counts as particle when its amount of light is less than half the background's. Why I insist on judging by "amount of light" is the subject of the very next section.

![A four-panel figure. In each panel, the top is a grayscale photo of the same four particles and the bottom shows the pixels judged as particle in red. In the backlight panel, the particles appear as dark circles and the red areas nearly coincide with the blue dashed circles. In the overhead-light panel, the particles appear a little lighter, in gray. In the 45° oblique-light panel, a faint shadow forms to the lower right of each particle and the red area pokes out slightly that way. In the 30° oblique panel with little ambient light, the shadows are long and dark, and the two large particles are joined by their shadows into a single red blob. Below each panel, the detected area reads 93%, 90%, 107% and 149%](/uploads/bccafadeffcd1740.webp =760)

*At 15 cm height, one pixel = 49.5 µm. Calculated for this series (model estimate)*

Backlight and overhead light, as we saw last time, only measure small particles a little small. Tilt the light and shadows attach to the particles. At 45°, the detected area was 7% larger than the truth; at 30° with little ambient light, 49% larger. In the 30° panel, two neighboring particles were joined by their shadows into one blob.

Whether a shadow attaches to a particle depends on whether the shadow is darker than the threshold. The brightness inside a shadow is set by ambient light's share of the total light. So even with the same light at the same angle, the result changes depending on whether the room's other lights are on or off. Photographing a particle 10 pixels across (500 µm at 15 cm) under 30° light, you measure its diameter 0.4% too large when ambient light is 50%, but 20% too large when it is 20%. Somewhere in between, the shadow crosses the threshold and sticks to the particle.

With backlight, there is no place for a shadow to form. A particle is recorded only by whether it blocks the light or not. That's why backlight is recommended for measuring coffee grounds. One caveat: very thin flakes, such as the chaff that falls off bean skins, can let a little light through and come out pale. And how evenly the light pad itself glows has to be checked separately.

## 50% of a photo isn't 50% of the light

Last time I put the threshold at 0.5. That's because even when a straight edge blurs, the line at brightness 0.5 stays exactly where the original edge was, since the same amount of light spreads to each side. Here 0.5 means half the amount of light.

But the pixel values a phone saves aren't amounts of light. The human eye is more sensitive to differences in the dark, so image files bend the amount of light to spend more values on the dark end. In the conversion formula of the sRGB standard, which the web and most photos follow, a pixel value of 128 (50%) corresponds to 21% of the light (IEC 61966-2-1). coffeegrindsize's 58.8% is 30% in amount of light.

![Two graphs. The left one plots brightness across the edge between a particle and the background. The horizontal axis is distance from the true edge, -3 to 3 px; the vertical axis is 0–100%. The blue curve (amount of light) passes 50% at distance 0. The red curve (pixel value), the same edge converted to sRGB, rises further left and passes 50% at -0.71 px. The right graph plots measured ÷ true diameter for particle diameters of 3–30 px. The blue "50% of light" curve passes 0.95 at 6 px and hugs 1 by 30 px. The orange "pixel value, 58.8% of background" and the green dashed "50% of light, overexposed" nearly overlap, at 0.9 at 10 px and 0.97 at 30 px. The red "pixel value, 50% of background" is lowest, at 0.84 at 10 px and 0.95 at 30 px](/uploads/a50834047e38f8a0.webp =760)

*Backlight, blur σ 0.8 px, each size measured 60 times with shifted particle positions. Calculated for this series (model estimate)*

Cutting at 50% of the pixel value is the same as cutting at 21% of the light. You're cutting deep inside the particle, so the particle shrinks. With a blur of 0.8 pixels, the edge moves 0.7 pixels inward on each side, so the diameter loses 1.4 pixels. At 15 cm that's about 70 µm. This amount barely depends on particle size, so the smaller the particle, the more it shrinks in proportion.

| Particle diameter | 50% of light | Pixel value, 58.8% of background | Pixel value, 50% of background |
|---|---|---|---|
| 6 px (300 µm) | −4.8% | −20% | −28% |
| 10 px (500 µm) | −1.9% | −10% | −16% |
| 20 px (1 mm) | −0.5% | −5.0% | −7.4% |

*At 15 cm height. Calculated for this series (model estimate)*

Cutting at coffeegrindsize's default measures a 500 µm particle 10% small. Volume goes with the cube of the diameter, so it comes out 28% small. Of course, real phones don't use the sRGB formula as is. Each maker adds its own tone curve to boost contrast. So how much things actually shrink varies from phone to phone. Still, no phone guarantees that a threshold chosen in pixel values lands on half the amount of light.

Exposure does the same thing. Raise the exposure to make the background look white, and the background pins at 255. The blurred edges of particles are still below 255, but the background can't get any brighter and gets clipped. So in an overexposed photo, "half the background" is pushed toward the particle compared with the true half of the light. Raise exposure 1.6× to blow out the background, and even cutting correctly at 50% of the light measures a 500 µm particle 10% small. That's almost the same as cutting at coffeegrindsize's default, which is why the orange curve and the green dashed curve in the graph above overlap.

There are two fixes. One is to convert pixel values back to amounts of light before cutting. Shooting RAW gives you values close to the amount of light directly. The other is to tune the threshold with an object of known size. The marker sheet from earlier posts has black squares of known size. Find the threshold at which a square's width measures exactly right and use it, and you can pick the pixel value corresponding to half the light without knowing the phone's tone curve. How to carry this method over to backlit photos, though, I haven't tried yet.

## Otsu's method

The threshold doesn't have to be set by a person; the photo can choose it. The most common way is the method Nobuyuki Otsu published in 1979 (Otsu 1979). It gathers the photo's pixel values into a histogram and finds the value at which splitting it in two separates the two groups most clearly. Here "most clearly" means the largest value of the squared difference between the two groups' means multiplied by the ratio of their pixel counts (the between-class variance). It's built into image-processing tools such as OpenCV, ImageJ and MATLAB.

Otsu's threshold usually lands midway between the two groups' means. In a backlit photo, the particle mean is near 0 and the background mean near 255, so the threshold stands between them.

![A two-row figure. The top row is a case where particles cover 5% of the frame and the lighting is even: the pixel-value histogram has a tall background peak at the far right (near 255), a particle peak at the far left, and low bars spread evenly in between. The red threshold line is at 152, and in the small image on the right the particles are painted red. Detected area ÷ true area is 0.81. The bottom row is a case where particles cover only 0.3% of the frame and the lighting tilts ±30% from left to right: the background peak is spread wide over 190–255. The threshold line is at 238, right in the middle of the background peak, and nearly the left half of the small image on the right is painted red. Detected area ÷ true area is 86.64](/uploads/4c9672ec294d223e.webp =760)

*Backlight, 15 cm, blur σ 0.8 px, 2% noise. Calculated for this series (model estimate)*

In evenly backlit photos, Otsu's threshold stood at pixel values of 146–156 (57–61%). It stayed in almost the same place whether particles covered 0.3% or 35% of the frame. That's nearly the same as coffeegrindsize's default of 58.8%. So using Otsu doesn't make the previous section's problem go away. Otsu also chooses from the histogram of pixel values, so in amount of light it cuts near 30% and measures particles just as small.

Where Otsu really breaks down is elsewhere: when particles are very sparse and the lighting is tilted. The bottom row of the figure above is a photo where particles cover only 0.3% of the frame and the left and right differ in brightness by ±30%. Otsu didn't separate particles from background; it separated the darker side of the background from the brighter side. When there are too few particles, splitting the background in half becomes the "clearer split" than peeling off the particles. The detected area was 87 times the truth. At 0.1% coverage, the same thing happened even at ±20%. Otsu's method works well when the histogram has two peaks of similar size. When one peak is too small, that premise breaks. Because of this weakness, methods that handle groups of different sizes and spreads followed (Kittler and Illingworth 1986; Sezgin and Sankur 2004).

Applying Otsu directly to Gagné's photo gave similar results. On the paper area, Otsu chose 107 and judged 99% of the shadowed bottom band to be coffee.

## Flattening the background first

If a background that differs from place to place can't be handled with one threshold, make the background even first. Before putting the coffee down, take one shot of the blank paper in the same spot under the same light, then divide the coffee photo, pixel by pixel, by that blank-paper photo. The background then becomes 1 everywhere, and the particles remain as a ratio to the background. This is flattening (flat-field correction), long used in astronomical and microscope photography. It does by calculation what Köhler did with lighting.

Without a blank-paper photo, you can estimate the background from the photo itself. Particles are small and the background's blotches are broad, so erasing the particles and averaging over a wide area yields a background map. The background map of Gagné's photo shown earlier was made this way. I divided the photo by that map and applied the same 58.8% threshold.

![A figure with two rows of three panels. The top row is the glare zone at the top of the photo; the bottom row is the shadow zone at the bottom left. The first panel is the original blue channel, the second is the result of a single threshold applied to the whole photo, and the third is the same threshold applied after dividing by the background. In the top row, the red particles in the second panel are each a little smaller than in the third. In the bottom row, most of the lower part of the second panel is painted red so the particles can't be made out, while in the third panel only the particles remain red. Below, the area judged as coffee reads 7.7% and 9.2% for the top, and 81.9% and 2.4% for the bottom](/uploads/58fce83d297de380.webp =760)

*Each zone is 600×450 px. Photo: Jonathan Gagné (MIT). Analysis: this series*

In the shadow zone, the area judged as coffee fell from 82% to 2.4%. In the glare zone, it rose from 7.7% to 9.2%, which means a single threshold had missed about a sixth of the coffee there. Over the whole paper area, a single threshold judged 15.3% of the area to be coffee; after flattening the background, 5.6%. Applying Otsu to the flattened photo put the threshold at 57.7% of the background, with almost the same result.

Flattening the background erases the differences from place to place in the photo. The previous section's problem, that half the pixel value isn't half the light, remains as is. The two have to be fixed separately.

## Try it yourself

In the simulation below you can change the lighting, particle density, lighting tilt, exposure, and how the threshold is chosen. On the left is a virtual photo of coffee grounds (about 12×9 mm) taken from 15 cm above; red marks pixels judged as particle, and blue dashed lines mark the true particles. On the right are the histogram of pixel values and the threshold. The numbers below show how many times the true area was detected, how many blobs were found, and the average error in diameter for particles larger than 300 µm. Set particle density to its lowest, raise the lighting tilt to ±40%, and choose Otsu to see the earlier breakdown again. Click the left image to turn off the judgment and show only the photo.

```map /embed/coffee-light-2026-en.html 16:12
A simulation for experimenting with the threshold that separates particles from background in a photo of coffee grounds. On the left is a virtual photo of coffee grounds taken with a phone held 15 cm above; pixels judged as particle are shown in red, and the true particle outlines as blue dashed circles. On the right are the histogram of pixel values and the chosen threshold. You can change the lighting (backlight, overhead, oblique), light angle, ambient light, particle density, lighting tilt, exposure, how the threshold is chosen (58.8% of pixel value, Otsu, 50% of light), and background flattening by dividing by a blank-paper photo; detected area ÷ true area, the number of blobs, and the diameter error for coarse particles are shown as numbers.
```

## What this model leaves out

- **The phone's actual tone curve.** This post used the sRGB formula. Real phones change tone depending on the scene and also apply edge enhancement. What percentage of light a threshold corresponds to can differ by phone and by photo
- **The particles' color and shape.** The values that coffee reflects 8% of the paper's light and that its height is 0.6 times its diameter are assumptions. They vary with roast and grind. The sheen of oily dark roasts isn't in the model either
- **The actual evenness of a light pad.** I recommended backlight, but I haven't measured how evenly a home light pad glows. Photographing an empty pad would tell you
- **A real comparison experiment.** I haven't yet photographed the same grounds under backlight and reflected light to compare them. All the numbers in this post come from virtual samples and one public example photo from Gagné

## Summary

- To measure particles in a photo, you have to set a threshold that separates background from particle. Move the threshold even slightly and every particle's edge moves with it
- Even in one public example photo, the same white paper differed in brightness by nearly 3.5 times from place to place. A single threshold over the whole photo judged 94% of the shadowed band to be coffee
- Oblique light attaches shadows to particles. Whether they attach depends on ambient light's share; under 30° light, a 500 µm particle was measured up to 20% too large. Backlight has no shadows
- 50% of the pixel value is 21% of the light. Cutting on pixel values moves the edge into the particle, so even coffeegrindsize's default (58.8%) measures a 500 µm particle 10% small. Raising exposure to blow out the background shrinks it about as much
- Otsu's method also chooses on pixel values, so it has the same bias. When particles cover only 0.1–0.3% of the frame and the lighting is tilted, it splits the background in two and breaks down
- Flattening by dividing by a blank sheet erases place-to-place differences. The brightness-scale problem is fixed separately, by converting back to amount of light or tuning the threshold with an object of known size

It's been more than 130 years since Köhler's illumination method. In that time photography has moved from film to phones, but the fact that an uneven background throws off measurement hasn't changed. Before you measure the size of a particle, measure the background.

## References

- Köhler A (1893). Ein neues Beleuchtungsverfahren für mikrophotographische Zwecke. *Zeitschrift für wissenschaftliche Mikroskopie und für mikroskopische Technik* 10(4), 433–440 — citation checked (secondary sources)
- Gagné J. *coffeegrindsize*. GitHub. [Link](https://github.com/jgagneastro/coffeegrindsize) — checked the code that uses only the blue channel and cuts at 58.8% of the median (`def_threshold = 58.8`), and the shooting advice and example photo in the manual (Help/coffee_grind_size_manual.pdf)
- IEC 61966-2-1:1999. Multimedia systems and equipment — Colour measurement and management — Part 2-1: Default RGB colour space — sRGB — conversion formula (pixel value 50% = 21.4% of light)
- Otsu N (1979). A threshold selection method from gray-level histograms. *IEEE Transactions on Systems, Man, and Cybernetics* 9(1), 62–66. [doi:10.1109/TSMC.1979.4310076](https://doi.org/10.1109/TSMC.1979.4310076) — citation checked
- Kittler J, Illingworth J (1986). Minimum error thresholding. *Pattern Recognition* 19(1), 41–47. [doi:10.1016/0031-3203(86)90030-0](https://doi.org/10.1016/0031-3203(86)90030-0) — citation checked
- Sezgin M, Sankur B (2004). Survey over image thresholding techniques and quantitative performance evaluation. *Journal of Electronic Imaging* 13(1), 146–165. [doi:10.1117/1.1631315](https://doi.org/10.1117/1.1631315) — citation checked

## Image credits

- Portrait of August Köhler: ZEISS Microscopy, [Wikimedia Commons](https://commons.wikimedia.org/wiki/File:August_K%C3%B6hler_(1866-1948)_(8527804902).jpg), CC BY-SA 2.0
- Coffee-ground example photo: Jonathan Gagné, [coffeegrindsize](https://github.com/jgagneastro/coffeegrindsize) Help/Decent_Example_Picture.png, [MIT License](https://github.com/jgagneastro/coffeegrindsize/blob/master/LICENSE) (resized). The background map and flattening figures were made by analyzing this photo
- All other figures, synthetic photos and simulations: made for this series
