San Luis Obispo County
University of California
San Luis Obispo County

Berry counting


Counting winegrape berries using image analysis
 
Mark Battany

 

Background:

 
This website describes a novel method for counting grape berries, which may be a suitable replacement for the labor-intensive manual counting which is commonly employed. The advantage of this method is that it is likely to be much faster, more pleasant to do, and potentially more accurate, as compared to manual counting.

Briefly, in this method loose berries are placed onto a glass-bottomed tray, which is then placed onto a flatbed scanner, and an image is made of the berries; this image will show the light-colored individual berries against a dark background. An image analysis software program is then used to count how many berries appear in the image. It takes much longer to explain it than to do it; the instructions below may look confusing at first, but once you become accustomed to the method it goes very quickly.

Note that the method as described works quite well with firm green berries (pre-veraison); modifications to the method will likely be needed if counts of dark berries are done. In practice, if you are counting berries from a large number of clusters, you will probably find it faster to first do all the scanning as a group, and then follow this with all the image analysis using ImageJ.

Equipment required:

  • A computer (Windows or Mac), preferably a later model with a high speed processor;
  • A common flatbed scanner; in my work I use a Canon “Canoscan LiDe 90”
  • A homemade berry tray and cover;
  • ImageJ software, downloaded for free from http://rsb.info.nih.gov/ij/ (these instructions are for version 1.42q of the software)

DSC_4031
 
Steps:

Setting up the equipment:

1)Install the scanner software on the computer according to manufacturer’s instructions

2)Remove scanner lid (optional, but makes it easier to work; on the Canon the lid pops off quite easily). Alternatively, you can attach a dark background to your existing scanner lid.

3)Download and install the ImageJ software on your computer

Making a berry tray:

The removable berry tray serves as a convenient tool to transfer berry samples to and from the scanner; while it may be possible to place berries directly on the platen of the scanner (the glass or plexiglass surface), this is not very practical. The berry tray is simply a sheet of flat clear glass, approximately 1/8” (3mm) thick. The glass is cut to a rectangular shape roughly the same size as the platen of the scanner. In this example, the glass was cut to dimensions of 8 ½” x 12” (21.6cm x 30.5 cm). A border is then glued on one side of the glass plate, around the edge; this forms an enclosed area, which keeps the berries from rolling off of the glass plate and also supports the cover which will be placed on the berry tray when scanning. In this example, I used ¾” plastic moulding to make the border; other materials will also work. The underside of the berry tray directly beneath the border is covered with black electrical tape, to prevent the border from appearing in the image. If you would like to try this method and need a berry tray, please contact me.

Copy of DSC_4025

Copy of DSC_4017

Preparing berry samples:
This method works best when berries to be counted are clean, dry, and uniformly green. Pull all berries from the rachis and place them in an open contanier, such as a small disposible plastic bowl. If you store the berries in bags, particularly in the refrigerator, they will often be damp with condensed water, which may make this method less efficient. Note that berries with substantial dark spots (wounds) may not count well with this method.
Scanning berries:
 
1)Remove the berries from the cluster rachis, preferably removing the pedicels from the berries;
 
2)Place the berries directly onto the berry tray, which is then placed onto the platen;
Copy of DSC_4027

3)Cover the glass frame with a dark-colored flat board; this provides the dark background in the initial scanned image;

Copy of DSC_4028

4)Start the scanner software;
 
5)Use the following scanner settings:
 
a.Color Mode: GRAYSCALE or BLACK & WHITE
b.Document Size: PLATEN (or appropriate size for your tray)
c.Resolution: 200 DPI
d.Save as file type: JPEG

6)With the above settings, scan the berry sample;

7)If you want to see the scanned image, open it in a program such as “Microsoft Office Picture Manager”, or the picture management software which came with your scanner (your scanner software may open the image automatically). You should see an image of light-colored berries on a dark background. If you would like to try using the ImageJ program, you can download practice images at the bottom of this page.

berries scanned 1

 

Counting the berries using the ImageJ program:

 

1)Open the ImageJ program; in the explanation below, the text in upper case blue font refers to menu titles in the program;
 
2)Choose FILE>>OPEN… and select the scanned image file from its folder on your computer; the image will appear as grey colored berries on a black background, as in the image below;
berries scanned 2
 
3)Choose IMAGE>>ADJUST>>THRESHOLD...
This step initially turns the image into black background with red berries; in the "Threshold" window there is a pull-down menu; change this from RED to B&W, and the image will turn to white berries on a black background. Now check the box marked DARK BACKGROUND to result in black berries on a white background as in the picture below;
berries scanned 2.5

