Class AlgorithmMode
- java.lang.Object
-
- java.lang.Thread
-
- gov.nih.mipav.model.algorithms.AlgorithmBase
-
- gov.nih.mipav.model.algorithms.filters.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 Summary
Fields Modifier and Type Field Description static int
AXIAL_KERNEL
(3D only).(package private) int
bdrBufferDepth
Border buffer number of slices.(package private) int
bdrBufferHeight
Border buffer number of rows.(package private) int
bdrBufferWidth
Border buffer number of columns.static int
CROSS_KERNEL
cross (2D only).static int
CUBE_KERNEL
(3D only).private int
currentSlice
Used for 3D loop control.private boolean
entireImage
true means apply to entire image, false only region.private int
halfK
The kernel radius.static int
HORZ_KERNEL
horizontal (2D only).private byte[]
kernel
mask to determine the region of pixels used in the mode filter.private int
kernelCenter
The index of the kernel center.private int[]
kernelMask
The kernel.private int
kernelShape
user-selectable shape of the region for neighbor-selection.private int
kernelSize
dimension of the kernel (ie., 5 = 5x5, 7 = 7x7, 9 = 9x9, etc.).private int
numberOfSlices
Used for 3D loop control.private boolean
sliceFiltering
do all filtering slice-by-slice, rather than as a volume.static int
SQUARE_KERNEL
square kernel (2D only).(package private) int
srcBufferDepth
Source buffer number of slices.(package private) int
srcBufferHeight
Source buffer number of rows.(package private) int
srcBufferWidth
Source buffer number of columns.static int
VERT_KERNEL
vertical (2D only).static int
X_KERNEL
X-shaped kernel, from 1 corner to opposite corner.-
Fields inherited from class gov.nih.mipav.model.algorithms.AlgorithmBase
destFlag, destImage, image25D, mask, maxProgressValue, minProgressValue, multiThreadingEnabled, nthreads, progress, progressModulus, progressStep, runningInSeparateThread, separable, srcImage, threadStopped
-
-
Constructor Summary
Constructors Constructor Description AlgorithmMode(ModelImage srcImg, int kSize, int kShape, boolean maskFlag)
Constructor for 2D images in which changes are returned to the source image.AlgorithmMode(ModelImage srcImg, int kSize, int kShape, boolean sliceBySlice, boolean maskFlag)
Constructor for 3D images in which changes are returned to the source image.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.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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
calcInPlaceBorder2D()
Mode filters the 2D source image.private void
calcInPlaceBorder3D()
Mode filters the 3D source image and replaces it with the mode filtered image.private void
calcStoreInDestBorder2D()
This function produces a new image that has been mode filtered and places filtered image in the destination image.private void
calcStoreInDestBorder3D()
Mode filters the 3D source image and makes a new image for the mode filtered image.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.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.void
finalize()
Prepares this class for destruction.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.private void
makeKernel()
Forms kernel.private void
makeKernelMask()
Makes the kernel mask.private int
mode(int[] list, int val)
Finds the mode value of the list.void
runAlgorithm()
Starts the algorithm.private void
setKernel()
Fill in the mask for which pixels are used in filtering.private void
sliceFilterBorder(int[] srcBdrBuffer, int[] destBuffer, int srcBufferStartingPoint, int destBufferStartingPoint)
Performs mode filtering on a single slice with a 2D or 3D buffer.private void
volumeFilterBorder(int[] srcBdrBuffer, int[] destBuffer)
Assumes a monochrome image of type BYTE, SHORT, INTEGER, UBYTE, USHORT, UINTEGER.-
Methods inherited from class gov.nih.mipav.model.algorithms.AlgorithmBase
actionPerformed, addListener, addProgressChangeListener, calculateImageSize, calculatePrincipleAxis, computeElapsedTime, computeElapsedTime, convertIntoFloat, delinkProgressToAlgorithm, delinkProgressToAlgorithmMulti, displayError, errorCleanUp, fireProgressStateChanged, fireProgressStateChanged, fireProgressStateChanged, fireProgressStateChanged, fireProgressStateChanged, generateProgressValues, getDestImage, getElapsedTime, getMask, getMaxProgressValue, getMinProgressValue, getNumberOfThreads, getProgress, getProgressChangeListener, getProgressChangeListeners, getProgressModulus, getProgressStep, getProgressValues, getSrcImage, isCompleted, isImage25D, isMultiThreadingEnabled, isRunningInSeparateThread, isThreadStopped, linkProgressToAlgorithm, linkProgressToAlgorithm, makeProgress, notifyListeners, removeListener, removeProgressChangeListener, run, setCompleted, setImage25D, setMask, setMaxProgressValue, setMinProgressValue, setMultiThreadingEnabled, setNumberOfThreads, setProgress, setProgressModulus, setProgressStep, setProgressValues, setProgressValues, setRunningInSeparateThread, setSrcImage, setStartTime, setThreadStopped, startMethod, windowActivated, windowClosed, windowClosing, windowDeactivated, windowDeiconified, windowIconified, windowOpened
-
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, toString, yield
-
-
-
-
Field Detail
-
SQUARE_KERNEL
public static final int SQUARE_KERNEL
square kernel (2D only).- See Also:
- Constant Field Values
-
CUBE_KERNEL
public static final int CUBE_KERNEL
(3D only).- See Also:
- Constant Field Values
-
CROSS_KERNEL
public static final int CROSS_KERNEL
cross (2D only).- See Also:
- Constant Field Values
-
AXIAL_KERNEL
public static final int AXIAL_KERNEL
(3D 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
-
HORZ_KERNEL
public static final int HORZ_KERNEL
horizontal (2D only).- See Also:
- Constant Field Values
-
VERT_KERNEL
public static final int VERT_KERNEL
vertical (2D only).- 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 classAlgorithmBase
-
runAlgorithm
public void runAlgorithm()
Starts the algorithm.- Specified by:
runAlgorithm
in classAlgorithmBase
-
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 bufferbdrBuffer
- int[] the border image buffersrcBufferOffsetIndex
- offset into srcBufferbdrBufferOffsetIndex
- 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 bufferbdrBuffer
- 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 datais2D
- 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 numbersval
- 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 bufferdestBufferStartingPoint
- 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 datadestBuffer
- int[] the destination buffer
-
-