How to measure diameters of berries?

Hello Image J Experts,

I am doing an experiment where I would like to automate the measurement of wine grape berries. My plan would be to lay out as many berries as possible on a white surface and take a photo. I would then want to use Image J to measure the diameter of each berry, and give me a count for the number of berries per given diameter.

Is this possible? It seems like it should be relatively simple. Would Diameter J work? It looks like it does what I need, just with fibers, so maybe it would work with round berries?

FTR I do have some experience with Image J using it to measure leaf area with a photo of a leaf and a ruler, so I imagine this should be simple.

Thank you all in advance!

1 Like

Hey @jwm,

that sounds doable. After running menu Image > Adjust > Threshold, you could call Analyze > Set Measurements, activate Area and Fit Ellipse and finally run Analyze > Analyze Particles. This will give you the area of the berries, from which you could calculate the radius under the assumption that they are circular. Alternatively, it will give you the minor and major axis from the ellipse fitting.

Give it a try!

Cheers,
Robert

2 Likes

In addition to the suggestions from @haesleinhuepf for a more precise measurements (if necessary) make sure to correct the lens distortions of your camera (e.g., barrel distortions, etc.) and make sure to illuminate the photographs evenly to avoid shadows, etc.

Probably this is not necessary for a rough measurement.

Furthermore make your setup (lightening, correct lens dist., etc.) reproducible for later measurements.

This avoids a lot of double work.

To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.

Ronald Fisher

3 Likes

Thanks to you both!

Will report back in a few weeks when I take my next set of measurements.

1 Like

Ok, I’m getting close to what I need. Below is a photo I took with frozen blueberries and a penny as my size reference. This is similar to the photos I will be taking with the grape berries, but ideally with 3-4k+ berries.

This is after converting to 8 bit and image>adjust>threshold with a slight adjustment to get rid of the shadows and juice stains:

However when I run the area calculator, I get way too many data points. I get over 860 values, when I should be getting around 70-100.

Is there a way to autofill the berries with a tool or image adjustment? I think that the light reflecting off of the berries is causing them to not be fully filled in, which is why I am getting over 800 values?

Any insight would be appreciated.

Finally- with the penny as my reference, is there a way to calibrate that to the photo, so that I get an output in millimeters?

Thank you all again in advance.

Addittional tips:
-Have a proper ruler in the field of view
-Try to get the contrast higher by adjusting the location of your illumination.
-Take away the stark shininess (white spots on the black berries) by illuminating in a photo light box (google the device) which gives a softer light.
-Experiment with light colour (white background, red light and green grapes come to mind)
-If using white grapes, use a black background :wink:

First of all set the scale:

http://microscopy.berkeley.edu/courses/dib/sections/04IPIII/IJsetscale.html
https://imagejdocu.tudor.lu/gui/analyze/set_scale

Avoid staining the background this makes measuring easier. For instance use another background which avoids the berry staining.

Iluminate the images evenly (e.g., with a soft light) .This avoids reflection on the berries and improves the measurements

