Package gov.nih.mipav.model.algorithms
Class AlgorithmMeanShiftClustering
- java.lang.Object
-
- java.lang.Thread
-
- gov.nih.mipav.model.algorithms.AlgorithmBase
-
- gov.nih.mipav.model.algorithms.AlgorithmMeanShiftClustering
-
- All Implemented Interfaces:
java.awt.event.ActionListener
,java.awt.event.WindowListener
,java.lang.Runnable
,java.util.EventListener
public class AlgorithmMeanShiftClustering extends AlgorithmBase
The java code is ported from C++ code downloaded from http://coewww.rutgers.edu/riul/research/code.html. The relevant web page section says: Adaptive mean shift based clustering C++ code implementing an (approximate) mean shift procedure with variable bandwith (in high dimensions). The algorithm is described in Mean shift based clustering in high dimensions: A texture classification example. For comments, please contact Bogdan Georgescu or Ilan Shimshoni. The original paper was written by Bogdan Georgescu, Ilan Shimshoni, and P. Meer in the proceedings of ICCV 2003. The original files were fams.cpp and fams.h. This code was ported by William Gandler.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
AlgorithmMeanShiftClustering.fams_cut
private class
AlgorithmMeanShiftClustering.fams_hash_entry
private class
AlgorithmMeanShiftClustering.fams_hash_entry2
private class
AlgorithmMeanShiftClustering.fams_res_cont
private class
AlgorithmMeanShiftClustering.famsPoint
-
Field Summary
Fields Modifier and Type Field Description private int[]
array1
private static int
Bs
private static int
Bs2
private byte[]
byteFloatBuffer
byte array for float *private byte[]
byteIntBuffer
byte array for int *private int
choosePoints
private int[][]
data
private java.lang.String
data_file_name
private int
dataSize
private boolean
endianess
private float
epsilon
static int
EVERY_POINT
private static double
FAMS_ALPHA
private static int
FAMS_BLOCKSIZE
private static int
FAMS_BLOCKSIZE2
private boolean
FAMS_DO_SPEEDUP
private static int
FAMS_FKL_NEL
private static int
FAMS_FKL_TIMES
private static float
FAMS_FLOAT_SHIFT
private static int
FAMS_MAX_K
private static int
FAMS_MAX_L
private static int
FAMS_MAXITER
private static int
FAMS_PRUNE_HDIV
private static int
FAMS_PRUNE_MAXM
private static int
FAMS_PRUNE_MAXP
private static int
FAMS_PRUNE_MINN
private static int
FAMS_PRUNE_WINDOW
private boolean
findOptimalKL
private boolean
fixedWidth
private int[]
hashCoeffs
private int[][]
hmodes
private java.lang.String
input_directory
private int
jump
private int
K
private int
K_
private int
k_neigh
private int
Kjump
private int
Kmin
private int
L
private int
L_
private int
M
private int
M2
private float
maxVal
private float
minVal
private int[][]
modes
private ModelImage
modesImage
private int
nDims
private int
nnres1
private int
nnres2
private boolean
noLSH
private int
npm
private int
nPoints
private int[]
nprunedmodes
private int
nsel
private double
percent
private AlgorithmMeanShiftClustering.famsPoint[]
points
private int[][]
prunedmodes
private ModelImage
prunedModesImage
private int[]
psel
private float[]
pttemp
private java.io.RandomAccessFile
raFile
(package private) double[]
rr
static int
SELECT_ON_JUMP
static int
SELECT_PERCENT
private java.util.Random
srand
private int[][]
t_cut_res
private int[]
t_hjump
private int[]
t_m
private int[]
t_m2
private int[][]
t_old_cut_res
private int[]
t_old_m
private long
tt1
private float
width
-
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 AlgorithmMeanShiftClustering(ModelImage image, int K, int L, int k_neigh, java.lang.String data_file_name, java.lang.String input_directory, int choosePoints, int jump, double percent, boolean fixedWidth, float width, boolean findOptimalKL, float epsilon, int Kmin, int Kjump, boolean FAMS_DO_SPEEDUP, int nPoints, int nDims, float[] pttemp, ModelImage modesImage, ModelImage prunedModesImage)
File formats: Files are all ASCII files.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
addDataToHash(AlgorithmMeanShiftClustering.fams_hash_entry[][] HT, int[] hs, AlgorithmMeanShiftClustering.famsPoint pt, int where, int Bs, int M, int which, int which2, int hjump)
private void
addDataToRes(AlgorithmMeanShiftClustering.fams_hash_entry[][] HT, int[] hs, AlgorithmMeanShiftClustering.fams_res_cont res, int where, int Bs, int M, int which, int nnres, int which2, int hjump)
(package private) void
bgISort(int[] ra, int nVec, int[] ira)
private void
bgSort(long[] ra, int nVec)
private void
cleanPrunedModes()
private void
cleanSelected()
private boolean
compareCutRes(int[][] in_cr1, int[][] in_cr2)
private void
computePilot(AlgorithmMeanShiftClustering.fams_hash_entry[][] HT, int[] hs, AlgorithmMeanShiftClustering.fams_cut[][] cuts, java.lang.String pilot_file_name)
private void
computeRealBandwidths(int h)
private void
computeScores(AlgorithmMeanShiftClustering.fams_hash_entry[][] HT, int[] hs, AlgorithmMeanShiftClustering.fams_cut[][] cuts, float[] scores)
private int
distL1(AlgorithmMeanShiftClustering.famsPoint inPt1, AlgorithmMeanShiftClustering.famsPoint inPt2)
private boolean
distL1Data(int[] in_d1, AlgorithmMeanShiftClustering.famsPoint in_pt2, double in_dist, double[] in_res)
private void
doFAMS(AlgorithmMeanShiftClustering.fams_hash_entry[][] HT, int[] hs, AlgorithmMeanShiftClustering.fams_cut[][] cuts, AlgorithmMeanShiftClustering.fams_hash_entry2[][] HT2, int[] hs2)
private long
doFindKLIteration(int K, int L, float[] scores)
private int
doMeanShiftAdaptiveIteration(AlgorithmMeanShiftClustering.fams_res_cont res, int[] old, int[] ret)
private void
evalCutRes(int[] in_dat, AlgorithmMeanShiftClustering.fams_cut[] in_part, int[] in_cut_res)
private void
evalCutRes(AlgorithmMeanShiftClustering.famsPoint in_pt, AlgorithmMeanShiftClustering.fams_cut[] in_part, int[] in_cut_res)
private int[]
findInHash(AlgorithmMeanShiftClustering.fams_hash_entry2[][] HT2, int[] hs2, int where, int which, int M2, int hjump)
float
getFloat(boolean bigEndian)
Reads four unsigned bytes from file.int
getInt(boolean bigEndian)
Reads four signed bytes from file.private void
getNearestNeighbours(AlgorithmMeanShiftClustering.famsPoint who, AlgorithmMeanShiftClustering.fams_hash_entry[][] HT, int[] hs, AlgorithmMeanShiftClustering.fams_cut[][] cuts, AlgorithmMeanShiftClustering.fams_res_cont res, int print, int[] num_l)
private int[]
getNearestNeighbours2H(int[] who, AlgorithmMeanShiftClustering.fams_hash_entry[][] HT, int[] hs, AlgorithmMeanShiftClustering.fams_cut[][] cuts, AlgorithmMeanShiftClustering.fams_res_cont res, int[][] solution, AlgorithmMeanShiftClustering.fams_hash_entry2[][] HT2, int[] hs2)
private int
getPrime(int minp)
private int
hashFunction(int[] cutVals, int offset, int whichPartition, int kk, int M, int[] hjump, int hOffset)
private void
initHash(int nk)
private void
insertIntoHash(AlgorithmMeanShiftClustering.fams_hash_entry2[][] HT2, int[] hs2, int where, int which, int[][] solution, int M, int hjump)
private boolean
loadBandwidths(java.lang.String fn)
private void
makeCutL(AlgorithmMeanShiftClustering.fams_cut[] cut)
private void
makeCuts(AlgorithmMeanShiftClustering.fams_cut[][] cuts)
private boolean
notEq(int[] in_d1, int[] in_d2)
private int
pruneModes(int hprune, int npmin)
void
runAlgorithm()
Actually runs the algorithm.private void
saveBandwidths(java.lang.String fn)
private void
saveModes(java.lang.String fn)
private void
savePrunedModes(java.lang.String fn)
private void
selectMSPoints(double percent, int jump)
private double
SQ(double x)
void
writeFloat(float data, boolean bigEndian)
Writes a float as four bytes to a file.void
writeInt(int data, boolean bigEndian)
Writes an int as four bytes to a file.-
Methods inherited from class gov.nih.mipav.model.algorithms.AlgorithmBase
actionPerformed, addListener, addProgressChangeListener, calculateImageSize, calculatePrincipleAxis, computeElapsedTime, computeElapsedTime, convertIntoFloat, delinkProgressToAlgorithm, delinkProgressToAlgorithmMulti, displayError, errorCleanUp, finalize, 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
-
EVERY_POINT
public static final int EVERY_POINT
- See Also:
- Constant Field Values
-
SELECT_ON_JUMP
public static final int SELECT_ON_JUMP
- See Also:
- Constant Field Values
-
SELECT_PERCENT
public static final int SELECT_PERCENT
- See Also:
- Constant Field Values
-
FAMS_FKL_NEL
private static final int FAMS_FKL_NEL
- See Also:
- Constant Field Values
-
FAMS_FKL_TIMES
private static final int FAMS_FKL_TIMES
- See Also:
- Constant Field Values
-
FAMS_MAX_K
private static final int FAMS_MAX_K
- See Also:
- Constant Field Values
-
FAMS_MAX_L
private static final int FAMS_MAX_L
- See Also:
- Constant Field Values
-
FAMS_BLOCKSIZE
private static final int FAMS_BLOCKSIZE
- See Also:
- Constant Field Values
-
FAMS_BLOCKSIZE2
private static final int FAMS_BLOCKSIZE2
- See Also:
- Constant Field Values
-
FAMS_MAXITER
private static final int FAMS_MAXITER
- See Also:
- Constant Field Values
-
FAMS_ALPHA
private static final double FAMS_ALPHA
- See Also:
- Constant Field Values
-
FAMS_PRUNE_WINDOW
private static final int FAMS_PRUNE_WINDOW
- See Also:
- Constant Field Values
-
FAMS_PRUNE_MINN
private static final int FAMS_PRUNE_MINN
- See Also:
- Constant Field Values
-
FAMS_PRUNE_MAXM
private static final int FAMS_PRUNE_MAXM
- See Also:
- Constant Field Values
-
FAMS_PRUNE_MAXP
private static final int FAMS_PRUNE_MAXP
- See Also:
- Constant Field Values
-
FAMS_PRUNE_HDIV
private static final int FAMS_PRUNE_HDIV
- See Also:
- Constant Field Values
-
Bs
private static final int Bs
- See Also:
- Constant Field Values
-
Bs2
private static final int Bs2
- See Also:
- Constant Field Values
-
FAMS_FLOAT_SHIFT
private static float FAMS_FLOAT_SHIFT
-
K
private int K
-
L
private int L
-
k_neigh
private int k_neigh
-
data_file_name
private java.lang.String data_file_name
-
input_directory
private java.lang.String input_directory
-
choosePoints
private int choosePoints
-
jump
private int jump
-
percent
private double percent
-
fixedWidth
private boolean fixedWidth
-
width
private float width
-
findOptimalKL
private boolean findOptimalKL
-
epsilon
private float epsilon
-
Kmin
private int Kmin
-
Kjump
private int Kjump
-
FAMS_DO_SPEEDUP
private boolean FAMS_DO_SPEEDUP
-
nPoints
private int nPoints
-
nDims
private int nDims
-
pttemp
private float[] pttemp
-
modesImage
private ModelImage modesImage
-
prunedModesImage
private ModelImage prunedModesImage
-
noLSH
private boolean noLSH
-
raFile
private java.io.RandomAccessFile raFile
-
byteIntBuffer
private final byte[] byteIntBuffer
byte array for int *
-
byteFloatBuffer
private final byte[] byteFloatBuffer
byte array for float *
-
endianess
private boolean endianess
-
array1
private int[] array1
-
minVal
private float minVal
-
maxVal
private float maxVal
-
points
private AlgorithmMeanShiftClustering.famsPoint[] points
-
data
private int[][] data
-
dataSize
private int dataSize
-
rr
double[] rr
-
psel
private int[] psel
-
nsel
private int nsel
-
modes
private int[][] modes
-
hmodes
private int[][] hmodes
-
npm
private int npm
-
prunedmodes
private int[][] prunedmodes
-
nprunedmodes
private int[] nprunedmodes
-
M
private int M
-
M2
private int M2
-
hashCoeffs
private int[] hashCoeffs
-
K_
private int K_
-
L_
private int L_
-
t_cut_res
private int[][] t_cut_res
-
t_old_cut_res
private int[][] t_old_cut_res
-
t_old_m
private int[] t_old_m
-
t_m
private int[] t_m
-
t_m2
private int[] t_m2
-
t_hjump
private int[] t_hjump
-
nnres1
private int nnres1
-
nnres2
private int nnres2
-
tt1
private long tt1
-
srand
private java.util.Random srand
-
-
Constructor Detail
-
AlgorithmMeanShiftClustering
public AlgorithmMeanShiftClustering(ModelImage image, int K, int L, int k_neigh, java.lang.String data_file_name, java.lang.String input_directory, int choosePoints, int jump, double percent, boolean fixedWidth, float width, boolean findOptimalKL, float epsilon, int Kmin, int Kjump, boolean FAMS_DO_SPEEDUP, int nPoints, int nDims, float[] pttemp, ModelImage modesImage, ModelImage prunedModesImage)
File formats: Files are all ASCII files. The input file has a one line header with two number_of_points dimension Then the input points are given as lines including d numbers. The pilot process produces the neighborhood size for each point If the input file is for example d040_1S.txt and the number of neighbors is k the pilot file will be pilot_d040_1S_k.txt If the file exists the program will assume that it was created by a previous run and use the information in it and will not rerun the pilot creation procedure. This enables the users to provide their own neighborhood sizes. The output files are for example: The result of MS on the selected points is in out_data_file_name.txt The result of joined modes is in modes_data_file_name.txt Example: We are given a file d040_1S.txt fams 30 46 200 d040_1S ./ -f 0.05 10 2 Finds K,L and optionally runs mean shift on all points. To run mean shift on same data with K=24 and L=35 fams 24 35 200 d040_1S ./ NOTES: - Internally the program scales the data between 0 and 2^16-1, therefore the precision is limited by range_of_data/2^16 - Running mean shift on all the points is not necessary when only the modes are needed - In the file fams.h there are several constants that influence the speed and accuracy of the program. For example you can set the bandwidth on which two modes are joined or the number of trials on which the test is run when finding K and L. Permission to port granted by Ilan Shimshoni: No problem. Good luck with that. I'll be happy to put your code also on my website if you like. Ilan Sent from my Cyanogen phone On Nov 23, 2016 9:02 PM, "Gandler, William (NIH/CIT) [E]"wrote: Dear Ilan Shimshoni, I am a Java programmer for the MIPAV imaging package at NIH working on mean shift. Could I have permission to port your Adaptive mean shift based clustering C++ code to Java for use in MIPAV? You will receive full acknowledgement for all code ported. Sincerely, William Gandler
-
-
Method Detail
-
runAlgorithm
public void runAlgorithm()
Description copied from class:AlgorithmBase
Actually runs the algorithm. Implemented by inheriting algorithms.- Specified by:
runAlgorithm
in classAlgorithmBase
-
savePrunedModes
private void savePrunedModes(java.lang.String fn)
-
saveModes
private void saveModes(java.lang.String fn)
-
pruneModes
private int pruneModes(int hprune, int npmin)
-
cleanPrunedModes
private void cleanPrunedModes()
-
bgISort
void bgISort(int[] ra, int nVec, int[] ira)
-
doFAMS
private void doFAMS(AlgorithmMeanShiftClustering.fams_hash_entry[][] HT, int[] hs, AlgorithmMeanShiftClustering.fams_cut[][] cuts, AlgorithmMeanShiftClustering.fams_hash_entry2[][] HT2, int[] hs2)
-
doMeanShiftAdaptiveIteration
private int doMeanShiftAdaptiveIteration(AlgorithmMeanShiftClustering.fams_res_cont res, int[] old, int[] ret)
-
SQ
private double SQ(double x)
-
distL1Data
private boolean distL1Data(int[] in_d1, AlgorithmMeanShiftClustering.famsPoint in_pt2, double in_dist, double[] in_res)
-
getNearestNeighbours2H
private int[] getNearestNeighbours2H(int[] who, AlgorithmMeanShiftClustering.fams_hash_entry[][] HT, int[] hs, AlgorithmMeanShiftClustering.fams_cut[][] cuts, AlgorithmMeanShiftClustering.fams_res_cont res, int[][] solution, AlgorithmMeanShiftClustering.fams_hash_entry2[][] HT2, int[] hs2)
-
insertIntoHash
private void insertIntoHash(AlgorithmMeanShiftClustering.fams_hash_entry2[][] HT2, int[] hs2, int where, int which, int[][] solution, int M, int hjump)
-
findInHash
private int[] findInHash(AlgorithmMeanShiftClustering.fams_hash_entry2[][] HT2, int[] hs2, int where, int which, int M2, int hjump)
-
notEq
private boolean notEq(int[] in_d1, int[] in_d2)
-
computePilot
private void computePilot(AlgorithmMeanShiftClustering.fams_hash_entry[][] HT, int[] hs, AlgorithmMeanShiftClustering.fams_cut[][] cuts, java.lang.String pilot_file_name)
-
loadBandwidths
private boolean loadBandwidths(java.lang.String fn)
-
saveBandwidths
private void saveBandwidths(java.lang.String fn)
-
getInt
public final int getInt(boolean bigEndian) throws java.io.IOException
Reads four signed bytes from file.- Parameters:
bigEndian
-true
indicates big endian byte order,false
indicates little endian.- Returns:
- The value of the integer read from the file.
- Throws:
java.io.IOException
- if there is an error reading the file
-
getFloat
public final float getFloat(boolean bigEndian) throws java.io.IOException
Reads four unsigned bytes from file.- Parameters:
bigEndian
-true
indicates big endian byte order,false
indicates little endian.- Returns:
- The value of the float read from the file.
- Throws:
java.io.IOException
- if there is an error reading the file
-
writeInt
public final void writeInt(int data, boolean bigEndian) throws java.io.IOException
Writes an int as four bytes to a file.- Parameters:
data
- Data to be written to file.bigEndian
-true
indicates big endian byte order,false
indicates little endian.- Throws:
java.io.IOException
- if there is an error writing the file
-
writeFloat
public final void writeFloat(float data, boolean bigEndian) throws java.io.IOException
Writes a float as four bytes to a file.- Parameters:
data
- Data to be written to file.bigEndian
-true
indicates big endian byte order,false
indicates little endian.- Throws:
java.io.IOException
- if there is an error writing the file
-
bgSort
private void bgSort(long[] ra, int nVec)
-
selectMSPoints
private void selectMSPoints(double percent, int jump)
-
computeRealBandwidths
private void computeRealBandwidths(int h)
-
doFindKLIteration
private long doFindKLIteration(int K, int L, float[] scores)
-
computeScores
private void computeScores(AlgorithmMeanShiftClustering.fams_hash_entry[][] HT, int[] hs, AlgorithmMeanShiftClustering.fams_cut[][] cuts, float[] scores)
-
getNearestNeighbours
private void getNearestNeighbours(AlgorithmMeanShiftClustering.famsPoint who, AlgorithmMeanShiftClustering.fams_hash_entry[][] HT, int[] hs, AlgorithmMeanShiftClustering.fams_cut[][] cuts, AlgorithmMeanShiftClustering.fams_res_cont res, int print, int[] num_l)
-
addDataToRes
private void addDataToRes(AlgorithmMeanShiftClustering.fams_hash_entry[][] HT, int[] hs, AlgorithmMeanShiftClustering.fams_res_cont res, int where, int Bs, int M, int which, int nnres, int which2, int hjump)
-
compareCutRes
private boolean compareCutRes(int[][] in_cr1, int[][] in_cr2)
-
addDataToHash
private void addDataToHash(AlgorithmMeanShiftClustering.fams_hash_entry[][] HT, int[] hs, AlgorithmMeanShiftClustering.famsPoint pt, int where, int Bs, int M, int which, int which2, int hjump)
-
hashFunction
private int hashFunction(int[] cutVals, int offset, int whichPartition, int kk, int M, int[] hjump, int hOffset)
-
evalCutRes
private void evalCutRes(AlgorithmMeanShiftClustering.famsPoint in_pt, AlgorithmMeanShiftClustering.fams_cut[] in_part, int[] in_cut_res)
-
evalCutRes
private void evalCutRes(int[] in_dat, AlgorithmMeanShiftClustering.fams_cut[] in_part, int[] in_cut_res)
-
makeCuts
private void makeCuts(AlgorithmMeanShiftClustering.fams_cut[][] cuts)
-
makeCutL
private void makeCutL(AlgorithmMeanShiftClustering.fams_cut[] cut)
-
initHash
private void initHash(int nk)
-
getPrime
private int getPrime(int minp)
-
distL1
private int distL1(AlgorithmMeanShiftClustering.famsPoint inPt1, AlgorithmMeanShiftClustering.famsPoint inPt2)
-
cleanSelected
private void cleanSelected()
-
-