Class AlgorithmMode

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

    public class AlgorithmMode
    extends AlgorithmBase
    AlgorithmMode applies one of the user specified kernels to a 2D or 3D BYTE, SHORT, INTEGER, UBYTE, USHORT, UINTEGER. AlgorithmMode is a spatial filtering technique that replaces the central kernel pixel with the mode of the pixels under the kernelMask. The mode of a set of values is the value that occurs most frequently. If all values are unique, that is, all numbers occur once in the kernelMask, the algorithm does NOT change the value of the central pixel.

    Mode filtering is currently thought to be useful in filtering a segmented image to get rid of small clusters of one value within a large cluser of a different value. We are currently investigating mode filtering in cleaning up a C-Means classification of brain images, which were detected using the Brain Extraction Tool (BET). Mode filtering can be applied to gray scale images to remove some noise, however, we have found that a 3X3X3 cube kernel significantly blurs the image, while an axis kernel appear to clean up some of the noise without too much blurring. The utility more filtering on gray scale images need more investigation.

    Giving credit where credit is due, this algorithm and its associated dialog, JDialogMode started with the AlgorithmMedian and JDialogMedian code. The color image stuff, iteration control, and standard deviation were removed. Furthermore, all processing is done using integer images and buffers, as finding the mode of floating (real) numbers is not meaningful.

    Version:
    1.0 June 24, 2004
    Author:
    Paul Hemler
    • 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
      • 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.
      • currentSlice

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

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

        private int halfK
        The kernel radius.
      • kernel

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

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

        private int[] kernelMask
        The kernel.
      • kernelShape

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

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

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

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

      • AlgorithmMode

        public AlgorithmMode​(ModelImage srcImg,
                             int kSize,
                             int kShape,
                             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.).
        kShape - Kernel shape: element neighbors to include when finding the mode.
        maskFlag - Flag that indicates that the mode filtering will be performed for the whole image if equal to true.
      • AlgorithmMode

        public AlgorithmMode​(ModelImage destImg,
                             ModelImage srcImg,
                             int kSize,
                             int kShape,
                             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.).
        kShape - Kernel shape: element neighbors to include when finding the mode.
        maskFlag - Flag that indicates that the mode filtering will be performed for the whole image if equal to true.
      • AlgorithmMode

        public AlgorithmMode​(ModelImage srcImg,
                             int kSize,
                             int kShape,
                             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.).
        kShape - Kernel shape: element neighbors to include when finding the mode.
        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 mode filtering will be performed for the whole image if equal to true.
      • AlgorithmMode

        public AlgorithmMode​(ModelImage destImg,
                             ModelImage srcImg,
                             int kSize,
                             int kShape,
                             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.).
        kShape - Kernel shape: element neighbors to include when finding the mode.
        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 mode 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
      • calcInPlaceBorder2D

        private void calcInPlaceBorder2D()
        Mode filters the 2D source image. Replaces the original image with the filtered image.
      • calcInPlaceBorder3D

        private void calcInPlaceBorder3D()
        Mode filters the 3D source image and replaces it with the mode filtered image.
      • calcStoreInDestBorder2D

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

        private void calcStoreInDestBorder3D()
        Mode filters the 3D source image and makes a new image for the mode filtered image.
      • copy2DSrcBufferToBdrBuffer

        private void copy2DSrcBufferToBdrBuffer​(int[] srcBuffer,
                                                int[] 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 - int[] the source image buffer
        bdrBuffer - int[] the border image buffer
        srcBufferOffsetIndex - offset into srcBuffer
        bdrBufferOffsetIndex - offset into bdrBuffer
      • copy3DSrcBufferToBdrBuffer

        private void copy3DSrcBufferToBdrBuffer​(int[] srcBuffer,
                                                int[] bdrBuffer)
        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 - int[] the source image buffer
        bdrBuffer - int[] the border image buffer
      • getBorderBufferNeighborList

        private int[] getBorderBufferNeighborList​(int i,
                                                  int[] data,
                                                  boolean is2D)
        Compiles a list of the values neighboring the desired pixel, that are defined in the kernel.
        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:
        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 mode of the central pixel. Its length is (number of pixels to be used to determine mode).

        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 mode sort.

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

      • mode

        private int mode​(int[] list,
                         int val)
        Finds the mode value of the list. If the numbers in the list are unique, return the value of the second parameter. This method assumes the parameter list is sorted, and counts consecutive array elements containing the same value.
        Parameters:
        list - int[] List of numbers
        val - Value to return if all list elements are unique
        Returns:
        the mode of the values in the list
      • setKernel

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

        private void sliceFilterBorder​(int[] srcBdrBuffer,
                                       int[] destBuffer,
                                       int srcBufferStartingPoint,
                                       int destBufferStartingPoint)
        Performs mode filtering on a single slice with a 2D or 3D buffer.
        Parameters:
        srcBdrBuffer - Source buffer.
        destBuffer - Destination Buffer.
        srcBufferStartingPoint - Index of the first pixel in the source buffer
        destBufferStartingPoint - Index of the first pixel for the slice to be filtered in the destination buffer.
      • volumeFilterBorder

        private void volumeFilterBorder​(int[] srcBdrBuffer,
                                        int[] destBuffer)
        Assumes a monochrome image of type BYTE, SHORT, INTEGER, UBYTE, USHORT, UINTEGER.
        Parameters:
        srcBdrBuffer - int[] the border buffer containing the source image data
        destBuffer - int[] the destination buffer