BH App Archive
Coffee Grind Analysis
Quick start – The idea of using a picture of coffee grinds to measure the size distribution came from Dr Jonathan Gagné. With generous help from Jonathan and the folks at Barista Hustle, this browser-based version opens up the technique to a wider audience.
The numerics are NOT yet optimized or validated, but you’ll get the general idea. The test image which opens is relatively low resolution so is not representative of a high quality image – but it is useful to show the principles. Its calibrated width is 138mm to get you started.
How it all works
Background explanation – The idea is simple. Take a picture of the grinds and automatically identify each true particle in the image, count the number of pixels in that particle, then calculate how the grains are distributed over various sizes
It is often imagined that such an “image analysis” technique would be very difficult. In reality, there are two major difficulties that are nothing to do with fancy image analysis:
1 – Getting even lighting, with perfect black/brown beans on a perfect white background is very hard. Any imperfection in the lighting, background, bean colour makes it harder to distinguish particle from background or noise.
2 – Calibration is far trickier than you might imagine. A pre-calibration requires you always to take the image from exactly the same distance. A calibration object in the image (a) takes space away from grinds and (b) needs some mouse-based user identification or a smart algorithm to define.
On the principle of “keep it simple”, the first problem is solved by providing a backlight to the grinds. In my case I used my laptop screen that folds flat – I could equally have used a monitor laying on its back. It helps that I have an excellent hand-held vacuum cleaner that quickly sucks up the grains after a test.
The other simple approach used here is that somehow you accurately know the full width in mm of your image. My preferred technique is to include a scale along the top or bottom of the image. The app “blobs” it into a single large object which is automatically discarded by the MaxPixel control – or the colour is set so faint that the blobbing ignores it anyway.
You just read the scale (e.g. counting 5mm markers and interpolating the last few mm) and enter the value. Because I use a laptop screen, I can create my scale as a graphic image, optimizing size, spacing, colour etc. for maximum accuracy with minimum interference with the counting.
Size analysis – Load an image. With the Pic-only option selected you can get a good idea of the image quality and can read off the scale and entered a good calibration value. You now have to decide on a Threshold value. Anything less bright that this will be considered as a particle, anything brighter will be ignored. You can move your mouse around the image and get an RGBK value. The K (for blacK) is grey-scale value used to threshold. Move over a few grains and get a feel for their K values (small) compared to the background (large). Then choose a Threshold value that safely includes your grains but excludes odd shadows, marks etc. – as best you can. Jonathan used the value from the Blue channel as this is most sensitive to the brown colour of the coffee grains. You can choose this option if you wish. If you click and drag the mouse over a grain you will get an idea of its size, providing your calibration is correct.
If you have a noisy image with lots of 1-2 pixel dots, set a Min-Pixels value of, say, 5, so that this noise is ignored. Of course this might exclude some real grains, so you need to reach a compromise value. The Max-Pixels value can often be set to a medium value to be able to exclude things like the scale marks or a bit of black edge that appeared in the image. Set it too small and some real grains are excluded. Too large and some bit of junk will be included and generally reduce the worth of the calculations. The more perfect your lighting and image, the less you need worry about these details.
To analyze the sizes it is customary to place them into “bins”. Everything from 0-0.1mm might be in one bin, everything from 0.1-0.2mm in another etc. N-Bins lets you choose. Too few and you don’t get a sensible analysis. Too many and you probably don’t have enough grains per bin for the data to make statistical sense.
Each time you change your inputs things are re-calculated automatically – speeds seem to be OK if you don’t slide too wildly. You get NGrounds, the number of individual particles identified (within your Min-Max Pixels) and Max the largest one. This box is very useful. If you change Threshold or Min-Max Pixels
over a reasonable range, and if these values remain not much changed then you are in a happy part of image analysis space where your measurements are way above any noise level from junk in the image. If the numbers change a lot then your final results depend on your judgement rather than hard numbers. The more time you devote to getting well-lit images with few artefacts, the less you will need to fiddle with the control variables.
The results – Suppose you have 99 particles each of diameter 1 and 1 particle of diameter 100. You can correctly say “My grind is 99% size 1” and you might be very happy – till you taste the coffee. Another way of thinking about it is to say that those 99 particles will weigh, say, 1 unit each so their combined weight is 99. Because weight depends on volume which depends on diameter³, the one large particle will weigh 1,000,000. Therefore, in weight terms your 99 particles are insignificant and effectively you only have a single large lump of coffee, which won’t extract efficiently.
So when we show the size distribution we could show it in Number terms, how many are at each diameter (or are in the bin with that diameter range). This matches our intuition when we look at the image – we see all those small grains and hardly notice the large ones. We can also show them in Area terms which expresses how much of the surface area comes from various particle sizes. And we can show them in Volume terms which tells us where the volume or mass of coffee really lies. I’ve chosen to plot all three. We can also plot them in a cumulative manner so you can readily see at which sizes interesting things happen. Some like to see them in logarithmic scale. The arithmetic of the calculations is discussed below.
If you have a rogue large particle, your top few bins might be empty, with a leap in the final (volume) bin. Decrease Max-Pixels below the Max value shown in the information box at the top. The rogue bin should disappear and your plot will look more sensible. Of course, if that rogue particle is real and is of a significantly larger size then (as you will see in the cumulative volume plot) your coffee extraction will be dominated by that one large particle!
Spreading the grinds – The app doesn’t get tired of counting. So the more particles you can pack into the image, the better the statistics for analysing the particle size. The problem is that if any two particles are touching, they will be counted as one larger particle. Finding a way to spread the maximum number of particles with the minimum number touching is one of your big challenges. Jonathan suggests sprinkling with your fingertips – an idea I found to be surprisingly good. He also has an option for you to delete touching particles from the image, but it requires you to make the judgement call and zap them with your mouse.
Your camera – With coffee grind analysis, as with everything else, it’s garbage in, garbage out. You can easily get a picture with your smartphone camera, but you will tend to find that the “smart” part of the phone is a real nuisance. It might autofocus on something important to it, and not to you. It might decide that your stark contrast really is unpleasant so will automatically smooth out your lighting. Who knows what “smart” algorithms it will invoke. Smartphones were not designed by people whose interests were primarily taking accurate, high contrast,
close-up shots for image analysis. If you are smart enough to know how to turn off the smart camera features on your phone then you are off to a good start.
My experience is that with my fairly good Canon camera, I need to turn off auto everything. By trying a few manual settings, I could get a balance of resolution, contrast, focus that time after time gave me the same results. When I allowed it to take over with automatic features, I never knew what I would get – to the level of detail required for good coffee grind analysis.
To make things even simpler (in the long run), set up a rig for the camera so it is always nicely square to your backlit source, with the scale clear at the top, at an optimum distance. In the image you see that I have my camera on a sturdy tripod and use a “hot shoe spirit level” (the yellow-green blob) to get everything square.
The Calculations – There is not enough information in a 2D picture of 3D grains to create an accurate analysis of their sizes. Calculations therefore come down to a choice of approximations.
Joanathan has chosen a more sophisticated method that uses the 2D shape information. For speed and simplicity I have chosen a different approach. Maybe a future version will attempt something more sophisticated.
All I know after the image processing is the number of pixels making up a “blob”. I can call this an area, A, so I know that if this was a circle, the radius would be given by Α=πr². Now I have the pseudo-radius, r=√(A/π), of each blob. I can then say that the surface area of the grain is 4πr² and its volume is 4/3πr³. This allows me, for each radius bin (though I plot it as the more comfortable diameter), to know the Number of particles in that bin, the Area of those particles and their Volume.
To report the results, for simplicity I give a Number, Area and Volume mean value to capture an “average”. For those familiar with the topic, these values are D[1,0], D[2,1] and D[3,2]. There is also a D-x which you define as D90, D80 etc. via the slider. This is the diameter below which 90%, 80% etc. of the grains are found. Other “average” values are available and further options might be added later.
Made by Professor Steven Abbott, especially for Barista Hustle!
This app was made especially for Barista Hustle and our subscribers by Professor Steven Abbott. His extraordinary career has taken him all around the world, with gigs that have included working with banana growers in the Philippines, printing companies in Colombia and a coffee bag valve company in the USA. Prof Abbott is a world expert on drying science and diffusion. His of apps and ebooks are a go-to for anyone serious about the science of extraction.
See Professor Steven Abbott’s collection of books here!