The Flake Extraction Process - derangedhk417/MicroscopeControl GitHub Wiki

IMPORTANT

This is out of date. A different process is used to extract flake contours now. Documentation TBD.

Introduction

This page attempts to describe the process used to extract flake geometry from an image in as much detail as possible.

The Process

The flake extraction process can be loosely broken into the following steps.

  1. Remove noise and determine where image features are located.
  2. Contour these image features.
  3. Make out the image features and determine the background color by averaging what remains.
  4. Subtract the background from the image and set all pixels below a certain threshold value to zero.
  5. Contour what remains, filtering out everything below a certain size threshold.
  6. Calculate the contrast value for each flake.
  7. Write the contour data, bounding rectangles and contrast values for each flake into a dictionary.
  8. Write this dictionary to a json file with the same name as the image file.

The actual number of steps necessary to produce the final result is closer to 40.

Note: Many of the images displayed below are not true color, because conversions from RGB to BGR and to HSV are involved in parts of this process.

Steps 1-3

Original Image Used for this Documentation

original_image

  • The image is down-scaled by a factor of 5 on each axis and converted to HSV.

downscaled_and_hsv

  • Fast non-local means denoising with a strength parameter of 30 is applied.

denoised

  • A Laplacian filter with a kernel size of 3x3 is applied to the image. The maximum of this across the H, S and V channels is taken in order to convert this into a single channel.

laplacian

  • All pixels with a Laplacian of less than 28 are set to zero. This value seems to work universally. You may have to experiment with it if you run into issues.

laplacian_removed

  • The image is dilated using a 4x4 elliptical filter. This closes any gaps that may be present and ensures that the contours do not under-fit the flakes (therefore negatively effecting the background measurement).

4x4_dilate

  • The image is then eroded using a 4x4 elliptical filter. This ensures that any extremely small islands or noise that survived the other filtering processes are removed.

4x4_erode

  • The image is then "leveled". This refers to setting all pixels with a value greater than zero to one. This is necessary to make edge detection simple.

leveled

  • A small border is added to the edges of the image. This ensures that flakes cut off at the edges of the image will be correctly processed by the contouring algorithm.

bordered

  • Canny edge detection with a lower threshold of zero and an upper threshold of 1 is applied. This does an excellent job of extracting edges at this point.

edged

  • The edges are dilated using a 2x2 elliptical filter to close any holes that may be present in the edges. This is necessary for the contour extraction algorithm to work correctly.

2x2_dilated

  • Contours are extracted from the edges created in the last step and a mask image is created where all pixels containing an image feature (non-background element) are set to 1.

mask

  • The background color is computed by averaging all pixels that are not covered by the mask. A background subtracted image is constructed by subtracting this background color from the original image and then converting the result to greyscale.

bg_subtracted

Note: This image is greyscale, but its intensity values have been mapped to the matplotlib default colormap in order to make the features more distinct.

Steps 4-6

This steps are carried out on the full resolution image.

  • Every pixel below a "noise threshold" is set to zero.

noise_threshold

  • The image is leveled

leveled

  • The image is eroded with a 7x7 elliptical filter. This removes any small islands that may not have been removed by the noise threshold. It also results in the contours being inside of the actual flakes. This reduces the amount of background that makes in into the final contrast statistics. Without this under-estimation of flake boundaries, more background around the edges of flakes would make it into the final statistics.

leveled

  • The image is bordered to ensure that flakes cut off at the edge of the image are contoured correctly.

  • Canny edge detection is run on the image.

leveled

  • The edges are dilated with a 2x2 elliptical filter.

leveled

  • A final contrast image is computed using the definition from the paper INSERT LINK. A list of contrast values is extracted from within the computed contours. This information is used later when determining the distribution of contrast values within flakes.

leveled

The Final Contours

leveled