4)   In the "Threshold" window there are two horizontal bars; you can use the mouse cursor to drag the top bar to the left to make the berries become fuller, if necessary; compare the picture below to the one above to see the results. This step may not be necessary, but if the scanned image does not have a distinct color difference between the berries and background this step can be helpful. Some berries will now touch; this will be corrected later. Close the "Threshold" window.

berries scanned 3

5) You should now have a black & white (binary) image showing black berries on a white background. Depending on the design of the berry tray you are using, you will also see some dark patches around the border of the image which are not berries, as in the image below. Note that some scanner programs will automatically crop the image to only show the berries.

berries scanned 4

6)   Choose PROCESS>>BINARY>>WATERSHED

This important step now places a dividing line between adjacent berries which are touching in the image; this is necessary otherwise the software will count any touching berries as one single 'particle'. This is not a perfect process; some distorted berries may inadvertently be split into two but the error rate is usually fairly small. It may appear that numerous berries are still touching in the resulting image, but if the image is expanded to full size the divisions will be obvious, as in the example below.
Watershed picture

7)Using the mouse cursor, select a square around the berry sample portion of the image; this shows up as a yellow box in the image below. The analysis in the next step will only evaluate the portion of the image within the yellow box; thus if your berry tray produces a black border in the binary image, this is an easy way to not include that section in the analysis.

berries scanned 5

8)   Choose ANALYZE>>ANALYZE PARTICLES to bring up the dialog box shown below. In this dialog box, place a check in the SUMMARIZE box. For SIZE (PIXEL^2), enter 500-8000; this means that the software will only count those particles which have an area between 500 and 8000 pixels. If this selection is kept with the default setting of 0-INFINITY, the very smallest particles in the image, such as those made by tiny bits of debris, will be counted as berries and will lead to very inaccurate measurements. A value of 500 is roughly equivalent to a 1/8” diameter berry, while 8000 is about ½” diameter; adjust these range values accordingly if the sizes of the berries you are measuring are outside these limits. Note that these settings are for an image scanned with a resolution of 200 dpi; if you scan with a different resolution, you will need to change these pixel size boundaries. See the chart at the bottom of this page for the relationship between square pixels and berry diameter for different scanner resolutions.

berries scanned 6

9)The results of the particle analysis are displayed in a new “Summary” window (shown overlaying the image below). Under the column labeled ‘Count’ is the total number of berries counted.

berries scanned 7

 

Optional steps:

If you would like to determine the pixel size of the individual berries that are counted, you can do this readily in the ANALYZE PARTICLES window. Place a check in the box labeled DISPLAY RESULTS:

berries scanned 8

This will produce a separate window with data values for each berry. The leftmost column is the berry number, and the column labeled "Area" is the number of pixels for each berry. This file can be saved, and opened later with a spreadsheet program.

berries scanned 9

If you would like to see which berries in the image are counted, in the ANALYZE PARTICLES window select OUTLINES in the SHOW: menu.

berries scanned 10

This step creates a new image file, in which each counted berry is now displayed as an outline. This image has been expanded below to better show the outlines.

berries scanned 11

 

Summary

This method, developed in early 2008, is now being used to assist yield forecasting for thousands of acres of winegrapes in California. Employees who previously were tasked with counting berries by hand have reported significant increases in productivity, comfort, and accuracy when using this method. If you try out the method and have any feedback on how it works for you, and suggestions for improvement, I'd like to hear about it. Please email me at mcbattany@ucdavis.edu


Tips

If you are using an old computer which works very slowly in both the scanning and image analysis operations, you can speed it up by using a 100 dpi resolution rather than a 200 dpi resolution as shown above. Just be sure to make the appropriate changes in the settings of the ANALYZE PARTICLES window of ImageJ. See the table at the bottom of this page for guidelines on these settings.

It is generally easier to remove berries from the rachis if the fruit is frozen. If you are working with frozen fruit, you will have to dry off the glass berry tray in between measurements. If you leave the cold sample in contact with the scanner platen for an extended period, it may lead to condensation on the inside of the platen, which will fog the subsequent images. This can be avoided by only placing the berry tray on the scanner to take the image, and removing promptly.

If you have programming skills, all of the steps outlined above in ImageJ can be set up to run automatically by using the built-in macro/script functions. If you set the program up this way, it would allow you to first scan all of your berry sample images, and to then have the ImageJ program process all of the images in one automated operation. This can save considerable time.

 

Practice images

You can download the scanned berry images below to practice with ImageJ:

Image 1 Image 2 Image 3 Image 4

Image 5 Image 6 Image 7 Image 8


berry pixels

 

To simplify information, trade names of products and/or company names may have been used. No endorsement of named products and/or companies is intended, nor is criticism implied of similar products and/or companies which are not mentioned.
Webmaster Email: kjerdin@co.slo.ca.us