Skip to content

Instantly share code, notes, and snippets.

@teh
Created June 8, 2013 14:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save teh/5735359 to your computer and use it in GitHub Desktop.
Save teh/5735359 to your computer and use it in GitHub Desktop.
Measure average grind coarseness from a picture.
from PIL import Image
import numpy
from skimage.filter import sobel
from skimage.morphology import watershed
from scipy import ndimage as nd
grind = numpy.asarray(Image.open('grind.png')).mean(axis=2)
edges = sobel(grind)
markers = numpy.zeros_like(grind)
# Grind is dark on white background (paper)
markers[grind < 70] = 1
markers[grind > 150] = 2
labels, num_features = nd.label(markers == 1)
areas = []
for i in xrange(num_features):
total = (labels == i).sum()
if total < 4 or total > 1000:
continue
areas.append(total)
print "mean {:.2f}".format(numpy.array(areas).mean())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment