The Mac app with the sand grains of a photo outlined, their axes shown, and the grain size panel open

Segment Every Grain finds every grain in a photo of sand, gravel or a thin section, lets you correct the result by hand, and reports the grain size distribution. It is the app version of the segmenteverygrain Python package (paper), for people who would rather not use Python: the same two neural networks, a U-Net and the Segment Anything Model (SAM 2.1), run directly on your Mac, iPhone or iPad.

Everything is computed on your device. Photos, grains and measurements are not sent anywhere; see the privacy policy.

The apps are coming to the App Store soon. This page will link to them when they are available.

Requirements: a Mac with Apple silicon and macOS 14 or later, or an iPhone or iPad with iOS 17 or later.

Quick start

  1. Open a photo. On the Mac, choose File ▸ Open Image (⌘O) or drag a photo onto the window. On iPhone and iPad, tap the photo button at the top left and pick Photo Library, Take Photo or Choose File. To try the app first, use the sample image.
  2. Press Segment. The app outlines every grain it finds. The sample image takes well under a minute; larger photos take longer.
  3. Correct the result. Click or tap a gap to add a missing grain; select wrong grains and delete or merge them.
  4. Set the scale from something of known length in the photo, such as a ruler or scale card.
  5. Read the grain sizes in the Grain Sizes panel, and export them as a table.
  6. Save the grains so that you can come back to them later.
The iPhone app with the grains of a gravel photo outlined and a 10 cm scale bar

The iPhone app with a photo segmented and the scale set

Taking good photos

  • Photograph the grains from straight above, in even light, and in sharp focus. Hard shadows and motion blur are read as grain boundaries.
  • Put a ruler or scale card in the picture. Without a scale, sizes are reported in pixels.
  • Grains that are only a few pixels across are usually missed. Move closer, or on iPhone and iPad use the Detailed setting.
  • Grains cut off by the edge of the photo look smaller than they are. They can be left out of the statistics (see Grain sizes).

Correcting the grains

The automatic result is usually good but rarely perfect. All changes can be undone, one step at a time.

To do this Mac iPhone and iPad
Add a missing grain Click the grain Tap the grain
Select a grain Click it; click more grains to add them Tap it; tap more grains to add them
Delete the selected grains D or ⌫ Delete
Merge the selected grains into one M Merge
Clear the selection Esc Deselect
Undo ⌘Z Undo button
Hide or show the outlines H Eye menu ▸ Grains
Show centroids and axes A Eye menu ▸ Centroids and Axes
Zoom ⌘+, ⌘−, ⌘0 to fit, or pinch Pinch; the arrows button fits the image

Only grains that touch can be merged. On the Mac, a right-click on a grain offers the same commands.

Grains that one click does not get right

When a single click outlines too much or too little, describe the grain with several points:

  • Mac: hold Option and click two or more points on the grain. Option-right-click places a point on something that should be left out, such as a neighboring grain. A preview of the outline follows the points. Release Option or press Return to create the grain; Esc discards the points.
  • iPhone and iPad: press and hold on the grain to place the first point, then tap to add more. Switch between On grain and Leave out for the next point, and tap Create Grain when the outline is right.

Setting the scale

Choose Set Scale (the ruler button), then click or tap the two ends of something whose length you know. Enter that length and its unit (µm, mm, cm or m). The scale appears as a bar on the image, and all sizes are then reported in millimeters. It can be changed or removed in the Grain Sizes panel.

Grain sizes

The Grain Sizes panel (⌘I on the Mac; Sizes on iPhone and iPad) shows:

  • a histogram and a cumulative curve of the major and minor axis lengths, on a logarithmic size axis with the names of the grain size classes;
  • the D16, D50 and D84 percentiles of both axes.

Two options change what is counted:

  • Weight by grain area weights each grain by its area, which is closer to sieving and pebble counts. Turn it off for a plain count of grains.
  • Leave out grains on the image edge removes the grains that are cut off by the edge of the photo. They stay on the image, dimmed, but are not counted or exported.

Export CSV (⌘E on the Mac) saves one row per counted grain: area, major and minor axis lengths, perimeter, orientation, position and mean color. With a scale set, lengths are in meters and areas in square meters, as in the Python package; without one they are in pixels.

Sorting grains into clusters

The cluster window, with sand grains sorted into groups that look alike

Clusters (⌘K on the Mac) sorts the grains into groups that look alike, for example by color and texture. It is a quick way to separate rock types, and to find outlines that are not grains at all.

  • Choose the number of clusters at the top; the grains are sorted again.
  • Rename a cluster after the kind of grain in it. The names go into saved grain files, the exported table and the montage.
  • Select grains in the cluster view or on the image, and move them to another cluster or to a new one.
  • Grains added after clustering can be put into the cluster they resemble most with Add to Clusters.
  • The Options menu sets the order within a cluster and the size of the pictures, colors the grains on the image by cluster, and exports a montage of all grains arranged by cluster.

Saving your work

Save Grains (⌘S on the Mac; Grain Files ▸ Save Grains on iPhone and iPad) writes the outlines of all grains, with their clusters, to a GeoJSON file. To continue later, open the same photo and then Open Grains. The file can also be read with load_grains in the Python package.

The apps warn you before you open another image, and on the Mac before you quit, if the grains have changed since they were last saved. An iPhone or iPad cannot ask when an app is closed from the app switcher, so save before you leave the app.

On iPhone and iPad:

  • Files are handed to the share sheet, from where they can be saved to Files, sent by AirDrop or mailed.
  • A photo taken in the app exists nowhere else, so it is shared together with its grain file. This can be switched off with Grain Files ▸ Save Camera Photo with Grains.
  • The photo button offers two working sizes: Standard (up to 2000 pixels on the long side) and Detailed (up to 4000 pixels, slower). Larger photos are scaled down to that size, and the grain file fits the scaled photo.

Teaching the model your own grains (Mac)

If the app keeps missing grains of a certain kind, the U-Net can be trained further on your own images. This is available in the Mac app only.

  1. Segment an image and correct it until every grain is outlined. Anything left out is taught as background.
  2. Choose Model ▸ Save Training Data and pick a folder. The image and a mask of its grains are saved there. A few images are enough to start.
  3. Choose Model ▸ Fine-Tune Model, pick the folder, name the model and start the training. It can be stopped at any time; what has been learned so far is kept.
  4. When the training ends, the app shows how the model did before and after on patches it never trained on. Keep the model and use it, keep it without using it, or discard it.

Fine-tuned models are listed in the Model menu, where you can switch between them and the built-in model at any time.

Differences between the apps

  • The Mac app works on images at full size and uses the largest SAM 2.1 model. The iPhone and iPad app scales photos down and uses a smaller SAM 2.1 model, to fit the memory of a phone. The two give very similar, but not identical, grains.
  • Fine-tuning is available on the Mac only.

Support

Questions, problems and suggestions are welcome at zoltan.sylvester@beg.utexas.edu. If something goes wrong with a particular photo, attaching the photo helps a lot.

Acknowledgments

The apps are built on the segmenteverygrain package and contain the Segment Anything Model 2.1 of Meta Platforms, Inc.; both are used under the Apache License 2.0. If you use the apps in published work, please cite:

Sylvester, Z., Stockli, D. F., Howes, N., Roberts, K., Malkowski, M. A., Poros, Z., Martindale, R. C., and Bai, W., 2025, Segmenteverygrain: A Python module for segmentation of grains in images: Journal of Open Source Software, v. 10, p. 7953.