Exclude dirt which was measured as particles (restrict roundness and particle size to exclude thresholded dirt, see:

https://imagej.nih.gov/ij/docs/guide/146-30.html)

Hi,

In addition to what @eljonco and @Bio7 said, I would add that you can fill the berries and when using the particle analysis, you can filter by size and shape.

setAutoThreshold("Li");
setOption("BlackBackground", true);
run("Convert to Mask");
run("Fill Holes");
run("Analyze Particles...", "size=5000-Infinity circularity=0.30-1.00 exclude clear add");

You 60 berries will be ok.

One last (important) thing. Here, you have 60 berries. It is easy to segment. If you have 3-4k berries, they will probably touch each other. So you’ll have to separate them. Here starts the nightmare :cold_face:
(of course watershed will help, but… you’ll see)

Nico

1 Like

If a few K berries, since the images still have to be recorded, you might be able to cheat and use a dented plate (think: an egg tray) or a temporary raster (cardboard pill box fitment or insert) to dump the berries on/in. This positions them apart and in a grid. Do make sure the dents are not visible in the image, either by proper illumination or by removing the raster before image recording.

2 Likes

Thanks again all. I feel like I’m getting closer, but still struggling with a few things.

I like the idea of a light box and an egg shell tray. If I decide to pursue this further, I will buy a rectangular egg shell light panel like the one here, and place it over a light box https://www.homedepot.com/p/Plaskolite-4-ft-x-2-ft-Suspended-Egg-Crate-Light-Ceiling-Panel-1199233A/202025149

Anyways, where I am still having trouble is the measurements I am receiving still do not make sense. Below is my process:

Base photo is 321 berries on top of a plastic bag. I realize that my lighting and staging could be better, but that is not the issue at the moment:

After converting to 8 bit, then Threshold Li, I set the scale with a line along the ruler, then set that to 300 mm:

Then I set the circularity to 0.3 and the minimum size to 5 mm2, because some of the berries could be as small 3 mm in diameter.

Quick question: Should I be checking the box for “Pixel Units”?

Finally, these are the results:

Now, the number of measurements is close enough (279, compared to an actual of 321) for the time being, and can be refined with better lighting and staging. What does not make sense to me are the Major Axis and Feret’s Diameter. Assuming I set the scale correctly, all of these measurements should be well over 3-4 mm, if not upwards of 8-10 mm plus. However nearly all of these measurements are less than 1.

What am I doing wrong? Thanks again in advance.

I suspect pixel units changes what your threshold means. So if you want objects 5 pixels or smaller, vs 5 mm^2 or smaller removed.
Which leads to the second part, where you have image a lot of left over small objects.

It would not be surprising if these “berries” were very small and had very different min and max feret/major minor axes. Because they do! Removing 5 pixel objects isn’t cleaning up that much stuff.

Hi @jwm,

You should tick add to Manager to see what is selected.
When you set your scale, you will lower the error of measuring in using a 10 cm line instead of a 1mm

Nico

A few more things.

  • Are you sure that you measure what you want ? Because usally, measurements are done on white objects (ROI manager will help you to see this)?
  • How do you get 8 bit image ? Sometimes, spliting image in RGB components is better that casting it in 8 bits.
  • Do you do some background correction to avoid juice problems (I used a rolling ball of size 20 with light background)?
  • If you use the plastic bag, avoid to put some berries on the folds.
  • Are you sure that the min diameter is 3mm ? in your image, berry mean radius seems to be 6mm

Nico

1 Like

Sorry, I took that screen shot prematurely. I am still getting the same results when I set the scale to 300 mm (1 foot ruler).

Let me rephrase. There is no minimum, but nearly all of the berries should be at least 3 mm, and you are correct that the mean radius should be closer to 6 mm.

Trying some of your suggestions now. Thanks all!

“You should tick add to Manager to see what is selected.”
Not sure where this setting is?

No matter what I do, I cannot seem to get results in Millimeters that are accurate.

Select the ruler with a line roi, measure and see the results. Try to Set Scale and see that n pixels are equivalent to known distance 300.0 unit of length mm. Measure again. Do you have accurate results now? If not, re-do the line selection of the ruler (draw the line between the 0-stripe and the 30 cm stripe (yes, that line will be with a bit of inclination, make sure you start and end at the end of the stripes that represent 0 and 30 cm. Measure again. Do you get 300 mm?

add to ROI manager is in Analyze Particles…
It should add a ROI for every berry. You can then select each berry, one by one.

Nico

Hi jwm,

In addition to what is already mentioned, a reason why ImageJ is counting less berries is that they cluster together. Those clustered are either not counted (because of the circularity constraint) or counted as one berrie. Try using watershed on your mask (image -> binary -> watershed). That should split the berries. If you get lines between them instead, first invert the mask (edit -> invert).

After ticking ‘add to Manager’ in Analyze Particles you can show them as an overlay on your image (preferrably the original image) by activating the ROI Manager (Analyze -> Tools -> ROI Manager) and ticking ‘Show All’.

Good luck!
Bram

Update!

I think I found the source of my angst, or at least a work around. When I first posted this, I was using Image J on a Mac and getting nonsensical results. When I switched to Image J on windows, I am getting clean results that actually make sense.

While I love my Mac for personal use, I find that they don’t play always nice with programs that are originally designed for Windows. Or maybe there is some default setting on the Mac version that was throwing my results off? I honestly don’t know.

Also, using a light box underneath has greatly helped with cleaning up my images, and they now convert flawlessly to Black and White when adjusting threshold. Now I can get true diameters and counts all in one, which is far more accurate than running berries through sieves.

Thank you all for your help!

1 Like

Glad it is working! For some reason I remember one hearing that Mac defaults the Invert setting differently, so that black and white are flipped. Something that was mentioned earlier as a possibility by @VirtualSlide