Morphological Operators for ImageJ
You probably downloaded this file and plugins as a single zip file from:
http://www.dentistry.bham.ac.uk/landinig/
This is my collection of ImageJ plugins to perform various morphological operations.
All plugins are recordable. Some macros that make use of those plugins are also included.
While most macros were written to deal with white particles, the plugins, deal with both, black or white particles (this means that the macros can be easily modified to deal with black particles too).
Tested under ImageJ 1.34o.
Please make sure that you have the latest version of the ImageJ ij.jar file.
Disclaimer: The software in these pages is experimental. Use these plugins at your own risk. Although I have made every effort to make sure that they run as intended, there may be bugs and unexpected behaviour in instances that I have not envisaged. Please send any comments, problems or improvements to G.Landini at bham. ac. uk
Copyright notice: Please check the copyright notices in the source code of each plugin. Some of the plugins are modifications of earlier versions written by other authors and therefore their original copyright notices apply. Please respect the original authors authorship notices. If no specific copyright notice is included, then consider the plugin as free software: you can redistribute it and/or modify it under the terms of the GNU General Public license as published by the Free Software Foundation; either version 2 of the license, or (at your option) any later version. These programs are distributed in the hope that they will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with the programs; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
BinaryConditionalDilate_.class
This plugin dilates (3x3 neighbourhood, 8-connected) particlesin an image (called seed) inside another image (called mask).
The procedure can be applied n of times, or until idempotence if n = -1. In that case the procedure becomes is the same as BinaryReconstruction.
BinaryConditionalErode_.class
This plugin erodes (3x3 neighbourhood, 8-connected) particles in an image (called seed) except what is masked in another image (called mask) (i.e. the mask "protects" what should not be eroded).
it can be applied n times, or, until idempotence if n = -1.
BinaryConnectivity_.class
Returns the number of connected pixels (+1) to each foreground pixel (8 neighbours): background = 0, single pixel = 1, end of a line = 2, bifurcations = 3, triple points = 4, etc. Brightness/Contrast must be adjusted to see the result.
BinaryDilate_.class
This plugin performs a 3x3 8-neighbour Binary Dilation of a binary image.
The differences with the built in Dilation in ImageJ are:
- It checks that the image is binary[0] & [255]
- Processes all pixels in the image (ImageJ leaves the borders without processing).
- Supports "coefficients" (0-7). This is a logical constrain of the operation to locations where the number of neighbouring pixels in the opposite state is the coefficient: Classical dilation (0), Filling of single pixels holes (7), Filling of small crack lines (6), Filling of (some) irregular holes (5). See reference [2]for further details.
- Can be applied n times without using SetIterations. Use -1 for dilation until the image is idempotent (no more changes).
- BinaryDilateTest.txt is a macro that shows the effect of the different coefficients.
Notice: This plugin will be discontinued since ImageJ 1.33q fixed a bug that prevented border processing and also implements the use "coefficients". Note, however that in ImageJ, the coefficients range from 1 to 8 instead of 0 to 7.
BinaryDilateNoMerge4_.class
BinaryDilateNoMerge8_.class
These plugins perform a conditional binary dilation of a binary image (without merging particles together). The results are similar to a binary watershed transform of the background, partitioning it into areas of influence of the particles. Dilations are done with 4 or 8 pixel structuring elements respectively. The number of iterations can be set. Use -1 for dilation until idempotence.
Very slow, maybe there are better algorithms, but slow seems better than nothing...
If you need to dilate without merging until idempotence, then use
the macro Influence_Zones.txt described below.
BinaryErode_.class
This plugin performs a 3x3 8-neighbour Binary Erosion of a binary image.
The differences with the built in Erosion in ImageJ are:
- Processes all pixels in the image (ImageJ leaves the borders without processing)
- Supports "coefficients" (0-7). This is a logical constrain of the operation to locations where the number of opposite state neighbours is the coefficient: Classical erosion (0), Removal of single pixels (7), Pruning of 8-connected lines (6), Removal of (some) irregular particles (5). See reference [2]for further details.
- Can be applied n times without using SetIterations. Use -1 for erosion until the image is idempotent (no more changes).
- BinaryErodeTest.txt is a macro that shows the effect of the different coefficients.
- BinaryPrune.txt is a macro that prunes all the branches of a binary skeleton, leaving only the closed loops. See also PruneAll.txt for another pruning method.
Notice: This plugin will be discontinued since ImageJ 1.33q fixed a bug that prevented border processing and also implements the use "coefficients". Note, however that in ImageJ, the coefficients range from 1 to 8 instead of 0 to 7.
BinaryFill_.class
This plugin fills holes in 8-connected particles (and also in child-particles) of a binary image. This function was incorporated in ImageJ v.1.31o (Process->Binary->Fill holes).
- Fill_Border_Holes.txt
Fills holes including those touching the borders of the image
This cannot be done with the "Fill Holes" command alone. Holes are assumed to be not only the traditional
4-connected background elements, but also those that touch the image borders, but extending no more
than 2 contiguous borders (i.e. touching one border or a corner).
BinaryFilterReconstruct_.class
This plugin filters 8-connected particles in a binary image that otherwise would disappear after n erosions.
The difference with morphological Opening is that BinaryFilterReconstruct preserves the original shape or the particles (Opening tends to smooth the boundaries of particles).
The algorithm is n erosions, followed by a Binary Reconstruction of the original image based on the eroded image as the seed.
BinaryHitOrMiss_.class
This plugin returns the locations of the image that match the kernel pattern.
The pattern is a 3x3 neighbourhood where 0=empty, 1=set, 2=don't care.
- Corners.txt: Returns the corners of a binary particle based on the Hit-or-miss transform.
BinaryLabel8_.class
ImageJ plugin for labelling particles (8 neighbours) in a binary image.
Can label up to 65530 particles in a unique greyscale value (from 1 to 65531), after that, the colours are recycled.
The output is a new 16 bit greyscale image with re-scaled brightness.
BinaryReconstruct_.class
This is a very powerful morphological operation that reconstructs (retains) 8-connected particles in an image (called mask) based on markers present in another image (called seed).
It consists of dilating the seeds inside the mask (so particles that do not have seeds are not reconstructed).
This procedure is called "Feature-AND" in reference [2].
- Hysteresis.txt: This macro uses Binary Reconstruction to aid thresholding an image.
Hysteresis thresholding is useful to segment edge gradients. The macro assumes that the object is bright (usually the gradient after applying some edge detector).
First set the threshold of the "safe zone" (for sure belongs to the "object") then set the threshold for the "unsafe zone" (for sure is outside the "object").
The in-between zone is called the "fuzzy zone". Hysteresis thresholding adds those parts of the fuzzy zone that are connected to the safe zone.
The macro creates 2 images: "Reconstructed" is the hysteresis-thresholded image, while "Result of Result" shows the safe zone (white), the pixels added to the safe zone (yellow), the pixels of the fuzzy zone not added (purple) and the unsafe zone (black).
The "Reconstructed" image corresponds to the white+yellow components of the "Result of Result" image.
- BinaryKillBorders.txt: This macro uses the reconstruction of the intersection of the image border with a 1 pixel thick box boundary to eliminate particles touching the border of the image.
BinaryThick_.class
BinaryThick2_.class
These two plugins dilate the locations of the image that match one (BinaryThick_.class) or two (BinaryThick2_.class) kernel patterns.
First dilates then rotates the kernel, if set to do so. The pattern is a 3x3neighbourhood where 0=empty, 1=set, 2=don't care.
- ConvexHull1.txt: a morphological convex hull -- square shape (not the true convex hull)
- ConvexHull2.txt: a morphological convex hull -- diamond shape (not the true convex hull)
- ConvexHull3.txt: a morphological strong convex hull. If done with 'rotate 90' it gives a quasi-convex hull (not the true convex hull)
BinaryThin_.class
BinaryThin2_.class
These two plugins erode the locations of the image that match one (BinaryThick_.class) or two (BinaryThick2_.class) kernel patterns.
First erodes then rotates the kernel, if set to do so. The pattern is a 3x3 neighbourhood where 0=empty, 1=set, 2=don't care.
- Boundary4.txt: produces a 4-connected boundary of particles
- Boundary8.txt: produces a 8-connected boundary of particles
- Influence_Zones.txt: creates influence zones by skeletonisation of the background followed by pruning. This is a fast way of "dilation without merging until idempotence"
- MCentroids.txt: creates the morphological centroids by thinning. The centroid of an object with a hole is a ring, otherwise it is a point.
- Prune1.txt: prunes a binary skeleton once.
- PruneAll.txt: prunes a binary skeleton until idempotence.
- Skeleton1.txt: an 8 connected skeleton, different from the one in ImageJ.
- Skeleton2.txt: an 8 connected skeleton, different from the one in ImageJ. It creates many lateral "bones" or branches.
- Skeleton3.txt: similar to skeleton1 but 4-connected.
Domes_.class
This plugin extracts "domes" in a greyscale image.
Domes are bright 8-connected regions of a chosen height (measured from their top downwards) in the greyscale function.
It can also return "basins" (locally dark regions) instead of domes.
Domes and basins are good candidates to extract reconstruction markers in images with uneven backgrounds. See reference [3].
- RegionalMaxima.txt
- RegionalMinMax.txt
- RegionalMinima.txt
These 3 macros are used to extract Regional Maxima, Minima or both in a greyscale image. They require the Domes_.class plugin above.
RegionalMaxima.txt returns the regional maxima as white pixels on a black background.
RegionalMinima.txt returns the regional minima as black pixels on a white background.
RegionalMinMax.txt returns the regional maxima as white [255] pixels and the regional minima as black pixels [0] on a grey background [127].
Regional maxima (and minima) are connected sets of pixels with a given value h (plateau at altitude [or depth] h) such that every pixel in the neighbourhood of the regional maxima (or minima) has a strictly lower (or higher) value.
All regional maxima (minima) pixels are local maxima (minima), but not all local maxima (minima) pixels are regional maxima (minima). Confusing? See Vincent's reference [3].
- LocalMaxima.txt
- LocalMinMax.txt
- LocalMinima.txt
These 3 macros are used to extract Local Maxima, Minima or both in a greyscale image. Added here for comparison with the 3 Regional macros above.
GreyscaleDilate_.class
GreyscaleErode_.class
These plugins perform a 3x3 Binary Dilation/Erosion of a greyscale image.
Same as the Min and Max filters of radius=1 (8 neighbours) in ImageJ, but:
- Deal with black or white foregrounds.
- They can be applied n times.
- GreyscaleTopHat.txt detects light spots of a particular size on a dark background
- GreyscaleWell.txt detects dark spots of a particular on a bright background
Greyscale "Proper" Morphological Filters (macros)
- GreyscaleProperOpen.txt: returns Min(f,COC(f)).
- GreyscaleProperClose.txt:returns Max(f,OCO(f)).
- GreyscaleProperAutomedian.txt: returns Max(OCO(f),Min(f,COC(f))).
GreyscaleReconstruct_.class
This plugin reconstructs a greyscale image (the "mask" image) based on a "seed" image. This is an implementation of the parallel algorithm from [3].
It is very important to read Vincent's paper to understand greyscale reconstruction and its applications.
The reconstruction algorithm is: iterated 8-neighbour geodesic dilations of the seed UNDER the mask image until stability is reached (the idempotent limit).
It supports stacks reconstructed by a single seed image and also a single image reconstructed by a stack of seeds. Based on Calculator_Plus by Wayne Rasband.
Morphological Gradients and 2nd Derivative macros
- MorphologicalGradient1.txt: difference between dilation & erosion.
- MorphologicalGradient2.txt: difference between closing & opening.
- MorphologicalGradient3.txt: difference between MorphologicalGradient1 & MorphologicalGradient2.
- MorphologicalSharpGradient.txt: minimum of ((dilation-original) & (original-erosion)).
- Morphological2Derivative1.txt: original-(average of erosion & dilation).
- Morphological2Derivative2.txt: original-(average of opening & closing).
- Morphological2Derivative3.txt: Morphological2Derivative1-Morphological2Derivative2.
- MorphologicalSmoothing1.txt: average of erosion & dilation.
- MorphologicalSmoothing2.txt: average of opening & closing.
EDM_16bits.txt
This macro produces an Euclidean Distance Map on a binary image [the object over which the EDM is calculated is assumed to be 255 and the background 0]. The macro extends the built in ImageJ command to distances of up to 65535 pixels. The result is a 16 bit image.
Particles4_.class
Particles8_.class
Particles8_Plus.class
These are plugins for estimating various statistics of binary 4- and 8-connected particles.
Warning!:
The plugins assume square pixels only.
These plugins do not return the same values as the built in ImageJ Analyze Particles command because they use another concept to extract area and perimeter.
Here, Perimeter and Area are measured from the centres of the boundary pixels of a particle.
Area disregards "holes" in the particles (i.e. it returns the area inside the boundary), but Pixels
returns the number of pixels forming the particle (a particle with holes will therefore have more Area than Pixels).
Also note that here Area is calculated from the polygon formed by the boundary pixels.
If the particle has no holes, then Area is smaller than Pixels (since the polygon is located
in the centre of boundary pixels). Note that using this logic, Area for 1 pixel particles is 0, for a 2x2 square it is 1, etc.
while the value of Pixels in each particle is what you see.
Why to write such a plugin?
This was created to return exactly the number of pixels in particles when dealing with synthetic images (such as percolation clusters).
The Analyze Particles command in ImageJ performs a running average of size 3 on the perimeter coordinates when calculating the polygon-area of particles.
That is a good way of reducing the effect of pixelation when calculating area and perimeter of digitised images, but in some instances one may want to know exactly (in pixels) the area of particles/clusters.
These plugins can label the particles in different colours. Some colours are reserved for the particle detection and various calculations, so there are only 250 labelling colours available (1 to 251).
It is therefore possible when using Particles4_ that two 4-connected particles which are corner neighbours could end up labelled with the same colour and therefore look like an 8-connected particle (this could happen
when a very large particle is surrounded by many small ones).
Although the labelling may be confusing, the statistics generated are correct.(If one only needs to label the particles unequivocally, then it is better to use the BinaryLabel8_.class plugin because it
can label up to 65530 particles in unique greyscale values --after that, it also recycles the labelling colour.)
The plugins can draw the centre of mass (rounded to the nearest pixel, labelled or not) of each particle or the start coordinates of each particle (the only pixel with 100% certainty that it belongs to the particle).
These may be useful for reconstruction purposes (see the KeepParticlesInRange.txt macro below and BinaryReconstruct_ plugin above).
Filtered particles (smaller than the minimum and larger than the maximum sizes) are deleted from the image.
New in Version 1.6
The labels of the options have been modified so they are consistent with those of the built in Analyzer in ImageJ.
A new option "Overwrite Results" was added to prevent the macro asking to save or delete the current Results when executing from a macro.
The results of the particle analysis are now sent to the ResultsTable, so the data generated can be retrieved from a macro for further processing. The macros below show how to do it.
- KeepLargestParticle.txt is a macro modified from CircularParticles.txt to select the largest particle(s) in a binary image. It uses the built it Analyzer and makes the selection based on the 'Area' of the particles. If the largest particle has holes and child-particles, they will be kept in the image.
- KeepLargestParticlePixels.txt uses Particles8_.class as the analysis engine and selects the largest particle(s) based on the number of 'Pixels' in it. This is different from the 'Area' as it considers holes in the particle. If the largest particle has holes and child-particles, they will not be kept in the image. This example also works with Particles4_.class as analysis engine.
- KeepParticlesInRange.txt is another example that selects particles in the range of 10 to 20 pixels by reconstructing the original image based on the starting coordinates of particles made of 10 to 20 pixels. Do not use Particles4_.class as the analysis engine with this macro because the reconstruction is based on 8-neighbours rather than 4.
Note: Although these plugins can analyse stacks, the results from all the slices are stored in the same ResultsTable. The data has an extra column named 'Stack' that indicates where the measurements belong to. Therefore, the macro examples above need to be modified to work properly with stacks.
New in Version 1.7
The plugin analysis has been extended to include the following new parameters:
- the Maximum Feret's Diameter (Feret) (the longest distance between 2 points in the perimeter),
- the coordinates of the points defining the Feret's diameter (FeretX1, FeretY1, FeretX2, FeretY2),
- the angle (FAngle) in degrees with the image horizontal (0..180).
Particles8_Plus adds more parameters (complete list below).
New in Version 1.8
Fixed bug that did not delete single border pixels when "exclude edge particles" was selected.
Added CountCorrect parameter for unbiased counting of particles. This parameter should be used with "exclude edge particles" checked.
Here is the complete list of parameters:
- Slice: Slice number in the stack (if any)
- Number: Blob number in the slice
- XStart: The x coordinate of the first scanned point in the particle
- YStart: The y coordinate of the first scanned point in the particle
- Perim: The Perimeter, calculated from the centres of the boundary pixles
- Area: The Area inside the polygon defined by the Perimeter
- Pixels: The number of pixels forming the blob
- XM: X coordinate of the centre of mass or momentum
- YM: Y coordinate of the centre of mass or momentum
- ROIX1: X coordinate of the top-left corner of the ROI that encloses the particle
- ROIY1: Y coordinate of the top-left corner of the ROI that encloses the particle
- ROIX2: X coordinate of the bottom-right corner of the ROI that encloses the particle
- ROIY2: Y coordinate of the bottom-right corner of the ROI that encloses the particle
- MinR: Radius of the inscribed circle centred at the centre of mass
- MaxR: Radius of the enclosing circle centred at the centre of mass
- Feret: Largest axis length
- FeretX1: X coordinate of the first point of the Feret
- FeretY1: Y coordinate of the first point of the Feret
- FeretX2: X coordinate of the second point of the Feret
- FeretY2: Y coordinate of the second point of the Feret
- FAngle: Angle (in degrees) of the Feret with the horizontal (0..180)
- Breadth: The largest axis perpendicular to the Feret (not necesarily colinear)
- BrdthX1: X coordinate of the first Breadth point
- BrdthY1: Y coordinate of the first Breadth point
- BrdthX2: X coordinate of the second Breadth point
- BrdthY2: Y coordinate of the second Breadth point
- CHull: Convex Hull or convex polygon calculated from pixel centres. (This value is the same as the perimeter only for rectangular particles),
- CArea: Area of the Convex Hull polygon,
- MBCX: X coordinate of the Minimal Bounding Circle centre,
- MBCY: Y coordinate of the Minimal Bounding Circle centre,
- MBCRadius: Radius of the Minimal Bounding Circle,
- CountCorrect: This is a correction factor for unbiased counting of particles calculated as CountCorrect=XY/(X-Fx)(Y-Fy), where X and Y are the width and height of the ROI and Fx and Fy are the maximum projected dimensions of the object in the X and Y axes (this is Fx=1+ROIX2-ROIX1 and Fy=1+ROIY2-ROIY1 respectively). This correction factor should be applied with data generated with "exclude edge particles" checked. See [2] p 529.
From these parameters, other morphometrical ratios can be derived:
- AspRatio: Aspect Ratio = Feret/Breadth ,
- Circ: Circularity = 4*Pi*Area/Perimeter2, sometimes called Form Factor,
- Roundness: Roundness = 4*Area/(Pi*Feret2),
- AreaEquivD: Area Equivalent Diameter = sqrt((4/Pi)*Area),
- PerimEquivD: Perimeter Equivalent Diameter = Area/Pi,
- EquivEllipseAr: Equivalent Ellipse Area = (Pi*Feret*Breadth)/4, this is the area of an ellipse with the same long and short axes as the particle,
- Compactness: Compactness = sqrt((4/Pi)*Area)/Feret or alternatively ArEquivD/Feret,
- Solidity: Solidity = Area/Convex_Area,
- Concavity: Concavity = Convex_Area-Area,
- Convexity: Convexity = Convex_Hull/Perimeter,
- Shape: Shape = Perimeter2/Area,
- RFactor: RFactor = Convex_Hull /(Feret*Pi),
- ModRatio: Modification Ratio = (2*MinR)/Feret,
- Sphericity: Sphericity = MinR/MaxR,
- ArBBox: ArBBox = Feret*Breadth. This is the area of the Bounding Box along the Feret diameter, but it is not necessarily the minimal bounding box! (Search the net for "rotating calipers algorithm"),
- Rectang: Rectangularity = Area/ArBBox. This approaches 0 for cross-like objects, 0.5 for squares, pi/4=0.79 for circles and appraches 1 for long rectangles.
The formulas of the morphological ratios in OpenOffice (ParticlesPlus.sxc) and Excel (ParticlesPlus.xls) spreadsheet formats are included in the zip file.
By pasting the plugin results under the red headers, the derived parameters are calculated under the headers in blue. Copy the formulas of the derived parameters from the first row after the headers to the rows below as necessary.
Example macros that show some of the new parameters: DrawBoundingBox.txt, DrawFeret&Breadth.txt, DrawMinR&MaxR.txt.
The macro UnbiasedCounting.txt counts particles that touch the top and right image frames.
The macro UnbiasedParticleArea.txt estimates the corrected average particle area and number using the CountCorrect parameter.
New in Version 1.8a of Particles8_Plus
Fixed a bug in the Minimal Bounding Circle routine.
Added an example macro: DrawBoundingCircle.txt.
New in Version 1.9
When the image is a stack, the stack label is written to the Results window ("null" otherwise).
Changed some parameter names that caused problems when importing the data into SPSS.
DrawFeret&Breadth.txt was updated to use the new parameter names.
References
First released 6/Nov/2003.
Updated 6/Dec/2003.
Updated 16/Dec/2003.
Updated 22/Apr/2004.
Updated 29/Dec/2004.
Updated 21/Mar/2005.
Updated 30/Apr/2005.
Updated 22/May/2005.
Updated 12/Jul/2005.