Particle analysis module

This is a module for some basic analysis of particle size and shape by thresholding the particle and extracting data, this works for videos and for images. There is a tutorial for using this module in the tutorials folder on the github (thresholding) While this module is relatively basic, it can be very easily and effectively used.

Usage

The basic method is:
  1. apply threshold to get binary image (Threshold())

  2. Find particles from binary image (Find_contours()) and filter by size

  3. Collect data (Collect_particle_data())

This can be done as follows:

#import module
from SimpliPyTEM.Particle_analysis import *
from SimpliPyTEM.Micrograph_class import *

micrograph = Micrograph()
micrograph.open_dm('My_micrograph.dm3')

#Threshold the image using pixel value 100 (for example)
thresh = Threshold(micrograph.image, 100)

#Find the edge of the particle from binary image, only consider particles over 200 pixels in area
contours_im, mask = Find_contours(thresh, minsize=200)

#Collect the data from the contours_im (edge coordinates)
particle_data = Collect_particle_data(contours_im, pixelSize, multimeasure)

#Plot a histogram of the radius data extracted from the particles.
plt.hist(particle_data['radius'])
plt.show()

Documentation

Particle_analysis.Collect_particle_data(contours_im, pixel_size, multimeasure=False)

This collects a number of data sets from the contours_im outputted by the find_contours function. Complex measurement of particle size can be done with multimeasure (it measures the distance across each particle at multiple points and then includes max, min, mean and std of these measurements)

Data collected for each particle in this function:

Area - The area of the particle in the image Centroid - The center point coordinate of the particle (x, y) Aspect ratio - ratio between minimum and maximum length of particle Circularity - The area of the particle divided by the area of a circle that completely bounds the particle, giving a value for how circular it is (or how much of a circle it fills) width - The width of the smallest possible rectangle that could fully contain the particle Height - The height of the smallest possible rectangle that could fully contain the particle radius - The radius of the smallest possible circle that could fully contain the particle. Major-Minor Ratio - the ratio between width and height

Multimeasure specific:

Max-diameter Min-diameter Mean-diameter Stddev-diameter Number of measurements - the number of measurements across the particle to give the above diameter values.

particle_data = { ‘Area’:area_particle, ‘Centroid’:centroid_particle,

‘Aspect_ratio’:aspect_ratio_particle, ‘Perimeter’:perimeter_particle, ‘Circularity’:circularity_particle, ‘Width’:width_particle, ‘Height’:height_particle, ‘Radius’:radius_particle, ‘Major-Minor Ratio’:MajorMinorRatio}

Parameters

contours_im:list

As generated from find_contours()

pixel_size:float

Pixel size in the image, the same unit is used in the output data so not important here but worth keeping an eye on.

multimeasure:bool

Whether to measure the distance across the particle many times, this can give an idea of the variation in shape and a better measure of diameter, but also significantly increases runtime.

Returns

particle_data:dict

A dictionary containing the data collected (with keys describing what the data is)

Particle_analysis.Convert_to_single_dict(l, combine_data=False)

Take a list of dictionaries and convert to a single dictionary. This can keep data per image or combine the data from each image, as per requirements

Parameters

l: list

List of dictionaries containing particle data for each image

combine_data:bool

Do you want the data from each frame separated or together? True for together

Particle_analysis.Find_contours(thresh, minsize=200, complex_coords=False, maxsize=100000, remove_edges=True, labelled=False, threads=5)
Particle_analysis.Flatten_list(l)

Simple function to make a single list from a list of lists, useful for combining data from different frames

Parameters

l: list of list of lists

These can be created if from the particle_analysis_video function or if you do particle analysis of multiple frames, the data from these can be combined with this function.

Returns

single list of values

Particle_analysis.Particle_analysis(image, threshold, minsize, pixel_size, multimeasure=False)

Do the thresholding, contours finding and data collection all in one to collect data from the particles in an image

Parameters

image:numpy array

The image to analyse

threshold:int

The threshold pixel value

minsize:int

minimum area of particles

pixel_size:float

Pixel size in the image, normally in nanometers but this unit is kept in the resulting data so it doesnt matter.

Multimeasure:bool

Whether to measure the distance across the particle many times, this can give an idea of the variation in shape and a better measure of diameter, but also significantly increases runtime.

Returns

mask:numpy array

A binary image showing the particles selected in white.

particle_data:dict

A dictionary containing the data collected (with keys describing what the data is)

Particle_analysis.Particle_analysis_video(video, threshold, minsize, pixel_size, multimeasure=False)

Runs the particle analysis for every frame in a video and creates a dictionary with a list of lists (data from each frame) as the value.

Parameters

video:numpy array

The video to analyse

threshold:int

The threshold pixel value

minsize:int

minimum area of particles

pixel_size:float

Pixel size in the image, normally in nanometers but this unit is kept in the resulting data so it doesnt matter.

Multimeasure:bool

Whether to measure the distance across the particle many times, this can give an idea of the variation in shape and a better measure of diameter, but also significantly increases runtime.

Returns

mask:numpy array

A binary video showing the particles selected in white.

particle_data:dict

A dictionary containing the data collected (with keys describing what the data is)

Particle_analysis.Sidebyside(Video1, Video2)

Stitches two videos together side by side - (good for comparing masks and originals)

Parameters

Video1:numpy array

Lefthand video

Video2:numpy array

Righthand video

Returns

sidebyside: numpy array

Single videos where the two videos play side by side

Particle_analysis.Threshold(image, threshold, brightfield=True)

Threshold the image to a particular value, such that below that value goes to black and above that value goes to white.

Parameters

image:numpy array

The image to be thresholded

threshold: int

The threshold value

returns

thresh:numpy array

The thresholded image

Particle_analysis.multiMeasure_particle(particle_contours, centroid)

Measures the diameter of the particle at multiple points around the particle by seeing if the angle between any two points on the perimeter and the center of the particle is 180 +/- 1 degree.

Parameters

particle_contours: list

A list of the coordinates bounding the particle (each list within contours_im)

centroid: array

The central coordinate of the particle.

returns

distances: list

A list of the diameters measured

coordinates

The pairs of coordinates measured (point1, center point, point2)

Particle_analysis.process_label(label, maxsize, minsize, shape, labels)