Class FixedWeightsKernelFilter

java.lang.Object
com.xinapse.filter.AbstractKernelFilter
com.xinapse.filter.FixedWeightsKernelFilter
All Implemented Interfaces:
SpatialFilter
Direct Known Subclasses:
GaussianBlurFilter, LaplacianFilter, SobelFilter

public class FixedWeightsKernelFilter extends AbstractKernelFilter implements SpatialFilter
A class for performing filtering of images based on a moving kernel of pixels with fixed weights within the kernel.
  • Field Details

    • weights

      protected final float[][][] weights
      The 3-D array of weights in the kernel. The first index is the slice index, the second is the row index, and the third is the column index.
  • Constructor Details

    • FixedWeightsKernelFilter

      protected FixedWeightsKernelFilter()
      Default constructor produces an identity FixedWeightsKernelFilter with kernel size 1x1x1 that has no effect when applied.
    • FixedWeightsKernelFilter

      public FixedWeightsKernelFilter(float[][] wts, boolean normalise, BoundaryCondition boundaryCondition) throws IllegalArgumentException
      Constructs a 2-dimensional kernel-based filter with the given filter weights. The filter may optionally be normalised so that it is intensity-preserving (i.e., the sum of weights will be forced to add up to 1).
      Parameters:
      wts - a two-dimensional array of weights. This array must have an odd number of elements in each of the two dimensions. The centre of the weights kernel will be centred on the pixel being filtered. The first index of the array refers to the the row direction, and the second to the column direction.
      normalise - if true the weights will be normalised when constructing the kernel. If the weights cannot be normalised, then this argument has no effect.
      boundaryCondition - the BoundaryCondition to apply when this filter is applied to an array of pixels.
      Throws:
      IllegalArgumentException - if the dimensions of the kernel weights array aren't odd-numbered in each of the two dimensions.
    • FixedWeightsKernelFilter

      public FixedWeightsKernelFilter(float[][][] wts, boolean normalise, BoundaryCondition boundaryCondition) throws IllegalArgumentException
      Constructs a 3-dimensional kernel-based filter with the given filter weights. The filter may optionally be normalised so that it is intensity-preserving (i.e., the sum of weights will be forced to add up to 1).
      Parameters:
      wts - a three-dimensional array of weights. This array must have an odd number of elements in each of the three dimensions. The centre of the weights kernel will be centred on the pixel being filtered. The first index of the array refers to the slice direction, the second index to the row direction, and the third to the column direction.
      normalise - if true the weights will be normalised when constructing the kernel. If the weights cannot be normalised, then this argument has no effect.
      boundaryCondition - the BoundaryCondition to apply when this filter is applied to an array of pixels.
      Throws:
      IllegalArgumentException - if the dimensions of the kernel weights array aren't odd-numbered in each of the three dimensions.
  • Method Details

    • getNKernelCols

      public int getNKernelCols()
      Description copied from class: AbstractKernelFilter
      Returns the number of columns in the filter kernel.
      Specified by:
      getNKernelCols in class AbstractKernelFilter
      Returns:
      the number of columns in the filter kernel.
    • getNKernelRows

      public int getNKernelRows()
      Description copied from class: AbstractKernelFilter
      Returns the number of rows in the filter kernel.
      Specified by:
      getNKernelRows in class AbstractKernelFilter
      Returns:
      the number of rows in the filter kernel.
    • getNKernelSlices

      public int getNKernelSlices()
      Description copied from class: AbstractKernelFilter
      Returns the number of slices in the filter kernel.
      Specified by:
      getNKernelSlices in class AbstractKernelFilter
      Returns:
      the number of slices in the filter kernel.
    • getFlatKernelWeights

      public float[] getFlatKernelWeights()
      Returns the kernel weights as a flat array.
      Returns:
      the kernel weights as a flat array in slice/row/col major order.
    • scale

      public void scale(float scaleFactor)
      Scale all kernel weights by the given scaling factor.
      Parameters:
      scaleFactor - the scaling factor for the weights.
    • sumWeights

      public float sumWeights()
      Returns the sum of all the weights for this FixedWeightsKernelFilter.
      Returns:
      the sum of all the weights.
    • add

      public void add(FixedWeightsKernelFilter filter) throws IllegalArgumentException
      Adds the weights from a FixedWeightsKernelFilter to the weights of this one.
      Parameters:
      filter - the FixedWeightsKernelFilter from which to add the weights.
      Throws:
      IllegalArgumentException - if the two filters do not have the same kernel dimensions.
    • add

      Adds the weights from two FixedWeightsKernelFilters to create a new FixedWeightsKernelFilter.
      Parameters:
      filter1 - the first FixedWeightsKernelFilter from which to add the weights.
      filter2 - the second FixedWeightsKernelFilter from which to add the weights.
      Returns:
      a new FixedWeightsKernelFilter with weights which calculated by summing the weights from the two supplied filters.
      Throws:
      IllegalArgumentException - if the two filters do not have the same kernel dimensions.
    • copyResize

      public FixedWeightsKernelFilter copyResize(int[] newSize, boolean normalise) throws IllegalArgumentException
      Copies a FixedWeightsKernelFilter, possibly resizing it, creating a new FixedWeightsKernelFilter. If the requested size is larger than the current size in any direction, the new filter is padded with zeros. If the requested size is smaller than the current size in any direction, the new filter is cropped.
      Parameters:
      newSize - an array of length at least 3 that specifies the new kernel size.
      normalise - whether the resulting FixedWeightsKernelFilter should be normalised (the sum of all weights should be unity).
      Returns:
      a copy of the supplied FixedWeightsKernelFilter, but resized.
      Throws:
      IllegalArgumentException
    • convolve

      Convolves this FixedWeightsKernelFilter with another FixedWeightsKernelFilter to create a new FixedWeightsKernelFilter.
      Parameters:
      filter - the FixedWeightsKernelFilter with which this FixedWeightsKernelFilter will be convolved.
      Returns:
      a new FixedWeightsKernelFilter that results from convolving this filter with the supplied filter.
    • filterInPlaceBinary

      public void filterInPlaceBinary(BitSet pixels, int nCols, int nRows, int nSlices, MonitorWorker worker, boolean verbose) throws CancelledException
      Description copied from class: AbstractKernelFilter
      Filter an array of binary pixel values "in place". This default implementation should be overridden by AbstractKernelFilters that are interested in a more efficient implementation for PixelDataType.BINARY images.
      Overrides:
      filterInPlaceBinary in class AbstractKernelFilter
      Parameters:
      pixels - the array of image pixel values.
      nCols - the number of image columns
      nRows - the number of image rows.
      nSlices - the number of image slices.
      worker - the MonitorWorker that may be used to cancel the filter operation.
      verbose - whether verbose reporting to System.out is turned on.
      Throws:
      CancelledException - if the filter operation is cancelled by the user or programmatically.
    • filterInPlaceByte

      public void filterInPlaceByte(byte[] pixels, int nCols, int nRows, int nSlices, MonitorWorker worker, boolean verbose) throws CancelledException
      Description copied from class: AbstractKernelFilter
      Filter an array of signed byte pixel values "in place". This default implementation should be overridden by SpatialFilters that are interested in a more efficient implementation for PixelDataType.BYTE images.
      Overrides:
      filterInPlaceByte in class AbstractKernelFilter
      Parameters:
      pixels - the array of image pixel values.
      nCols - the number of image columns
      nRows - the number of image rows.
      nSlices - the number of image slices.
      worker - the MonitorWorker that may be used to cancel the filter operation.
      verbose - whether verbose reporting to System.out is turned on.
      Throws:
      CancelledException - if the filter operation is cancelled by the user or programmatically.
    • filterInPlaceUByte

      public void filterInPlaceUByte(byte[] pixels, int nCols, int nRows, int nSlices, MonitorWorker worker, boolean verbose) throws CancelledException
      Description copied from class: AbstractKernelFilter
      Filter an array of unsigned byte pixel values "in place". This default implementation should be overridden by SpatialFilters that are interested in a more efficient implementation for PixelDataType.UBYTE images.
      Overrides:
      filterInPlaceUByte in class AbstractKernelFilter
      Parameters:
      pixels - the array of image pixel values.
      nCols - the number of image columns
      nRows - the number of image rows.
      nSlices - the number of image slices.
      worker - the MonitorWorker that may be used to cancel the filter operation.
      verbose - whether verbose reporting to System.out is turned on.
      Throws:
      CancelledException - if the filter operation is cancelled by the user or programmatically.
    • filterInPlaceShort

      public void filterInPlaceShort(short[] pixels, int nCols, int nRows, int nSlices, MonitorWorker worker, boolean verbose) throws CancelledException
      Description copied from class: AbstractKernelFilter
      Filter an array of signed short pixel values "in place". This default implementation should be overridden by SpatialFilters that are interested in a more efficient implementation for PixelDataType.SHORT images.
      Overrides:
      filterInPlaceShort in class AbstractKernelFilter
      Parameters:
      pixels - the array of image pixel values.
      nCols - the number of image columns
      nRows - the number of image rows.
      nSlices - the number of image slices.
      worker - the MonitorWorker that may be used to cancel the filter operation.
      verbose - whether verbose reporting to System.out is turned on.
      Throws:
      CancelledException - if the filter operation is cancelled by the user or programmatically.
    • filterInPlaceUShort

      public void filterInPlaceUShort(short[] pixels, int nCols, int nRows, int nSlices, MonitorWorker worker, boolean verbose) throws CancelledException
      Description copied from class: AbstractKernelFilter
      Filter an array of unsigned short pixel values "in place". This default implementation should be overridden by SpatialFilters that are interested in a more efficient implementation for PixelDataType.USHORT images.
      Overrides:
      filterInPlaceUShort in class AbstractKernelFilter
      Parameters:
      pixels - the array of image pixel values.
      nCols - the number of image columns
      nRows - the number of image rows.
      nSlices - the number of image slices.
      worker - the MonitorWorker that may be used to cancel the filter operation.
      verbose - whether verbose reporting to System.out is turned on.
      Throws:
      CancelledException - if the filter operation is cancelled by the user or programmatically.
    • filterInPlaceInt

      public void filterInPlaceInt(int[] pixels, int nCols, int nRows, int nSlices, MonitorWorker worker, boolean verbose) throws CancelledException
      Description copied from class: AbstractKernelFilter
      Filter an array of signed int pixel values "in place". This default implementation should be overridden by SpatialFilters that are interested in a more efficient implementation for PixelDataType.INT images.
      Overrides:
      filterInPlaceInt in class AbstractKernelFilter
      Parameters:
      pixels - the array of image pixel values.
      nCols - the number of image columns
      nRows - the number of image rows.
      nSlices - the number of image slices.
      worker - the MonitorWorker that may be used to cancel the filter operation.
      verbose - whether verbose reporting to System.out is turned on.
      Throws:
      CancelledException - if the filter operation is cancelled by the user or programmatically.
    • filterInPlaceUInt

      public void filterInPlaceUInt(int[] pixels, int nCols, int nRows, int nSlices, MonitorWorker worker, boolean verbose) throws CancelledException
      Description copied from class: AbstractKernelFilter
      Filter an array of unsigned int pixel values "in place". This default implementation should be overridden by SpatialFilters that are interested in a more efficient implementation for PixelDataType.UINT images.
      Overrides:
      filterInPlaceUInt in class AbstractKernelFilter
      Parameters:
      pixels - the array of image pixel values.
      nCols - the number of image columns
      nRows - the number of image rows.
      nSlices - the number of image slices.
      worker - the MonitorWorker that may be used to cancel the filter operation.
      verbose - whether verbose reporting to System.out is turned on.
      Throws:
      CancelledException - if the filter operation is cancelled by the user or programmatically.
    • filterInPlaceLong

      public void filterInPlaceLong(long[] pixels, int nCols, int nRows, int nSlices, MonitorWorker worker, boolean verbose) throws CancelledException
      Description copied from class: AbstractKernelFilter
      Filter an array of long pixel values "in place". This default implementation should be overridden by SpatialFilters that are interested in a more efficient implementation for PixelDataType.LONG images.
      Overrides:
      filterInPlaceLong in class AbstractKernelFilter
      Parameters:
      pixels - the array of image pixel values.
      nCols - the number of image columns
      nRows - the number of image rows.
      nSlices - the number of image slices.
      worker - the MonitorWorker that may be used to cancel the filter operation.
      verbose - whether verbose reporting to System.out is turned on.
      Throws:
      CancelledException - if the filter operation is cancelled by the user or programmatically.
    • filterInPlaceFloat

      public void filterInPlaceFloat(float[] pixels, int nCols, int nRows, int nSlices, MonitorWorker worker, boolean verbose) throws CancelledException
      Description copied from class: AbstractKernelFilter
      Filter an array of floating-point pixel values "in place".
      Specified by:
      filterInPlaceFloat in class AbstractKernelFilter
      Parameters:
      pixels - the array of image pixel values.
      nCols - the number of image columns
      nRows - the number of image rows.
      nSlices - the number of image slices.
      worker - the MonitorWorker that may be used to cancel the filter operation.
      verbose - whether verbose reporting to System.out is turned on.
      Throws:
      CancelledException - if the filter operation is cancelled by the user or programmatically.
    • filterInPlaceFloat

      public void filterInPlaceFloat(float[] pixels, int fromCol, int toCol, int fromRow, int toRow, int fromSlice, int toSlice, int nCols, int nRows, int nSlices)
      Filter a cuboid portion of an array of floating-point pixel values "in place".
      Parameters:
      pixels - the array of image pixel values.
      fromCol - the first image column to start filtering.
      toCol - the last image column (exclusive) to stop filtering.
      fromRow - the first image row to start filtering.
      toRow - the last image row to stop filtering.
      fromSlice - the first image row to start filtering.
      toSlice - the last image slice to start filtering.
      nCols - the number of image columns.
      nRows - the number of image rows.
      nSlices - the number of image slices.
    • filterInPlaceDouble

      public void filterInPlaceDouble(double[] pixels, int nCols, int nRows, int nSlices, MonitorWorker worker, boolean verbose) throws CancelledException
      Description copied from class: AbstractKernelFilter
      Filter an array of double-precision pixel values "in place". This default implementation should be overridden by SpatialFilters that are interested in a more efficient implementation for PixelDataType.DOUBLE images.
      Overrides:
      filterInPlaceDouble in class AbstractKernelFilter
      Parameters:
      pixels - the array of image pixel values.
      nCols - the number of image columns
      nRows - the number of image rows.
      nSlices - the number of image slices.
      worker - the MonitorWorker that may be used to cancel the filter operation.
      verbose - whether verbose reporting to System.out is turned on.
      Throws:
      CancelledException - if the filter operation is cancelled by the user or programmatically.
    • toImage

      public WritableImage toImage(Class<? extends WritableImage> imageClass) throws InvalidImageException, IOException
      Create an image of the kernel weights.
      Parameters:
      imageClass - the Class of image to create.
      Returns:
      an image of the kernel weights.
      Throws:
      InvalidImageException - if the image cannot be created.
      IOException - if the image cannot be created because of an I/O error.
    • toString

      public String toString()
      Returns a String describing this FixedWeightsKernelFilter.
      Overrides:
      toString in class Object
      Returns:
      a String showing the weights of this FixedWeightsKernelFilter.