Class AlgorithmMorphology2D

  • All Implemented Interfaces:
    java.awt.event.ActionListener, java.awt.event.WindowListener, java.lang.Runnable, java.util.EventListener

    public class AlgorithmMorphology2D
    extends AlgorithmBase
    Two-Dimensional mathmatical morphology class. Kernels of 3x3 (4 or 8 connected) and 5x5 (12 connected) are available or user defined kernels can be supplied.

    Methods include:

    • Background Distance Map
    • close
    • Delete objects
    • dilate
    • Distance Map
    • erode
    • fill holes
    • find edges
    • Identify objects
    • Morphological gradient
    • open
    • Particle Analysis
    • Skeletonize with pruning option
    • ultimate erode
    • Border clearing
    • Geodesic dilation
    • Geodesic erosion
    • Morphological reconstruction by dilation
    • Morphological reconstruction by erosion
    • Opening by reconstruction
    • Closing by reconstruction
    Version:
    1.0 March 15, 1998
    Author:
    Matthew J. McAuliffe, Ph.D.
    • Field Detail

      • PARTICLE_ANALYSIS

        public static final int PARTICLE_ANALYSIS
        DOCUMENT ME!
        See Also:
        Constant Field Values
      • PARTICLE_ANALYSIS_NEW

        public static final int PARTICLE_ANALYSIS_NEW
        DOCUMENT ME!
        See Also:
        Constant Field Values
      • DISTANCE_MAP_FOR_SHAPE_INTERPOLATION

        public static final int DISTANCE_MAP_FOR_SHAPE_INTERPOLATION
        DOCUMENT ME!
        See Also:
        Constant Field Values
      • MORPHOLOGICAL_RECONSTRUCTION_BY_DILATION

        public static final int MORPHOLOGICAL_RECONSTRUCTION_BY_DILATION
        See Also:
        Constant Field Values
      • MORPHOLOGICAL_RECONSTRUCTION_BY_EROSION

        public static final int MORPHOLOGICAL_RECONSTRUCTION_BY_EROSION
        See Also:
        Constant Field Values
      • OPENING_BY_RECONSTRUCTION

        public static final int OPENING_BY_RECONSTRUCTION
        See Also:
        Constant Field Values
      • CLOSING_BY_RECONSTRUCTION

        public static final int CLOSING_BY_RECONSTRUCTION
        See Also:
        Constant Field Values
      • algorithm

        private int algorithm
        algorithm type (i.e. erode, dilate)
      • circleDiameter

        private float circleDiameter
        kernel diameter.
      • circleDiameterErode

        private float circleDiameterErode
        Erosion kernel diameter.
      • distanceMap

        private float[] distanceMap
        DOCUMENT ME!
      • edgingType

        private int edgingType
        Edge type.
      • entireImage

        private boolean entireImage
        if true, indicates that the VOIs should NOT be used and that entire image should be processed.
      • imgBuffer

        private short[] imgBuffer
        imgBuffer that hold pixel value for the 2D slice.
      • imgBuffer2

        private short[] imgBuffer2
      • maskBuffer

        private short[] maskBuffer
      • iterationsD

        private int iterationsD
        Dilation iteration times.
      • iterationsE

        private int iterationsE
        Erosion iteration times.
      • iterationsErodeDilate

        private int iterationsErodeDilate
        DOCUMENT ME!
      • kDim

        private int kDim
        Kernel dimension.
      • kernel

        private java.util.BitSet kernel
        Kernel for both erosion and dilation.
      • kernelType

        private int kernelType
        kernel size (i.e. connectedness)
      • kernelTypeErode

        private int kernelTypeErode
        Erosion kernel type.
      • min

        private int min
        maximum, minimum size of objects.
      • max

        private int max
        maximum, minimum size of objects.
      • numPruningPixels

        private int numPruningPixels
        Number pixels to prune.
      • pixDist

        private float pixDist
        DOCUMENT ME!
      • processBuffer

        private short[] processBuffer
        intermediate processing buffer, same size with imgBuffer.
      • pruneSeeds

        private java.util.Vector<java.lang.Integer> pruneSeeds
        Vector that hold the prune seeding pixels.
      • showFrame

        private boolean showFrame
        Flag to show frame during each algorithm method call.
      • ultErodeObjects

        private java.awt.Point[] ultErodeObjects
        DOCUMENT ME!
      • maskImage

        private ModelImage maskImage
        Used in geodesic dilation, geodesic erosion, morphological reconstruction by dilation, and morphological reconstruction by erosion Not used in opening by reconstruction and closing by reconstruction
      • geodesicSize

        private int geodesicSize
        Size of geodesic erosions and dilations
    • Constructor Detail

      • AlgorithmMorphology2D

        public AlgorithmMorphology2D​(ModelImage srcImg,
                                     int kernelType,
                                     float circleDiameter,
                                     int method,
                                     int iterD,
                                     int iterE,
                                     int pruningPix,
                                     int edType,
                                     boolean entireImage)
        Creates a new AlgorithmMorphology2D object.
        Parameters:
        srcImg - source image model
        kernelType - kernel size (i.e. connectedness)
        circleDiameter - only valid if kernelType == SIZED_CIRCLE and represents the width of a circle in the resolution of the image
        method - setup the algorithm method (i.e. erode, dilate)
        iterD - number of times to dilate
        iterE - number of times to erode
        pruningPix - the number of pixels to prune
        edType - the type of edging to perform (inner or outer)
        entireImage - if true, indicates that the VOIs should NOT be used and that entire image should be processed
      • AlgorithmMorphology2D

        public AlgorithmMorphology2D​(ModelImage srcImg,
                                     int kernelType,
                                     float circleDiameter,
                                     int kernelTypeErode,
                                     float circleDiameterErode,
                                     int method,
                                     int iterErodeDilate,
                                     int iterE,
                                     int pruningPix,
                                     int edType,
                                     boolean entireImage,
                                     boolean _showFrame)
        Creates a new AlgorithmMorphology2D object.
        Parameters:
        srcImg - source image model
        kernelType - dilation kernel size (i.e. connectedness)
        circleDiameter - dilation only valid if kernelType == SIZED_CIRCLE and represents the width of a circle in the resolution of the image
        kernelTypeErode - kernel size (i.e. connectedness) of erosion
        circleDiameterErode - Erosion only valid if kernelType == SIZED_CIRCLE and represents the width of a circle in the resolution of the image
        method - setup the algorithm method (i.e. erode, dilate)
        iterErodeDilate - number of times to erode and dilate
        iterE - number of times to erode
        pruningPix - the number of pixels to prune
        edType - the type of edging to perform (inner or outer)
        entireImage - if true, indicates that the VOIs should NOT be used and that entire image should be processed
        _showFrame - if true, indicates that show image frame after each algorithm be processed
      • AlgorithmMorphology2D

        public AlgorithmMorphology2D​(ModelImage srcImg,
                                     ModelImage maskImage,
                                     int kernelType,
                                     float circleDiameter,
                                     int method,
                                     int geodesicSize,
                                     int iterD,
                                     int iterE,
                                     boolean entireImage,
                                     boolean _showFrame)
        Creates a new AlgorithmMorphology2D object.
        Parameters:
        srcImg - source image model
        kernelType - dilation kernel size (i.e. connectedness)
        circleDiameter - dilation only valid if kernelType == SIZED_CIRCLE and represents the width of a circle in the resolution of the image
        method - setup the algorithm method (i.e. erode, dilate)
        geodesicSize -
        iterD - number of times to dilate
        iterE - number of times to erode
        entireImage - if true, indicates that the VOIs should NOT be used and that entire image should be processed
        _showFrame - if true, indicates that show image frame after each algorithm be processed
      • AlgorithmMorphology2D

        public AlgorithmMorphology2D()
    • Method Detail

      • runGeodesicDilationTest

        public void runGeodesicDilationTest()
      • runGeodesicErosionTest

        public void runGeodesicErosionTest()
      • runMorphologicalReconstructionByDilationTest

        public void runMorphologicalReconstructionByDilationTest()
      • genContour

        public static final java.awt.Polygon genContour​(java.util.BitSet image,
                                                        int xDim,
                                                        int yDim,
                                                        java.awt.Point startPt)
        Static method that generates a boundary of a binary object. (aka. turtle algorithm: if 1 turn left, step if 0 turn right, step)
        Parameters:
        image - input image where binary object is located.
        xDim - the x dimension of the image
        yDim - the y dimension of the image
        startPt - starting point
        Returns:
        returns boundary of the object as Polygon
      • genContour

        public static final java.awt.Polygon genContour​(ModelImage image,
                                                        int xDim,
                                                        int yDim,
                                                        java.awt.Point startPt)
        Static method that generates a boundary of a binary object. (aka. turtle algorithm: if 1 turn left, step if 0 turn right, step)
        Parameters:
        image - input image (Boolean) where binary object is located.
        xDim - the x dimension of the image
        yDim - the y dimension of the image
        startPt - starting point
        Returns:
        returns boundary of the object as Polygon
      • genContour

        public static final java.awt.Polygon genContour​(int xDimE,
                                                        int yDimE,
                                                        java.awt.Point startPt,
                                                        java.util.BitSet maskA)
        Static method that generates a boundary of a general object. (aka. turtle algorithm: if 1 turn left, step if 0 turn right, step)
        Parameters:
        xDimE - the x dimension of the image
        yDimE - the y dimension of the image
        startPt - starting point
        maskA - mask of the object
        Returns:
        returns boundary of the object as Polygon
      • genContour

        public static final java.awt.Polygon genContour​(short[] image,
                                                        int xDim,
                                                        int yDim,
                                                        java.awt.Point startPt,
                                                        short objectValue)
        Static method that generates a boundary of a general object. (aka. turtle algorithm: if 1 turn left, step if 0 turn right, step)
        Parameters:
        image - input image where <> object is located.
        xDim - the x dimension of the image
        yDim - the y dimension of the image
        startPt - starting point
        objectValue - value of the object
        Returns:
        returns boundary of the object as Polygon
      • finalize

        public void finalize()
        Prepares this class for destruction.
        Overrides:
        finalize in class AlgorithmBase
      • identifyObjects

        public void identifyObjects​(boolean returnFlag)
        Labels each object in an image with a different integer value.
        Parameters:
        returnFlag - if true then this operation is a step in the morph process (i.e. close)
      • prune

        public void prune​(int iter,
                          boolean returnFlag)
        This method is to be applied on skeletonized images: it removes all branches which are iter or less pixels in length.
        Parameters:
        iter - the threshold number of pixels for the maximum length of a removed branch to be
        returnFlag - if true then this operation is a step in the morph process (i.e. close)
      • pruneAuto

        public void pruneAuto​(boolean returnFlag)
        This method is to be applied on skeletonized images: it skeletonized each branches into a point.
        Parameters:
        returnFlag - if true then this operation is a step in the morph process (i.e. close)
      • setAlgorithm

        public void setAlgorithm​(int method)
        Sets the algorithm type (i.e. erode, dilate)
        Parameters:
        method - algorithm type
      • setImage

        public void setImage​(ModelImage img)
        Sets the algorithm to new image of boolean type.
        Parameters:
        img - image model of boolean type
      • setIterations

        public void setIterations​(int itersD,
                                  int itersE)
        Sets number of iterations for closing or opening.
        Parameters:
        itersD - number of dilations
        itersE - number of erosions
      • setKernel

        public void setKernel​(java.util.BitSet kernel,
                              int dim)
        Sets user defined square kernels.
        Parameters:
        kernel - user defined kernel
        dim - length of one dimension (kernel should be square)
      • setMinMax

        public void setMinMax​(int _min,
                              int _max)
        Sets bounds of object size used to delete objects.
        Parameters:
        _min - minimum size of objects
        _max - maximum size of objects
      • setPixDistance

        public void setPixDistance​(float dist)
        Used in the ultimate erode function to remove all eroded points less than the distance specified in pixel units.
        Parameters:
        dist - distance in pixels. Default = 0 (i.e. do not remove any points)
      • setShowFrame

        public void setShowFrame​(boolean showFrame)
        DOCUMENT ME!
        Parameters:
        showFrame - DOCUMENT ME!
      • showKernel

        public void showKernel()
        display kernel as 1 or 0 in the debug window.
      • skeletonize

        public void skeletonize​(int pruningPixels,
                                boolean returnFlag)
        Skeletonizes the image by using a lookup table to repeatedly remove pixels from the edges of objects in a binary image, reducing them to single pixel wide skeletons. Based on a thinning algorithm by Zhang and Suen. There is an entry in the table for each of the 256 possible 3x3 neighborhood configurations. An entry of '1' signifies to delete the indicated pixel on the first pass, '2' means to delete the indicated pixel on the second pass, and '3' indicates to delete the pixel on either pass.
        Parameters:
        pruningPixels - the number of pixels to prune after skeletonizing. (should be 0 if no pruning is to be done)
        returnFlag - if true then this operation is a step in the morph process (i.e. close)
      • distanceMapForShapeInterpolation

        private void distanceMapForShapeInterpolation​(boolean returnFlag)
      • backgroundDistanceMap

        private void backgroundDistanceMap​(boolean returnFlag)
        Euclidian distance map of the background.
        Parameters:
        returnFlag - if true then this operation is a step in the morph process
      • deleteObjects

        private void deleteObjects​(int min,
                                   int max,
                                   boolean returnFlag)
        Deletes objects larger than maximum size indicated and objects smaller than the indicated minimum size.
        Parameters:
        min - delete all objects smaller than the minimum value (pixels)
        max - delete all objects greater than the maximum value (pixels)
        returnFlag - if true then this operation is a step in the morph process (i.e. close)
      • borderClearing

        private void borderClearing​(boolean returnFlag)
        Removes objects that touch (i.e., are connected to) the border
        Parameters:
        returnFlag -
      • dilate

        private void dilate​(boolean returnFlag,
                            int iters)
        Dilates a boolean, unsigned byte or unsigned short image using the indicated kernel and the indicated number of executions.
        Parameters:
        returnFlag - if true then this operation is a step in the morph process (i.e. close)
        iters - number of dilations
      • geodesicDilation

        private void geodesicDilation​(boolean returnFlag,
                                      int geodesicSize)
        geodesic dilation of a boolean, unsigned byte or unsigned short image using the indicated kernel and the indicated number of executions.
        Parameters:
        returnFlag - if true then this operation is a step in the morph process (i.e. close)
        geodesicSize -
      • geodesicErosion

        private void geodesicErosion​(boolean returnFlag,
                                     int geodesicSize)
        geodesic erosion of a boolean, unsigned byte or unsigned short image using the indicated kernel and the indicated number of executions.
        Parameters:
        returnFlag - if true then this operation is a step in the morph process (i.e. close)
        geodesicSize -
      • distanceMap

        private void distanceMap​(boolean returnFlag)
        Euclidian distance map.
        Parameters:
        returnFlag - if true then this operation is a step in the morph process
      • erode

        private void erode​(boolean returnFlag,
                           int iters)
        Erodes a boolean or unsigned byte or unsigned short image using the indicated kernel and the indicated number of executions.
        Parameters:
        returnFlag - if true then this operation is a step in the morph process (i.e. open)
        iters - number of erosion iterations.
      • fillHoles

        private void fillHoles​(boolean returnFlag)
        Fill the holes inside the cell region blocks.
        Parameters:
        returnFlag - if true then this operation is a step in the morph process (i.e. close)
      • fillHolesRegion

        private void fillHolesRegion​(int stIndex,
                                     short floodValue,
                                     short objValue)
        2D flood fill that fill the holes insize the cell region block.
        Parameters:
        stIndex - the starting index of the seed point
        floodValue - the value to flood the region with
        objValue - object ID value that idenditifies the flood region.
      • findEdges

        private void findEdges​(int edgingType,
                               boolean returnFlag)
        Finds the edges of the objects in an image.
        Parameters:
        edgingType - DOCUMENT ME!
        returnFlag - if true then this operation is a step in the morph process (i.e. close)
      • floodFill

        private int floodFill​(short[] idBuffer,
                              int stIndex,
                              short floodValue,
                              short objValue)
        2D flood fill that forms a short mask.
        Parameters:
        idBuffer - buffer used to hold flood value
        stIndex - the starting index of the seed point
        floodValue - the value to flood the region with
        objValue - object ID value that idenditifies the flood region.
        Returns:
        DOCUMENT ME!
      • isEndpoint

        private boolean isEndpoint​(int pix,
                                   short[] tmpBuffer)
        This method returns whether or not pix is the index of an endpoint in tmpBuffer (it is assumed that location pix is not the intensity of the background in tmpBuffer).
        Parameters:
        pix - the index of a non-zero pixel in tmpBuffer
        tmpBuffer - the image
        Returns:
        DOCUMENT ME!
      • isNeighbor

        private boolean isNeighbor​(int index,
                                   int pixNeighbor)
        Check the neighbor pix.
        Parameters:
        index - pixel in center
        pixNeighbor - neighbor pix
        Returns:
        boolean is connected neighbor or not
      • isSinglePoint

        private boolean isSinglePoint​(int index,
                                      short[] tmpBuffer)
        Check if the given pix is a single point or not.
        Parameters:
        index - index
        tmpBuffer - short[]
        Returns:
        boolean
      • makeCircularKernel

        private void makeCircularKernel​(float circDiameter)
        Generates a cicular kernel of a specific diameter.
        Parameters:
        circDiameter - represents the width of a circle in the resolution of the image
      • makeKernel

        private void makeKernel​(int kernelType)
        Generates a kernel of the indicated type.
        Parameters:
        kernelType - type of kernel to be generated.
      • onePixel

        private boolean onePixel​(short[] buffer,
                                 int index,
                                 int xDim)
        Used by ultimate erode to simplify code a little.
        Parameters:
        buffer - reference to the mask data
        index - pointer into the array
        xDim - the width of image
        Returns:
        DOCUMENT ME!
      • particleAnalysis

        private void particleAnalysis()
        DOCUMENT ME!
      • particleAnalysisNew

        private void particleAnalysisNew​(boolean returnFlag)
        particleAnalysis = Ult erode & (bg dist map AND orig image) => watershed(ultErodePts, ANDED Bg Dist) => IDobjects.
        Parameters:
        returnFlag - DOCUMENT ME!
      • thin

        private int thin​(int pass,
                         int[] table)
        This a thinning algorithm used to do half of one layer of thinning (which layer is dictated by whether pass is even or odd).
        Parameters:
        pass - the number pass this execution is on the image
        table - the table to lookup whether to delete the pixel or let it stay.
        Returns:
        DOCUMENT ME!
      • ultimateErode

        private void ultimateErode​(boolean returnFlag)
        Ultimate erodes objects down to a single pixel (almost). Sometimes depending on the shape of the object (bar bell) more than a single point. Most likely a good situation.
        Parameters:
        returnFlag - if true then this operation is a step in the morph process