Coffee Data Science
Improving Coffee Grind Distribution using Adaptive Thresholds
Using image processing to understand grinder performance Part 4
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.
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:
- False color
- 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:
- Throws out small particles
- 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.
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.
If you like, follow me on Twitter and YouTube where I post videos of espresso shots on different machines and espresso related stuff. You can also find me on LinkedIn. You can also follow me on Medium.
Further readings of mine:
Collection of Espresso Articles
A Collection of Work and School Stories
Measuring Coffee Grind Particle Distribution using Image Processing
Espresso Filter Baskets: Visualized
Espresso Filter Comparison: Pesado vs VST
Espresso Baskets (VST): Ridged vs Ridgeless
IMS Superfine vs VST: A Small Sample Espresso Filter Comparison
Espresso Simulation: First Steps in Computer Models
Kruve: Further Analysis on the State of the Art