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:
ActionListener,WindowListener,Runnable,EventListener
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 ClassesModifier and TypeClassDescriptionprivate classprivate classprivate classprivate classprivate classNested classes/interfaces inherited from class java.lang.Thread
Thread.Builder, Thread.State, Thread.UncaughtExceptionHandler -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate int[]private static final intprivate static final intprivate final byte[]byte array for float *private final byte[]byte array for int *private intprivate int[][]private Stringprivate intprivate booleanprivate floatstatic final intprivate static final doubleprivate static final intprivate static final intprivate booleanprivate static final intprivate static final intprivate static floatprivate static final intprivate static final intprivate static final intprivate static final intprivate static final intprivate static final intprivate static final intprivate static final intprivate booleanprivate booleanprivate int[]private int[][]private Stringprivate intprivate intprivate intprivate intprivate intprivate intprivate intprivate intprivate intprivate intprivate floatprivate floatprivate int[][]private ModelImageprivate intprivate intprivate intprivate booleanprivate intprivate intprivate int[]private intprivate doubleprivate AlgorithmMeanShiftClustering.famsPoint[]private int[][]private ModelImageprivate int[]private float[]private RandomAccessFile(package private) double[]static final intstatic final intprivate Randomprivate int[][]private int[]private int[]private int[]private int[][]private int[]private longprivate floatFields inherited from class gov.nih.mipav.model.algorithms.AlgorithmBase
destFlag, destImage, image25D, mask, maxProgressValue, minProgressValue, multiThreadingEnabled, nthreads, progress, progressModulus, progressStep, runningInSeparateThread, separable, srcImage, threadStoppedFields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY -
Constructor Summary
ConstructorsConstructorDescriptionAlgorithmMeanShiftClustering(ModelImage image, int K, int L, int k_neigh, String data_file_name, 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
Modifier and TypeMethodDescriptionprivate voidaddDataToHash(AlgorithmMeanShiftClustering.fams_hash_entry[][] HT, int[] hs, AlgorithmMeanShiftClustering.famsPoint pt, int where, int Bs, int M, int which, int which2, int hjump) private voidaddDataToRes(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) voidbgISort(int[] ra, int nVec, int[] ira) private voidbgSort(long[] ra, int nVec) private voidprivate voidprivate booleancompareCutRes(int[][] in_cr1, int[][] in_cr2) private voidcomputePilot(AlgorithmMeanShiftClustering.fams_hash_entry[][] HT, int[] hs, AlgorithmMeanShiftClustering.fams_cut[][] cuts, String pilot_file_name) private voidcomputeRealBandwidths(int h) private voidcomputeScores(AlgorithmMeanShiftClustering.fams_hash_entry[][] HT, int[] hs, AlgorithmMeanShiftClustering.fams_cut[][] cuts, float[] scores) private intprivate booleandistL1Data(int[] in_d1, AlgorithmMeanShiftClustering.famsPoint in_pt2, double in_dist, double[] in_res) private voiddoFAMS(AlgorithmMeanShiftClustering.fams_hash_entry[][] HT, int[] hs, AlgorithmMeanShiftClustering.fams_cut[][] cuts, AlgorithmMeanShiftClustering.fams_hash_entry2[][] HT2, int[] hs2) private longdoFindKLIteration(int K, int L, float[] scores) private intdoMeanShiftAdaptiveIteration(AlgorithmMeanShiftClustering.fams_res_cont res, int[] old, int[] ret) private voidevalCutRes(int[] in_dat, AlgorithmMeanShiftClustering.fams_cut[] in_part, int[] in_cut_res) private voidevalCutRes(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) final floatgetFloat(boolean bigEndian) Reads four unsigned bytes from file.final intgetInt(boolean bigEndian) Reads four signed bytes from file.private voidgetNearestNeighbours(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 intgetPrime(int minp) private inthashFunction(int[] cutVals, int offset, int whichPartition, int kk, int M, int[] hjump, int hOffset) private voidinitHash(int nk) private voidinsertIntoHash(AlgorithmMeanShiftClustering.fams_hash_entry2[][] HT2, int[] hs2, int where, int which, int[][] solution, int M, int hjump) private booleanloadBandwidths(String fn) private voidprivate voidprivate booleannotEq(int[] in_d1, int[] in_d2) private intpruneModes(int hprune, int npmin) voidActually runs the algorithm.private voidsaveBandwidths(String fn) private voidprivate voidprivate voidselectMSPoints(double percent, int jump) private doubleSQ(double x) final voidwriteFloat(float data, boolean bigEndian) Writes a float as four bytes to a file.final voidwriteInt(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, windowOpenedMethods 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, isVirtual, join, join, join, join, ofPlatform, ofVirtual, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, sleep, start, startVirtualThread, stop, suspend, threadId, toString, yield
-
Field Details
-
EVERY_POINT
public static final int EVERY_POINT- See Also:
-
SELECT_ON_JUMP
public static final int SELECT_ON_JUMP- See Also:
-
SELECT_PERCENT
public static final int SELECT_PERCENT- See Also:
-
FAMS_FKL_NEL
private static final int FAMS_FKL_NEL- See Also:
-
FAMS_FKL_TIMES
private static final int FAMS_FKL_TIMES- See Also:
-
FAMS_MAX_K
private static final int FAMS_MAX_K- See Also:
-
FAMS_MAX_L
private static final int FAMS_MAX_L- See Also:
-
FAMS_BLOCKSIZE
private static final int FAMS_BLOCKSIZE- See Also:
-
FAMS_BLOCKSIZE2
private static final int FAMS_BLOCKSIZE2- See Also:
-
FAMS_MAXITER
private static final int FAMS_MAXITER- See Also:
-
FAMS_ALPHA
private static final double FAMS_ALPHA- See Also:
-
FAMS_PRUNE_WINDOW
private static final int FAMS_PRUNE_WINDOW- See Also:
-
FAMS_PRUNE_MINN
private static final int FAMS_PRUNE_MINN- See Also:
-
FAMS_PRUNE_MAXM
private static final int FAMS_PRUNE_MAXM- See Also:
-
FAMS_PRUNE_MAXP
private static final int FAMS_PRUNE_MAXP- See Also:
-
FAMS_PRUNE_HDIV
private static final int FAMS_PRUNE_HDIV- See Also:
-
Bs
private static final int Bs- See Also:
-
Bs2
private static final int Bs2- See Also:
-
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
-
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
-
prunedModesImage
-
noLSH
private boolean noLSH -
raFile
-
byteIntBuffer
private final byte[] byteIntBufferbyte array for int * -
byteFloatBuffer
private final byte[] byteFloatBufferbyte array for float * -
endianess
private boolean endianess -
array1
private int[] array1 -
minVal
private float minVal -
maxVal
private float maxVal -
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
-
-
Constructor Details
-
AlgorithmMeanShiftClustering
public AlgorithmMeanShiftClustering(ModelImage image, int K, int L, int k_neigh, String data_file_name, 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]" invalid input: '<'ilb@mail.nih.gov> 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 Details
-
runAlgorithm
public void runAlgorithm()Description copied from class:AlgorithmBaseActually runs the algorithm. Implemented by inheriting algorithms.- Specified by:
runAlgorithmin classAlgorithmBase
-
savePrunedModes
-
saveModes
-
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, String pilot_file_name) -
loadBandwidths
-
saveBandwidths
-
getInt
Reads four signed bytes from file.- Parameters:
bigEndian-trueindicates big endian byte order,falseindicates little endian.- Returns:
- The value of the integer read from the file.
- Throws:
IOException- if there is an error reading the file
-
getFloat
Reads four unsigned bytes from file.- Parameters:
bigEndian-trueindicates big endian byte order,falseindicates little endian.- Returns:
- The value of the float read from the file.
- Throws:
IOException- if there is an error reading the file
-
writeInt
Writes an int as four bytes to a file.- Parameters:
data- Data to be written to file.bigEndian-trueindicates big endian byte order,falseindicates little endian.- Throws:
IOException- if there is an error writing the file
-
writeFloat
Writes a float as four bytes to a file.- Parameters:
data- Data to be written to file.bigEndian-trueindicates big endian byte order,falseindicates little endian.- Throws:
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
-
makeCutL
-
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()
-