Finding grains

My colleague asked me to help him out with this image:

Grains in an SEM image (78 pixels per micron)

Grains in an SEM image (78 pixels per micron)

He needs to know the grain size distribution, and they’ve been having trouble automating this. He knew I’d been doing some work with Mathematica’s image analysis capabilities so he thought maybe I could make some headway. This post shows my current progress.

My first idea was do use EdgeDetect to find the boundaries:


This is the result of using EdgeDetect in Mathematica on the original image

This seems to isolate most of the grains, but I need Mathematica to isolate the areas that the edges separate. What I decided to do was to darken up the edges by dilating them and subtracting them from the original image:

original image with edges darkened and the rest set to white

original image with edges darkened and the rest set to white

Now I use the very cool MorphologicalComponents command to get this:

overlay of the identified grains on the original image. If it's colored, it's identified.

overlay of the identified grains on the original image. If it’s colored, it’s identified.

Here’s an animation that slowly identifies the grains:

Slowly reveals the grains (the last frame is shown in the previous image)

Slowly reveals the grains (the last frame is shown in the previous image)

Cool, huh? I thought so. I’m waiting to hear from my colleague to see if this is the sort of identification he needs. My guess is that he wants something like a histogram of the areas of the grains. With ComponentMeasurements, that’s super easy:

Histogram of the areas of the grains (in square microns)

Histogram of the areas of the grains (in square microns)

Ok, so now I admit to you my ignorance. I have no idea how people do this, though I did find this standard (paywall) at the American Society for Testing and Materials (ASTM). I’m hoping some of you can help me out with refining this technique. It’s really fast in Mathematica to run it, and I think it’s pretty robust, but it does clearly miss a few grains and inadvertently joins a few.

Thoughts? Here are some starters:

  1. This is cool, but I’d love to know the exact Mathematica commands.
  2. This is dumb, there’s a much better way to do it, and here’s how . . .
  3. This is cool, can I send you all my data so that I can graduate sooner?
  4. This is dumb, we pay grad students to do this by hand so that they learn to hate. Don’t make this available.
  5. This is cool, but I bet it would struggle with . . .
  6. This is dumb, it only worked this time because . . .


About Andy "SuperFly" Rundquist

Professor of physics at Hamline University in St. Paul, MN
This entry was posted in mathematica, physics. Bookmark the permalink.

9 Responses to Finding grains

  1. jg says:

    This is very cool. I’m getting bummed that my grad program is a MATLAB shop instead of MMA.

    • Andy "SuperFly" Rundquist says:

      All of these transformations are mostly straightforward mathematical calculations done at the pixel level. Mathematica just has some nice built in functions to do them. I wouldn’t be surprised to hear Matlab has similar functionality.

  2. EMC says:

    Very cool! Reminds me of two things that were associated with my grad research. Etch pit density (EPD) characterization and Electron Back Scatter Diffraction (EBSD, specifically crystallographic orientation mapping). The whole grad student counting thing has always been a joke for EPD calculations and I think I’ve actually seen a presentation showing that the brute force (i.e. grad student) method was the most reliable for counting accurately. ImageJ has some capability for this type of analysis if you spend some time with it. As far as EBSD, I think most systems come with analysis software. Oxford has a nice page about the technique, their equipment, and their software ( I suspect that the U of M has an SEM with EBSD capability. As far as raw SEM image analysis though, your stuff is a about as good as I’ve seen.

  3. Niki says:

    very impressive. I am kind of having the same problem as your friend. Are you willing to send me the Mathematica code?

  4. Isabelle Cauwet says:

    Would you allow us to use this image to illustrate our MountainsMap software?
    We use segmentation method and it works well on this example. Thank you in advance.
    If you contact me I can send you an image of what we obtain in one click.

    • Andy "SuperFly" Rundquist says:

      Sure. Please just mention me and my colleagues whenever you post it somewhere. A link here to the results from your approach would be great!

  5. Jérémie says:

    great work! exactly what I was looking for!
    it’s a bit an old post and maybe you’re not working on this anymore, but would it be possible to access the code? since then, did you make additional progress on this topic?
    thanks a lot

    • Andy "SuperFly" Rundquist says:

      I think I have the code somewhere. email me at firstname.lastname at gmail

      On Tue, Jul 19, 2016 at 11:04 AM, SuperFly Physics wrote:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s