Class AlgorithmMean

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

    public class AlgorithmMean
    extends AlgorithmBase
    Algorithm to apply a mean filter to an image, placing it in a new ModelImage if New image is selected or returning the changed picture to the same image if Replace image is selected. Define a region including the target pixel and neigbors. Then the mean is given by the sum of all the pixels in the region divided by the total number of pixels in the region. The target regions are either odd lengthed squares for slice averaging or odd lengthed cubes for volume multislice averaging. For 2D imaging only slice averaging can be used since there is only 1 slice. For 3D averaging slice averaging is the default, but slice averaging can be selected with the Apply slice kernel button and volume averaging can be selected with the Apply volume kernel button. If slice averaging is selected, a 3 x 3 kernel size is the default, but a 3 x 3, 5 x 5, 7 x 7, 9 x 9, or 11 x 11 size may be selected. If volume averaging is selected, a 3 x 3 x 3 kernel size is the default, but a 3 x 3 x 3, 5 x 5 x 5, 7 x 7 x 7, 9 x 9 x 9, or 11 x 11 x 11 kernel size may be selected. With slice averaging the time requirement will grow as the square of the kernel length and with volume averaging the time requirement will grow as the cube of the kernel length.

    Whole image application of the mean is the default, but the user can select either whole image or voi region(s). If voi region(s) is selected, then the mean value of the pixel will replace the original value of the pixel inside the voi regions(s) and the pixel values outside the voi regions(s) will remain unchanged.

    For color images the default is to obtain the means of the red, green, and blue channels. However, if Red Channel, Green Channel, or Blue Channel is unchecked, then that color channel will pass thru with no mean averaging performed.

    Note the the term average does not always represent mean. If fact, there are three types of average, the commonly used mean and median and the infrequently used mode. The mean is defined as the sum of the values in a group divided by the number of values in a group. The median value is defined as the value which has the same number of values greater than it and less than it. The mode is defined as the value with the greatest number of occurrences.

    If part of the neighborhood kernel falls outside the image when the pixel is on or near the boundary, simply only use that portion of the kernel inside the image. Consider, for example, the upper right corner pixel in a 2D image using a 3 x 3 kernel. In this case only the target pixel and its lower, left, and diagonal left low neighbors will be used. The sum of these 4 pixels will be divided by 4.

    If the image is color and the filterType == ADAPTIVE_VECTOR, then an adaptive vector directional filter is used. The distance between 2 pixels is taken as the vector angle between the 2 pixels: acos(dotProduct(xi, xj)/(norm(xi)*norm(xj)). For each position xi in the window, the sum of the angles over all j is obtained: ai = sum over all j of angle(i,j). A weight wi is then taken as wi = 2.0/(1.0 + exp(ai)). The weights are normalized by dividing each weight by the sum of all the weights in the window: wi = wi/(sum over i of wi). Then the average is obtained by multiplying each pixel in the window by the normalized weight. The red, green, and blue from a given pixel will always be used together as a unit; the red, green, and blue from the same pixel will always be multiplied by the same weight. In summary, a fuzzy membership function based on an angle criterion is used to determine the weights of an adaptive weighted mean filter. One reference is: "Color Image Processing Using Adaptive Vector Directional Filters" by K. N. Plataniotis, D. Androutsos, and A. N. Venetsanopoulos, IEEE Transactions on Circuits and Systems - II: Analog and Digital Sginal Processing, Vol. 45, No. 10, October, 1998, pp. 1414-1419.

    If the image is color and the filterType == PARALLEL_VECTOR, then the image is decomposed into components parallel and perpindicular to the direction (redVector, greenVector, blueVector). Only the parallel component is filtered. After filtering the parallel component, the parallel and perpindicular components are added back together. The filtering takes place in parallel and perpindicular directions in hue-saturation space. This algorithm is from "Linear Colour-Dependent Image Filtering based on Vector Decomposition" by Stephen J. Sangwine, Todd A. Ell, Barnabas N. Gatsheni. In a later paper "Colour-Dependent Linear Vector Image Filtering" by Stephen J. Sangwine, Todd A. Ell, and Barbabas N. Gatsheni, 3 major limitations of this scheme are discussed: 1.) Limited color selectivity Filter as the cosine of the angle between any hue and the reference hue. 2.) Luminance filtering, even in pixels not close to the chosen color direction. and 3.) The opponent color to the color of interest is not separable from the color of interest using a linear operation, although a nonlinear extraction using the sign bit would be easy enough to perform.

    • Field Detail

      • bChannel

        private boolean bChannel
        DOCUMENT ME!
      • currentSlice

        private int currentSlice
        DOCUMENT ME!
      • entireImage

        private boolean entireImage
        DOCUMENT ME!
      • gChannel

        private boolean gChannel
        DOCUMENT ME!
      • halfK

        private int halfK
        DOCUMENT ME!
      • isColorImage

        private boolean isColorImage
        DOCUMENT ME!
      • kernelSize

        private int kernelSize
        DOCUMENT ME!
      • mask

        private java.util.BitSet mask
        DOCUMENT ME!
      • numberOfSlices

        private int numberOfSlices
        DOCUMENT ME!
      • rChannel

        private boolean rChannel
        DOCUMENT ME!
      • sliceFiltering

        private boolean sliceFiltering
        DOCUMENT ME!
      • valuesPerPixel

        private int valuesPerPixel
        DOCUMENT ME!
      • filterType

        private int filterType
      • redNorm

        private float redNorm
      • greenNorm

        private float greenNorm
      • blueNorm

        private float blueNorm
      • midGray

        private float midGray
      • colorMax

        private float colorMax
    • Constructor Detail

      • AlgorithmMean

        public AlgorithmMean​(ModelImage srcImg,
                             int kSize,
                             boolean maskFlag)
        Constructor for 2D images in which changes are returned to the source image.
        Parameters:
        srcImg - Source image model.
        kSize - Kernel size: dimension of the kernel (ie., 5 = 5x5, 7 = 7x7, 9 = 9x9, etc.).
        maskFlag - Flag that indicates that the mean filtering will be performed for the whole image if equal to true.
      • AlgorithmMean

        public AlgorithmMean​(ModelImage destImg,
                             ModelImage srcImg,
                             int kSize,
                             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.
        kSize - Kernel size: dimension of the kernel (ie., 5 = 5x5, 7 = 7x7, 9 = 9x9, etc.).
        maskFlag - Flag that indicates that the mean filtering will be performed for the whole image if equal to true.
      • AlgorithmMean

        public AlgorithmMean​(ModelImage srcImg,
                             int kSize,
                             boolean sliceBySlice,
                             boolean maskFlag)
        Constructor for 3D images in which changes are returned to the source image.
        Parameters:
        srcImg - Source image model.
        kSize - Kernel size: dimension of the kernel (ie., 5 = 5x5, 7 = 7x7, 9 = 9x9, etc.).
        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 mean filtering will be performed for the whole image if equal to true.
      • AlgorithmMean

        public AlgorithmMean​(ModelImage destImg,
                             ModelImage srcImg,
                             int kSize,
                             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.
        kSize - Kernel size: dimension of the kernel (ie., 5 = 5x5, 7 = 7x7, 9 = 9x9, etc.).
        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 mean 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 filterType,
                                        boolean r,
                                        boolean g,
                                        boolean b,
                                        int redVector,
                                        int greenVector,
                                        int blueVector)
        If filterType == SEPARATE_COMPONENT, RGB images are mean filtered by 'channel.' That is, each color, red, blue and green, is filtered independently of the other two colors. This mean filter permits selectively filtering any combination of the three channels instead of simply filtering all three. If filterType == ADAPTIVE_VECTOR, use fuzzy memebership functions based on an angle criterion to determine the weights of an adaptive weighted mean filter. If filterType == PARALLEL_VECTOR, decompose image into components parallel and perpindicular to the direction given by (redVector, greenVector, and blueVector) and only filter the parallel component before adding the 2 components back together.
        Parameters:
        filterType -
        r - Filter red channel.
        g - Filter green channel.
        b - Filter blue channel.
        redVector -
        greenVector -
        blueVector -
      • calcInPlace2D

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

        private void calcInPlace3D()
        Mean filters the source image and replaces the source image with the mean filtered image.
      • calcStoreInDest2D

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

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

        private float getMean​(int i,
                              float[] data,
                              boolean is2D)
        DOCUMENT ME!
        Parameters:
        i - The central pixel to find neighbors for.
        data - 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:
        mean value of the kernel neighborhood
      • getAdaptiveMean

        private float[] getAdaptiveMean​(int i,
                                        float[] data,
                                        boolean is2D)
        Used for color images only when filterType == ADAPTIVE_VECTOR.
        Parameters:
        i - The central pixel to find neighbors for.
        data - 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:
        mean value of the kernel neighborhood
      • 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 - Source buffer.
        destBuffer - Destination Buffer.
        bufferStartingPoint - Starting point for the buffer.
        msgString - A text message that can be displayed as a message text in the progressBar.
      • sliceParallelVectorFilter

        private void sliceParallelVectorFilter​(float[] srcBuffer,
                                               float[] destBuffer,
                                               int bufferStartingPoint,
                                               java.lang.String msgString)
        Allows a single slice to be filtered. Used only for color images when filterType == PARALLEL_VECTOR.
        Parameters:
        srcBuffer - Source buffer.
        destBuffer - Destination Buffer.
        bufferStartingPoint - Starting point for the buffer.
        msgString - A text message that can be displayed as a message text in the progressBar.
      • sliceAdaptiveVectorFilter

        private void sliceAdaptiveVectorFilter​(float[] srcBuffer,
                                               float[] destBuffer,
                                               int bufferStartingPoint,
                                               java.lang.String msgString)
        Allows a single slice to be filtered. Used only for color images when filterType == ADAPTIVE_VECTOR.
        Parameters:
        srcBuffer - Source buffer.
        destBuffer - Destination Buffer.
        bufferStartingPoint - Starting point for the buffer.
        msgString - A text message that can be displayed as a message text in the progressBar.
      • volumeColorFilter

        private void volumeColorFilter​(float[] srcBuffer,
                                       float[] destBuffer)
        Filter a Color 3D image with a 3D kernel. Allows mean 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 - Source image.
        destBuffer - Destination image.
        See Also:
        volumeFilter(float[],float[])
      • volumeParallelVectorFilter

        private void volumeParallelVectorFilter​(float[] srcBuffer,
                                                float[] destBuffer)
        Filter a Color 3D image with a 3D kernel. Allows mean 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. Used on when filterType == PARALLEL_VECTOR.
        Parameters:
        srcBuffer - Source image.
        destBuffer - Destination image.
        See Also:
        volumeFilter(float[],float[])
      • volumeAdaptiveVectorFilter

        private void volumeAdaptiveVectorFilter​(float[] srcBuffer,
                                                float[] destBuffer)
        Filter a Color 3D image with a 3D kernel. Allows mean 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. Used only when filterType == ADAPTIVE_VECTOR.
        Parameters:
        srcBuffer - Source image.
        destBuffer - Destination image.
        See Also:
        volumeFilter(float[],float[])
      • volumeFilter

        private void volumeFilter​(float[] srcBuffer,
                                  float[] destBuffer)
        Filter a 3D image with a 3D kernel. Allows mean 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 - Source image.
        destBuffer - Destination image.
        See Also:
        volumeColorFilter(float[],float[]), getNeighborList