Added method for generating default centroids for an image.
authormccreedy@NIH.GOV <mccreedy@NIH.GOV@ba61647d-9d00-f842-95cd-605cb4296b96>
Mon, 5 Feb 2018 16:39:23 +0000 (16:39 +0000)
committermccreedy@NIH.GOV <mccreedy@NIH.GOV@ba61647d-9d00-f842-95cd-605cb4296b96>
Mon, 5 Feb 2018 16:39:23 +0000 (16:39 +0000)
git-svn-id: https://citdcbmipav.cit.nih.gov/repos-pub/mipav/trunk@15359 ba61647d-9d00-f842-95cd-605cb4296b96

mipav/src/gov/nih/mipav/view/dialogs/JDialogFuzzyCMeans.java

index 8b29bf0..47e8d61 100644 (file)
@@ -10,6 +10,7 @@ import gov.nih.mipav.view.*;
 \r
 import java.awt.*;\r
 import java.awt.event.*;\r
+import java.util.BitSet;\r
 \r
 import javax.swing.*;\r
 \r
@@ -544,6 +545,184 @@ public class JDialogFuzzyCMeans extends JDialogScriptableBase implements Algorit
         scriptParameters.getParams().put(ParameterFactory.newParameter("segmentation_type", segmentation));\r
         scriptParameters.getParams().put(ParameterFactory.newParameter("centroids", centroids));\r
     }\r
