Esempio n. 1
0
  public void test01() throws java.lang.Exception {
    final File imageFile = new File("test/data/clown24.png");
    final double tolerance = 0.01;
    final double[][] expectedCenters = {
      {182.389, 108.690, 45.733},
      {224.589, 187.087, 151.137},
      {115.623, 51.116, 20.329},
      {30.557, 10.600, 5.617},
    };

    assertTrue("File exists", imageFile.exists());

    // Read test image
    final ImagePlus imp = IOUtils.openImage(imageFile);

    if (imp.getType() != ImagePlus.COLOR_RGB) {
      throw new java.lang.Exception("Expecting color image.");
    }

    // Convert RGB to a stack
    final ImageConverter ic = new ImageConverter(imp);
    ic.convertToRGBStack();
    final StackConverter sc = new StackConverter(imp);
    sc.convertToGray32();

    final KMeans.Config config = new KMeans.Config();
    config.setNumberOfClusters(4);
    config.setRandomizationSeedEnabled(true);
    config.setRandomizationSeed(31415);
    final KMeans kmeans = new KMeans(config);
    final ImageProcessor ip = kmeans.run(imp.getStack());
    assertNotNull(ip);

    float[][] centers = kmeans.getClusterCenters();
    for (int i = 0; i < centers.length; i++) {
      for (int j = 0; j < centers[i].length; j++) {
        assertEquals(
            "center[" + i + "][" + j + "]", expectedCenters[i][j], centers[i][j], tolerance);
      }
    }

    //        final ImagePlus imp1 = new ImagePlus("K-means", ip);
    //        final FileSaver saver = new FileSaver(imp1);
    //        saver.saveAsTiff("kmeans-output.tif");
  }
Esempio n. 2
0
  /**
   * Creates an @link{ImageStack} by converting an @link{ImagePlus} object Duplicate of a method
   * in @link{KmeansClusteringPlugin}
   *
   * @param src the @link{ImagePlus} object to convert
   * @return an @link{ImageStack} object containing the
   */
  private static ImageStack convertToFloatStack(final ImagePlus src) {

    final ImagePlus imp = duplicate(src);

    // Remember scaling setup
    final boolean doScaling = ImageConverter.getDoScaling();

    try {
      // Disable scaling
      ImageConverter.setDoScaling(false);

      if (imp.getType() == ImagePlus.COLOR_RGB) {
        if (imp.getStackSize() > 1) {
          throw new RuntimeException("Unsupported image type: stack of COLOR_RGB");
        }

        final ImageConverter converter = new ImageConverter(imp);
        converter.convertToRGBStack();
      }

      if (imp.getStackSize() > 1) {
        final StackConverter converter = new StackConverter(imp);
        converter.convertToGray32();
      } else {
        final ImageConverter converter = new ImageConverter(imp);
        converter.convertToGray32();
      }

      // FIXME: make sure that there are no memory leaks
      //            imp.flush();
      return imp.getStack();

    } finally {
      // Restore original scaling option
      ImageConverter.setDoScaling(doScaling);
    }
  }