Coffee Data Science

Improving Coffee Grind Distribution using Adaptive Thresholds

Using image processing to understand grinder performance Part 4

Robert McKeon Aloe
Towards Data Science
5 min readMar 5, 2021

--

Previously, I have used a sifter and different imaging techniques to improve coffee grind distribution measurement, and now I’m going to apply an old technique of mine to better collect data in imaging. Often, the images have easy to find grounds, but some are not. Having a simple threshold doesn’t always work because of how light interacts with coffee, so I took a closer look.

The resulting change tripled the number of coffee particles I’m able to measure, and it is a straight forward adaptive filter which helps with lighting issues.

Quick Review

I take an image, determined the measurement ground truth (pixels to mm²) using the piece of paper, then took a threshold to find the particles.

All images by author

Adaptive Filter

I started by looking at some images. I felt like the distributions did not fully capture the number of fine particles. So I looked at my grayscale image with is the product of the red, green, and blue planes. Then I take the inverse to get dark spots as 1 as the highest value.

I visualised this in two ways:

  1. False color
  2. Mesh with false color

It reveals that there are many lighter spots that may not reach the threshold. In this case, the threshold was 0.9, which has two effects:

  1. Throws out small particles
  2. Reduces the edges of other particles, which affects the over distribution.

Low Filter (Adaptive Filter)

Back during my master’s degree, I worked on an autonomous vehicle in the bronze age of computer vision. Using low level image processing, I developed a method called the low filter for improving filtering out noise when trying to do lane line identification (Lane Identification and Path Planning for Autonomous Mobile Robots).

The low filter was based on the observation that the average row value increases as one went from the bottom of the image to the top. This could be used to help filter out less desirable pixels that a global threshold could not.

Top Left: Original image. Top Right: Barrels removed. Bottom Left: Low filter applied. Bottom Right: Threshold.

This filter worked by throwing out pixels in each color plane based on this threshold:

Threshold = mean(row) + A * std(row), where A is a user defined constant.

So I took this idea, and I applied it to coffee grounds!

Filtering Coffee with a Low Filter

I applied this filter row-wise and column-wise, and I found A=2.5 worked pretty well for my application. I seemed to pick up a lot more particles and particle edges than I had previously.

We can look at the total count across three images, and it is almost triple.

Coffee Distributions

I show a comparison of particle distributions before and after. I have three settings at the top, and then I split each one below.

There is a slight shift for the smallest setting, which confirms that this technique is able to pick out smaller particles or at least it is picking out more small particles.

This seems like a shift in the weird distribution of setting 15.

The more I look at these distributions, the more they seem to defy how I think they should look because they are bumpy. This one is mostly unaffected except for a slight shift.

Particle Distributions

These distributions were close, soI looked at the raw particle distributions.

These were pretty close as you can see in this breakdown by setting. There are some changes in the shape of the curves.

The main trouble is still assuming coffee is round, and it is not. However, this technique could still work relatively well to compare different grind settings and grinders. I’m more confident having an adaptive filter to help characterize the particles. This technique also makes the whole process more robust to lighting variations.

The next step is to compare multiple grind settings and grinders.

--

--

I’m in love with my Wife, my Kids, Espresso, Data Science, tomatoes, cooking, engineering, talking, family, Paris, and Italy, not necessarily in that order.