+    \r
+    public static float[] getDefaultCentroids(ModelImage image, int nClasses, boolean regionFlag, BitSet regionMask, boolean cropBackground, float threshold) {\r
+        int i;\r
+        float minimum, maximum;\r
+        int xDim = image.getExtents()[0];\r
+        int yDim = image.getExtents()[1];\r
+        int zDim;\r
+\r
+        if (image.getNDims() > 2) {\r
+            zDim = image.getExtents()[2];\r
+        } else {\r
+            zDim = 1;\r
+        }\r
+\r
+        int sliceSize = xDim * yDim;\r
+        int volSize = xDim * yDim * zDim;\r
+        float[] buffer = null;\r
+        int yStepIn, yStepOut, zStepIn, zStepOut;\r
+        int x, y, z, index, newXDim, newYDim, newZDim, newSliceSize;\r
+\r
+        try {\r
+            buffer = new float[volSize];\r
+            image.exportData(0, volSize, buffer);\r
+\r
+            image.calcMinMax();\r
+            minimum = (float) image.getMin();\r
+            maximum = (float) image.getMax();\r
+\r
+            if (!regionFlag) {\r
+                maximum = -Float.MAX_VALUE;\r
+                minimum = Float.MAX_VALUE;\r
+\r
+                for (i = 0; i < volSize; i++) {\r
+                    if (regionMask.get(i)) {\r
+\r
+                        if (buffer[i] > maximum) {\r
+                            maximum = buffer[i];\r
+                        }\r
+\r
+                        if (buffer[i] < minimum) {\r
+                            minimum = buffer[i];\r
+                        }\r
+                    }\r
+                }\r
+            } // if (!wholeImage)\r
+\r
+            int xLow = 0;\r
+            int yLow = 0;\r
+            int zLow = 0;\r
+            int xHigh = xDim - 1;\r
+            int yHigh = yDim - 1;\r
+            int zHigh = zDim - 1;\r
+\r
+            if (cropBackground) {\r
+\r
+                // Find the smallest bounding box for the data\r
+                xLow = xDim - 1;\r
+                yLow = yDim - 1;\r
+                zLow = zDim - 1;\r
+                xHigh = 0;\r
+                yHigh = 0;\r
+                zHigh = 0;\r
+\r
+                for (z = 0; z < zDim; z++) {\r
+                    zStepIn = z * sliceSize;\r
+\r
+                    for (y = 0; y < yDim; y++) {\r
+                        yStepIn = (y * xDim) + zStepIn;\r
+\r
+                        for (x = 0; x < xDim; x++) {\r
+                            index = x + yStepIn;\r
+\r
+                            if (buffer[index] >= threshold) {\r
+\r
+                                if (x < xLow) {\r
+                                    xLow = x;\r
+                                }\r
+\r
+                                if (x > xHigh) {\r
+                                    xHigh = x;\r
+                                }\r
+\r
+                                if (y < yLow) {\r
+                                    yLow = y;\r
+                                }\r
+\r
+                                if (y > yHigh) {\r
+                                    yHigh = y;\r
+                                }\r
+\r
+                                if (z < zLow) {\r
+                                    zLow = z;\r
+                                }\r
+\r
+                                if (z > zHigh) {\r
+                                    zHigh = z;\r
+                                }\r
+                            } // if (buffer[index] > threshold)\r
+                        } // for (x = 0; x < xDim; x++)\r
+                    } // for (y = 0; y < yDim; y++)\r
+                } // for (z = 0; z < zDim; z++)\r
+\r
+                if ((xLow > 0) || (xHigh < (xDim - 1)) || (yLow > 0) || (yHigh < (yDim - 1)) || (zLow > 0) ||\r
+                        (zHigh < (zDim - 1))) {\r
+\r
+                    // A smaller bounding box has been found for the data\r
+                    // Recopy area to smaller data array to save space\r
+                    newXDim = xHigh - xLow + 1;\r
+                    newYDim = yHigh - yLow + 1;\r
+                    newZDim = zHigh - zLow + 1;\r
+\r
+                    float[] buffer2 = new float[newXDim * newYDim * newZDim];\r
+                    newSliceSize = newXDim * newYDim;\r
+\r
+                    for (z = zLow; z <= zHigh; z++) {\r
+                        zStepOut = z * sliceSize;\r
+                        zStepIn = ((z - zLow) * newSliceSize) - xLow - (yLow * newXDim);\r
+\r
+                        for (y = yLow; y <= yHigh; y++) {\r
+                            yStepIn = (y * newXDim) + zStepIn;\r
+                            yStepOut = (y * xDim) + zStepOut;\r
+\r
+                            for (x = xLow; x <= xHigh; x++) {\r
+                                buffer2[x + yStepIn] = buffer[x + yStepOut];\r
+                            } // for (x = xLow; x <= xHigh; x++)\r
+                        } // for (y = yLow; y <= yHigh; y++)\r
+                    } // for (z = zLow; z <= zHigh; z++)\r
+\r
+                    xDim = newXDim;\r
+                    yDim = newYDim;\r
+                    zDim = newZDim;\r
+                    sliceSize = xDim * yDim;\r
+                    volSize = sliceSize * zDim;\r
+                    buffer = new float[volSize];\r
+\r
+                    for (i = 0; i < sliceSize; i++) {\r
+                        buffer[i] = buffer2[i];\r
+                    }\r
+\r
+                    buffer2 = null;\r
+\r
+                    // Find the new minimum\r
+                    minimum = maximum;\r
+\r
+                    for (i = 0; i < volSize; i++) {\r
+\r
+                        if (buffer[i] < minimum) {\r
+                            minimum = buffer[i];\r
+                        } // if (buffer[i] < minimum)\r
+                    } // for (i = 0; i < sliceSize; i++)\r
+                } // if ((xLow > 0) || (xHigh < (xDim-1)) || (yLow > 0) || (yHigh < (yDim - 1)))\r
+            } // if (cropBackground)\r
+        } catch (java.io.IOException ioe) {\r
+            buffer = null;\r
+            System.gc();\r
+            MipavUtil.displayError("Error trying to get centroids.");\r
+\r
+            return null;\r
+        } catch (OutOfMemoryError error) {\r
+            buffer = null;\r
+            System.gc();\r
+            MipavUtil.displayError("Algorithm FuzzyCMeans reports:\n" + error.toString());\r
+\r
+            return null;\r
+        }\r
+\r
+        buffer = null;\r
+        System.gc();\r
+\r
+        // Autodetect initial centroids\r
+        float[] centroids = new float[nClasses];\r
+        \r
+        for (i = 0; i < nClasses; i++) {\r
+            centroids[i] = minimum + ((maximum - minimum) * (i + 1) / (nClasses + 1));\r
+        }\r
+        \r
+        return centroids;\r
+    }\r
 \r
     /**\r
      * Gets the minimum and maximum of each image and initializes the centroids dialog appropriately.\r