Class AlgorithmMedian

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

    public class AlgorithmMedian
    extends AlgorithmBase
    Algorithm to apply a median filter to an image, placing it in a new ModelImage, or returning the changed picture to the same image. Algorithm takes values in the neighborhood that correspond to the "on" values in the kernel (mask) and places them into a sorted list. If the variance (square of standard Deviation) isn't 0 and the pixel's value is outside the bounds given by the given fraction of the standard deviation, then the median of the list is computed and placed into that element of the image. If the variance is 0, the median of the list is always computed and placed into that element of the image. Color images can use either component filtering or vector filtering. In vector filtering the red, green, and blue are all fitered at once; that is, the new pixel value will be selected from red, green, and blue from one pixel in the neighborhood. With vector filtering the variance is always set to 0, and the vector median is always used.

    If any element in the kernel which does not have a corresponding element in the image, then the pixel is unfiltered. This results in a border of unfiltered pixels in the image 1/2 of the kernel-size large.

    When adaptiveSize is true, the Adaptive median filter discussed in Digital Image Processing Second Edition by Rafael C. Gonzalez and Richard E. Woods in Chapter 5.3 pages 241-243 is implemented. It has 3 main purposes: 1. remove salt and pepper (impulse) noise 2. provide smoothening of other noise that may not be impulsive 3. reduce distortion such as excessive thinning or thickening of object bounderies. Depending on certain conditions, the kernel size can incresase during the filtering operation. For Adaptive Truncated Vector Median Filter for color images see "Adaptive Truncated Vector Median Filter" by Bogdan Smolka, 2011 IEEE International Conference on Computer Science and Automation Engineering, June, 2011, pp. 261-266. The truncated median filter estimates the mode. See Feature Extraction and Image Processing for Computer Vision Third Edition by Mark S. Nixon and Alberto S. Aguado, Section 3.5.2 Model filter, pp. 112 - 114. The symmetrical triangular fuzzy filter with a truncated median is used to suppress the type of noise found in historical photographs. This is characterized by fine fractures or threads in the image and by dirt that leads to a grainy appearance. See "Suppression of Noise in Historical Photographs Using a Fuzzy Truncated-Median Filter" by Michael Wirth and Bruce Bobier, Image Analysis and Recognition, Lecture Notes in Computer Science, Volume 4633, 2007, pp. 1206-1216.
    Version:
    1.0; 17 February 2000
    Author:
    David Parsons (parsonsd), Matthew J. McAuliffe, Ph.D., Paul F. Hemler, Ph.D.
    • Field Detail

      • SQUARE_KERNEL

        public static final int SQUARE_KERNEL
        square kernel (2D only).
        See Also:
        Constant Field Values
      • X_KERNEL

        public static final int X_KERNEL
        X-shaped kernel, from 1 corner to opposite corner.
        See Also:
        Constant Field Values
      • VECTOR_MAGNITUDE_FILTER

        public static final int VECTOR_MAGNITUDE_FILTER
        See Also:
        Constant Field Values
      • VECTOR_DIRECTION_FILTER

        public static final int VECTOR_DIRECTION_FILTER
        See Also:
        Constant Field Values
      • ADAPTIVE_TRUNCATED_VECTOR_MEDIAN_FILTER

        public static final int ADAPTIVE_TRUNCATED_VECTOR_MEDIAN_FILTER
        See Also:
        Constant Field Values
      • SYMMETRICAL_TRIANGULAR_FUZZY_TRUNCATED_MEDIAN

        public static final int SYMMETRICAL_TRIANGULAR_FUZZY_TRUNCATED_MEDIAN
        See Also:
        Constant Field Values
      • bdrBufferDepth

        int bdrBufferDepth
        Border buffer number of slices.
      • bdrBufferHeight

        int bdrBufferHeight
        Border buffer number of rows.
      • bdrBufferWidth

        int bdrBufferWidth
        Border buffer number of columns.
      • srcBufferDepth

        int srcBufferDepth
        Source buffer number of slices.
      • srcBufferHeight

        int srcBufferHeight
        Source buffer number of rows.
      • srcBufferWidth

        int srcBufferWidth
        Source buffer number of columns.
      • bChannel

        private boolean bChannel
        the blue channel.
      • currentSlice

        private int currentSlice
        Used for 3D loop control.
      • entireImage

        private boolean entireImage
        true means apply to entire image, false only region.
      • gChannel

        private boolean gChannel
        the green channel.
      • halfK

        private int[] halfK
        The kernel radius.
      • isColorImage

        private boolean isColorImage
        indicates the image is a color image.
      • iterations

        private int iterations
        number of times to filter the image.
      • kernel

        private byte[][] kernel
        mask to determine the region of pixels used in a median filter.
      • kernelCenter

        private int[] kernelCenter
        The index of the kernel center.
      • kernelMask

        private float[][] kernelMask
        The kernel.
      • kernelVectorMask

        private float[][] kernelVectorMask
      • kernelShape

        private int kernelShape
        user-selectable shape of the region for neighbor-selection.
      • minimumSize

        private int minimumSize
        Smallest kernel size used in adaptive filtering
      • kernelSize

        private int[] kernelSize
        dimension of the kernel (ie., 5 = 5x5, 7 = 7x7, 9 = 9x9, etc.).
      • filterType

        private int filterType
      • delta

        private float delta
        Used in adaptive truncated vector median filter used on color images If the changes in the RGB components are all less than a threshold delta then the color image pixels are not changed, otherwise they are replaced by the ATVMF.
      • maximumSize

        private int maximumSize
        When adaptiveSize is true, the maximum size the kernel mask can be increased to.
      • kernelNumber

        private int kernelNumber
        Number of kernels used, more than 1 if adaptive filtering is used
      • mask

        private java.util.BitSet mask
        contains VOI.
      • maskCenter

        private int[] maskCenter
        The kernel.
      • numberOfSlices

        private int numberOfSlices
        Used for 3D loop control.
      • rChannel

        private boolean rChannel
        // if T, filter the red channel.
      • sliceFiltering

        private boolean sliceFiltering
        do all filtering slice-by-slice, rather than as a volume.
      • stdDevLimit

        private float stdDevLimit
        only filter pixels with a value further than stdDevLimit from the mean.
      • valuesPerPixel

        private int valuesPerPixel
        number of elements in a pixel. Monochrome = 1, Color = 4. (a, R, G, B)
      • colorFilterType

        private int colorFilterType
        Either COMPONENT_FILTER, VECTOR_MAGNITUDE_FILTER, VECTOR_DIRECTION_FILTER, or ADAPTIVE_TRUNCATED_VECTOR_MEDIAN_FILTER For a vector filter the new red, green, and blue will all come from the same pixel.
    • Constructor Detail

      • AlgorithmMedian

        public AlgorithmMedian​(ModelImage srcImg,
                               int iters,
                               int kSize,
                               int kShape,
                               float stdDev,
                               int filterType,
                               int maximumSize,
                               boolean maskFlag)
        Constructor for 2D images in which changes are returned to the source image.
        Parameters:
        srcImg - Source image model.
        iters - Number of interations of the median filter.
        kSize - Kernel size: dimension of the kernel (ie., 5 = 5x5, 7 = 7x7, 9 = 9x9, etc.).
        kShape - Kernel shape: element neighbors to include when finding the median.
        stdDev - Inner-bounds by which to process pixels (pixel values outside this bound will be median filtered).
        filterType - STANDARD, ADAPTIVE_SIZE, or TRUNCATED_MEDIAN
        maximumSize - If adaptiveSize is true, the maximum size the kernel mask can be increased to.
        maskFlag - Flag that indicates that the median filtering will be performed for the whole image if equal to true.
      • AlgorithmMedian

        public AlgorithmMedian​(ModelImage destImg,
                               ModelImage srcImg,
                               int iters,
                               int kSize,
                               int kShape,
                               float stdDev,
                               int filterType,
                               int maximumSize,
                               boolean maskFlag)
        Constructor for 2D images in which changes are placed in a predetermined destination image.
        Parameters:
        destImg - Image model where result image is stored.
        srcImg - Source image model.
        iters - Number of interations of the median filter.
        kSize - Kernel size: dimension of the kernel (ie., 5 = 5x5, 7 = 7x7, 9 = 9x9, etc.).
        kShape - Kernel shape: element neighbors to include when finding the median.
        stdDev - Inner-bounds by which to process pixels (pixel values outside this bound will be median filtered).
        filterType - STANDARD, ADAPTIVE_SIZE, or TRUNCATED_MEDIAN.
        maximumSize - If adaptiveSize is true, the maximum size the kernel mask can be increased to.
        maskFlag - Flag that indicates that the median filtering will be performed for the whole image if equal to true.
      • AlgorithmMedian

        public AlgorithmMedian​(ModelImage srcImg,
                               int iters,
                               int kSize,
                               int kShape,
                               float stdDev,
                               int filterType,
                               int maximumSize,
                               boolean sliceBySlice,
                               boolean maskFlag)
        Constructor for 3D images in which changes are returned to the source image.
        Parameters:
        srcImg - Source image model.
        iters - Number of interations of the median filter.
        kSize - Kernel size: dimension of the kernel (ie., 5 = 5x5, 7 = 7x7, 9 = 9x9, etc.).
        kShape - Kernel shape: element neighbors to include when finding the median.
        stdDev - Inner-bounds by which to process pixels (pixel values outside this bound will be median filtered).
        filterType - STANDARD, ADAPTIVE_SIZE, or TRUNCATED_MEDIAN
        maximumSize - If adaptiveSize is true, the maximum size the kernel mask can be increased to.
        sliceBySlice - Each slice in a volume image is to be filtered separately (when true), else the volume will use a kernel with 3 dimensions.
        maskFlag - Flag that indicates that the median filtering will be performed for the whole image if equal to true.
      • AlgorithmMedian

        public AlgorithmMedian​(ModelImage destImg,
                               ModelImage srcImg,
                               int iters,
                               int kSize,
                               int kShape,
                               float stdDev,
                               int filterType,
                               int maximumSize,
                               boolean sliceBySlice,
                               boolean maskFlag)
        Constructor for 3D images in which changes are placed in a predetermined destination image.
        Parameters:
        destImg - Image model where result image is stored.
        srcImg - Source image model.
        iters - Number of interations of the median filter.
        kSize - Kernel size: dimension of the kernel (ie., 5 = 5x5, 7 = 7x7, 9 = 9x9, etc.).
        kShape - Kernel shape: element neighbors to include when finding the median.
        stdDev - Inner-bounds by which to process pixels (pixel values outside this bound will be median filtered).
        filterType - STANDARD, ADAPTIVE_SIZE, or TRUNCATED_MEDIAN
        maximumSize - If adaptiveSize is true, the maximum size the kernel mask can be increased to.
        sliceBySlice - Each slice in a volume image is filtered separately (when true), else the volume will use a kernel with 3 dimensions.
        maskFlag - Flag that indicates that the median filtering will be performed for the whole image if equal to true.
    • Method Detail

      • finalize

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

        public void setRGBChannelFilter​(int colorFilterType,
                                        boolean r,
                                        boolean g,
                                        boolean b,
                                        float delta)
        RGB images are median filtered by 'channel.' That is, each color, red, blue and green, is filtered independently of the other two colors. This median filter permits selectively filtering any combination of the three channels instead of simply filtering all three.
        Parameters:
        colorFilterType - Either COMPONENT_FILTER, VECTOR_MAGNITUDE_FILTER, or VECTOR_DIRECTION_FILTER
        r - Filter red channel.
        g - Filter green channel.
        b - Filter blue channel.
        delta -
      • calcInPlace2D

        private void calcInPlace2D()
        Median filters the source image. Replaces the original image with the filtered image.
      • calcInPlace3D

        private void calcInPlace3D()
        Median filters the source image and replaces the source image with the median filtered image.
      • calcInPlaceBorder2D

        private void calcInPlaceBorder2D()
        Replaces the original image with the Median filtered image.

        Make a copy of the srcImage in a buffer with borders for the kernel. Copy the closest rows and columns data into the added borders.

      • calcInPlaceBorder3D

        private void calcInPlaceBorder3D()
        Mode filters the source image and replaces the source image with the mode filtered image.
      • calcStoreInDest2D

        private void calcStoreInDest2D()
        This function produces a new image that has been median filtered and places filtered image in the destination image.
      • calcStoreInDest3D

        private void calcStoreInDest3D()
        This function produces a new volume image that has been median filtered. Image can be filtered by filtering each slice individually, or by filtering using a kernel-volume.
      • calcStoreInDestBorder2D

        private void calcStoreInDestBorder2D()
        This method produces a new image that has been mode filtered and places filtered image in the destination image.
      • calcStoreInDestBorder3D

        private void calcStoreInDestBorder3D()
        Median filters the source image and replaces the source image with the mode filtered image.
      • copy2DSrcBufferToBdrBuffer

        private void copy2DSrcBufferToBdrBuffer​(float[] srcBuffer,
                                                float[] bdrBuffer,
                                                int srcBufferOffsetIndex,
                                                int bdrBufferOffsetIndex)
        Method copies the data in srcBuffer centered in the border buffer That is, the src image is completely surrounded by a border. The values in the border locations are copies of nearby srcBuffer values.
        Parameters:
        srcBuffer - float [] the source image buffer
        bdrBuffer - float [] the border image buffer
        srcBufferOffsetIndex - offset into srcBuffer
        bdrBufferOffsetIndex - offset into bdrBuffer
      • copy3DSrcBufferToBdrBuffer

        private void copy3DSrcBufferToBdrBuffer​(float[] srcBuffer,
                                                float[] bdrBuffer)
        Method copies the data in srcBuffer so it is centered in the border buffer That is, the src image is completely surrounded by a border. The values in the border locations are copies of nearby srcBuffer values.
        Parameters:
        srcBuffer - float [] the source image buffer
        bdrBuffer - float [] the border image buffer
      • getBorderBufferNeighborList

        private float[] getBorderBufferNeighborList​(int kn,
                                                    int i,
                                                    float[] data,
                                                    boolean is2D)
        Compiles a list of the values neighboring the desired pixel, that are defined in the kernel.
        Parameters:
        kn - The kernel number; always 0 if adaptiveSize is false
        i - The central pixel to find neighbors for.
        data - float [] Image data
        is2D - True indicates that the neighbors are found along a 2D slice (or 2D image) instead of neighbors in a 3D volume.
        Returns:
        The neighboring pixel list corresponds to the kernel chosen.
      • getNeighborList

        private float[] getNeighborList​(int kn,
                                        int i,
                                        float[] data,
                                        boolean is2D)
        Compiles a list of the values neighboring the desired pixel, that are defined in the kernel.

        Color images are processed differently from the monochrome images because although colour images use the same size kernel as mono images, it fills the kernel with brightness levels that are spread out in the data set. The Neighbor list still reports the monochromatic brightness values. That is, for a color image: the neighbors of the central pixel with the same color are returned in the neighbor list's kernel.

        Parameters:
        kn - The kernel number; always 0 if adaptiveSize is false.
        i - The central pixel to find neighbors for.
        data - float [] Image data
        is2D - True indicates that the neighbors are found along a 2D slice (or 2D image) instead of neighbors in a 3D volume.
        Returns:
        The neighboring pixel list corresponds to the kernel chosen.
      • getVectorNeighborList

        private float[] getVectorNeighborList​(int kn,
                                              int i,
                                              float[] data,
                                              boolean is2D)
        Compiles a list of the values neighboring the desired pixel, that are defined in the kernel.

        Only used with color images in vector filtering. Returned float array has red, green, blue, red, green, blue, etc.

        Parameters:
        kn - The kernel number; always zero if adaptiveSize is false.
        i - The central pixel to find neighbors for.
        data - float [] Image data
        is2D - True indicates that the neighbors are found along a 2D slice (or 2D image) instead of neighbors in a 3D volume.
        Returns:
        The neighboring pixel list corresponds to the kernel chosen.
      • makeKernel

        private void makeKernel()
        Forms kernel.
      • makeKernelMask

        private void makeKernelMask()
        Makes the kernel mask. The kernel mask is the list of values pulled from the image which will be used to find the median of the central pixel. Its length is (number of pixels to be used to determine median).

        Thus the kernel center (decided here), has the value of the location of the central pixel shown in the window. The value of the kernel center is the number of pixels picked up to median sort.

        Note that for symmetric masks always have kernelCenter = count/2 and maskCenter = count/2.

      • mean

        private float mean​(float[] list)
        Finds the mean value (average) in the list.
        Parameters:
        list - float [] List of numbers
        Returns:
        float The mean.
      • median

        private float median​(float[] list)
        Finds the median value of the list.
        Parameters:
        list - float [] List of numbers
        Returns:
        The median.
      • vectorMagnitudeMedian

        private int vectorMagnitudeMedian​(float[] list)
        Only used with color images in vector filtering. Returns index of pixel whose sum of squares difference from the other pixels in the list is smallest.
        Parameters:
        list -
        Returns:
      • vectorDirectionMedian

        private int vectorDirectionMedian​(float[] list)
        Only used with color images in vector filtering. Returns index of pixel whose sum of angles difference from the other pixels in the list is smallest.
        Parameters:
        list -
        Returns:
      • adaptiveTruncatedVectorMedian

        private float[] adaptiveTruncatedVectorMedian​(float r,
                                                      float g,
                                                      float b,
                                                      float[] list)
        Only used with color images in vector filtering. Returns red, green, blue in float array for adaptive truncated vector median filtering
        Parameters:
        r -
        g -
        b -
        list -
        Returns:
      • setCopyColorText

        private void setCopyColorText​(java.lang.String colorText)
        If the progress bar is visible, sets the text to:
        Copying all color values ...
        Parameters:
        colorText - The color to use. Eg., "red" or "blue".
      • setKernel

        private void setKernel()
        Fill in the mask for which pixels are used in filtering.
      • sliceFilter

        private void sliceFilter​(float[] srcBuffer,
                                 float[] destBuffer,
                                 int bufferStartingPoint,
                                 java.lang.String msgString)
        Allows a single slice to be filtered. Note that a progressBar must be created first.
        Parameters:
        srcBuffer - float [] Source buffer.
        destBuffer - float[] Destination Buffer.
        bufferStartingPoint - Starting point for the buffer.
        msgString - A text message that can be displayed as a message text in the progressBar.
      • sliceVectorMagnitudeFilter

        private void sliceVectorMagnitudeFilter​(float[] srcBuffer,
                                                float[] destBuffer,
                                                int bufferStartingPoint,
                                                java.lang.String msgString)
        Allows a single slice to be filtered. Note that a progressBar must be created first. Only used on color with vector magnitude filtering. Red, green, and blue are all filtered together; that is, the new red, green, and blue will all come from the same pixel.
        Parameters:
        srcBuffer - float [] Source buffer.
        destBuffer - float[] Destination Buffer.
        bufferStartingPoint - Starting point for the buffer.
        msgString - A text message that can be displayed as a message text in the progressBar.
      • sliceVectorDirectionFilter

        private void sliceVectorDirectionFilter​(float[] srcBuffer,
                                                float[] destBuffer,
                                                int bufferStartingPoint,
                                                java.lang.String msgString)
        Allows a single slice to be filtered. Note that a progressBar must be created first. Only used on color with vector direction filtering. Red, green, and blue are all filtered together; that is, the new red, green, and blue will all come from the same pixel.
        Parameters:
        srcBuffer - float [] Source buffer.
        destBuffer - float[] Destination Buffer.
        bufferStartingPoint - Starting point for the buffer.
        msgString - A text message that can be displayed as a message text in the progressBar.
      • sliceAdaptiveTruncatedVectorMedianFilter

        private void sliceAdaptiveTruncatedVectorMedianFilter​(float[] srcBuffer,
                                                              float[] destBuffer,
                                                              int bufferStartingPoint,
                                                              java.lang.String msgString)
        Allows a single slice to be filtered. Note that a progressBar must be created first. Only used on color with adaptive truncated vector median filtering. Red, green, and blue are all filtered together; that is, the new red, green, and blue will all come from the same pixel.
        Parameters:
        srcBuffer - float [] Source buffer.
        destBuffer - float[] Destination Buffer.
        bufferStartingPoint - Starting point for the buffer.
        msgString - A text message that can be displayed as a message text in the progressBar.
      • sliceFilterBorder

        private void sliceFilterBorder​(float[] srcBdrBuffer,
                                       float[] destBuffer,
                                       int srcBufferStartingPoint,
                                       int destBufferStartingPoint,
                                       boolean showProgress)
        Allows a single slice to be filtered. Note that a progressBar must be created first.
        Parameters:
        srcBdrBuffer - float[] Source buffer.
        destBuffer - float[] Destination Buffer.
        srcBufferStartingPoint - Starting point for the buffer.
        destBufferStartingPoint - Starting point for the buffer.
      • standardDeviation

        private float standardDeviation​(float[] list,
                                        float average)
        Finds the standard deviation of the values in the input list (defined as: s = [(1/(N-1))*SUM (from 0 to N-1)[ (Xi - X)^2]]^(1/2)).
        Parameters:
        list - float [] The list of numbers.
        average - Arithmetic mean of the values in list.
        Returns:
        The standard deviation.
      • volumeColorFilter

        private void volumeColorFilter​(float[] srcBuffer,
                                       float[] destBuffer)
        Filter a Color 3D image with a 3D kernel. Allows median filtering to include the picture elements at greater depths than only the current slice. This method allows selected band filtering, and does not filter the alpha band.
        Parameters:
        srcBuffer - float [] Source image.
        destBuffer - float [] Destination image.
        See Also:
        volumeFilter(float[],float[])
      • volumeVectorMagnitudeColorFilter

        private void volumeVectorMagnitudeColorFilter​(float[] srcBuffer,
                                                      float[] destBuffer)
        Filter a Color 3D image with a 3D kernel with vector magnitude filtering. Allows median filtering to include the picture elements at greater depths than only the current slice. This method does not filter the alpha band. Red, green, and blue are all filtered together; that is, the new red, green, and blue will all come from the same pixel.
        Parameters:
        srcBuffer - float [] Source image.
        destBuffer - float [] Destination image.
        See Also:
        volumeFilter(float[],float[])
      • volumeVectorDirectionColorFilter

        private void volumeVectorDirectionColorFilter​(float[] srcBuffer,
                                                      float[] destBuffer)
        Filter a Color 3D image with a 3D kernel with vector direction filtering. Allows median filtering to include the picture elements at greater depths than only the current slice. This method does not filter the alpha band. Red, green, and blue are all filtered together; that is, the new red, green, and blue will all come from the same pixel.
        Parameters:
        srcBuffer - float [] Source image.
        destBuffer - float [] Destination image.
        See Also:
        volumeFilter(float[],float[])
      • volumeAdaptiveTruncatedVectorMedianColorFilter

        private void volumeAdaptiveTruncatedVectorMedianColorFilter​(float[] srcBuffer,
                                                                    float[] destBuffer)
        Filter a Color 3D image with a 3D kernel with adaptive truncated vector median filtering. Allows median filtering to include the picture elements at greater depths than only the current slice. This method does not filter the alpha band. Red, green, and blue are all filtered together; that is, the new red, green, and blue will all come from the same pixel.
        Parameters:
        srcBuffer - float [] Source image.
        destBuffer - float [] Destination image.
        See Also:
        volumeFilter(float[],float[])
      • volumeFilter

        private void volumeFilter​(float[] srcBuffer,
                                  float[] destBuffer)
        Filter a 3D image with a 3D kernel. Allows median filtering to include the picture elements at greater depths than only the current slice.

        Note that this volume filter will correctly filter color images on all bands (aRGB) because the neighbor list is correct (see getNeighborList()). This means, however, it will not selectivly filter any bands (one may not filter only the Red channel, for instance), and will also filter all alpha values as well. Of course, progress bar updates will not include any color information. For these reasons it a useable, but limited color filter.

        Parameters:
        srcBuffer - float[] Source image.
        destBuffer - float [] Destination image.
      • volumeFilterBorder

        private void volumeFilterBorder​(float[] srcBdrBuffer,
                                        float[] destBuffer)
        Method performs median filtering on a volume that has been centerend in a border buffer and places the result in the destination buffer.
        Parameters:
        srcBdrBuffer - float[]
        destBuffer - float[]