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 classAlgorithmMeanShiftClustering.fams_cutprivate classAlgorithmMeanShiftClustering.fams_hash_entryprivate classAlgorithmMeanShiftClustering.fams_hash_entry2private classAlgorithmMeanShiftClustering.fams_res_contprivate classAlgorithmMeanShiftClustering.famsPoint
-
Field Summary
Fields Modifier and Type Field Description private int[]array1private static intBsprivate static intBs2private byte[]byteFloatBufferbyte array for float *private byte[]byteIntBufferbyte array for int *private intchoosePointsprivate int[][]dataprivate java.lang.Stringdata_file_nameprivate intdataSizeprivate booleanendianessprivate floatepsilonstatic intEVERY_POINTprivate static doubleFAMS_ALPHAprivate static intFAMS_BLOCKSIZEprivate static intFAMS_BLOCKSIZE2private booleanFAMS_DO_SPEEDUPprivate static intFAMS_FKL_NELprivate static intFAMS_FKL_TIMESprivate static floatFAMS_FLOAT_SHIFTprivate static intFAMS_MAX_Kprivate static intFAMS_MAX_Lprivate static intFAMS_MAXITERprivate static intFAMS_PRUNE_HDIVprivate static intFAMS_PRUNE_MAXMprivate static intFAMS_PRUNE_MAXPprivate static intFAMS_PRUNE_MINNprivate static intFAMS_PRUNE_WINDOWprivate booleanfindOptimalKLprivate booleanfixedWidthprivate int[]hashCoeffsprivate int[][]hmodesprivate java.lang.Stringinput_directoryprivate intjumpprivate intKprivate intK_private intk_neighprivate intKjumpprivate intKminprivate intLprivate intL_private intMprivate intM2private floatmaxValprivate floatminValprivate int[][]modesprivate ModelImagemodesImageprivate intnDimsprivate intnnres1private intnnres2private booleannoLSHprivate intnpmprivate intnPointsprivate int[]nprunedmodesprivate intnselprivate doublepercentprivate AlgorithmMeanShiftClustering.famsPoint[]pointsprivate int[][]prunedmodesprivate ModelImageprunedModesImageprivate int[]pselprivate float[]pttempprivate java.io.RandomAccessFileraFile(package private) double[]rrstatic intSELECT_ON_JUMPstatic intSELECT_PERCENTprivate java.util.Randomsrandprivate int[][]t_cut_resprivate int[]t_hjumpprivate int[]t_mprivate int[]t_m2private int[][]t_old_cut_resprivate int[]t_old_mprivate longtt1private floatwidth-
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 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 voidcleanPrunedModes()private voidcleanSelected()private booleancompareCutRes(int[][] in_cr1, int[][] in_cr2)private voidcomputePilot(AlgorithmMeanShiftClustering.fams_hash_entry[][] HT, int[] hs, AlgorithmMeanShiftClustering.fams_cut[][] cuts, java.lang.String pilot_file_name)private voidcomputeRealBandwidths(int h)private voidcomputeScores(AlgorithmMeanShiftClustering.fams_hash_entry[][] HT, int[] hs, AlgorithmMeanShiftClustering.fams_cut[][] cuts, float[] scores)private intdistL1(AlgorithmMeanShiftClustering.famsPoint inPt1, AlgorithmMeanShiftClustering.famsPoint inPt2)private 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)floatgetFloat(boolean bigEndian)Reads four unsigned bytes from file.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(java.lang.String fn)private voidmakeCutL(AlgorithmMeanShiftClustering.fams_cut[] cut)private voidmakeCuts(AlgorithmMeanShiftClustering.fams_cut[][] cuts)private booleannotEq(int[] in_d1, int[] in_d2)private intpruneModes(int hprune, int npmin)voidrunAlgorithm()Actually runs the algorithm.private voidsaveBandwidths(java.lang.String fn)private voidsaveModes(java.lang.String fn)private voidsavePrunedModes(java.lang.String fn)private voidselectMSPoints(double percent, int jump)private doubleSQ(double x)voidwriteFloat(float data, boolean bigEndian)Writes a float as four bytes to a file.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, 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:AlgorithmBaseActually runs the algorithm. Implemented by inheriting algorithms.- Specified by:
runAlgorithmin 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.IOExceptionReads 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:
java.io.IOException- if there is an error reading the file
-
getFloat
public final float getFloat(boolean bigEndian) throws java.io.IOExceptionReads 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:
java.io.IOException- if there is an error reading the file
-
writeInt
public final void writeInt(int data, boolean bigEndian) throws java.io.IOExceptionWrites 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:
java.io.IOException- if there is an error writing the file
-
writeFloat
public final void writeFloat(float data, boolean bigEndian) throws java.io.IOExceptionWrites 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:
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()
-
-