Ejemplo n.º 1
0
  /**
   * Calculate the Rand index between some 2D original labels and the corresponding proposed labels.
   * Both image are binarized. We follow the definition of Rand index as described by William M.
   * Rand \cite{Rand71}.
   *
   * <p>BibTeX:
   *
   * <pre>
   * &#64;article{Rand71,
   *   author    = {William M. Rand},
   *   title     = {Objective criteria for the evaluation of clustering methods},
   *   journal   = {Journal of the American Statistical Association},
   *   year      = {1971},
   *   volume    = {66},
   *   number    = {336},
   *   pages     = {846--850},
   *   doi       = {10.2307/2284239)
   * }
   * </pre>
   *
   * @param label 2D image with the original labels
   * @param proposal 2D image with the proposed labels
   * @param binaryThreshold threshold value to binarize the input images
   * @return rand index value and derived statistics
   */
  public ClassificationStatistics randIndexStats(
      ImageProcessor label, ImageProcessor proposal, double binaryThreshold) {
    // Binarize inputs
    ByteProcessor binaryLabel = new ByteProcessor(label.getWidth(), label.getHeight());
    ByteProcessor binaryProposal = new ByteProcessor(label.getWidth(), label.getHeight());

    for (int x = 0; x < label.getWidth(); x++)
      for (int y = 0; y < label.getHeight(); y++) {
        binaryLabel.set(x, y, label.getPixelValue(x, y) > binaryThreshold ? 255 : 0);
        binaryProposal.set(x, y, proposal.getPixelValue(x, y) > binaryThreshold ? 255 : 0);
      }

    // Find components
    ShortProcessor components1 =
        (ShortProcessor)
            Utils.connectedComponents(new ImagePlus("binary labels", binaryLabel), 4)
                .allRegions
                .getProcessor();

    ShortProcessor components2 =
        (ShortProcessor)
            Utils.connectedComponents(new ImagePlus("proposal labels", binaryProposal), 4)
                .allRegions
                .getProcessor();

    return getRandIndexStats(components1, components2);
  }
Ejemplo n.º 2
0
  public void run(ImageProcessor image) {
    /**
     * ********************************************************************************* Initial
     * phase *********************************************************************************
     */
    int width = image.getWidth();
    int heigh = image.getHeight();
    ByteProcessor bp = Service.getByteProcessor(image);

    /**
     * ********************************************************************************* Convolve
     * with LoG kernel
     * *********************************************************************************
     */
    Convolver convolver = new Convolver();
    convolver.setNormalize(false);
    convolver.convolve(bp, log, (int) Math.sqrt(log.length), (int) Math.sqrt(log.length));
    ImagePlus outImg = new ImagePlus("Later Log kernel", bp);
    outImg.show();

    /**
     * ********************************************************************************* threshold
     * *********************************************************************************
     */
    ByteProcessor bpThreshold = Service.getByteProcessor(bp);
    int lut[] = new int[256];
    int i = 0;
    for (; i < threshold; i++) lut[i] = 0;
    for (int j = i; j < lut.length; j++) lut[j] = 255;
    bpThreshold.applyTable(lut);
    ImagePlus outImg1 = new ImagePlus("Later threshold phase", bpThreshold);
    outImg1.show();

    /**
     * ********************************************************************************* Find Zero
     * crossing *********************************************************************************
     */
    ByteProcessor out = new ByteProcessor(width, heigh);
    for (i = 0; i < width; i++) for (int j = 0; j < heigh; j++) out.set(i, j, 255);
    for (int x = 0; x < width - 1; x++) {
      for (int y = 0; y < heigh - 1; y++) {
        if (bpThreshold.get(x, y) != bpThreshold.get(x, y + 1)) out.set(x, y, 0);
        if (bpThreshold.get(x, y) != bpThreshold.get(x + 1, y)) out.set(x, y, 0);
        if (bpThreshold.get(x, y) != bpThreshold.get(x + 1, y + 1)) out.set(x, y, 0);
      }
    }
    ImagePlus outImg2 = new ImagePlus("Edge Find", out);
    outImg2.show();
  }
  @Test
  public final void testDistanceMap_UntilCorners_Weights23() {
    ByteProcessor image = new ByteProcessor(7, 7);
    image.setValue(255);
    image.fill();
    image.set(4, 4, 0);

    short[] weights = ChamferWeights.WEIGHTS_23.getShortWeights();
    DistanceTransform5x5Short algo = new DistanceTransform5x5Short(weights, false);
    ImageProcessor result = algo.distanceMap(image);

    assertNotNull(result);
    assertEquals(image.getWidth(), result.getWidth());
    assertEquals(image.getHeight(), result.getHeight());
    assertEquals(12, result.get(0, 0));
    assertEquals(10, result.get(6, 0));
    assertEquals(10, result.get(0, 6));
    assertEquals(6, result.get(6, 6));

    assertEquals(9, result.get(0, 5));
  }
  /** Another test for chessknight weigths, to fix a bug that incorrectly checked image bounds. */
  @Test
  public final void testDistanceMap_UntilCorners_ChessKnight2() {
    ByteProcessor image = new ByteProcessor(9, 9);
    image.setValue(255);
    image.fill();
    image.set(6, 6, 0);

    short[] weights = ChamferWeights.CHESSKNIGHT.getShortWeights();
    DistanceTransform5x5Short algo = new DistanceTransform5x5Short(weights, false);
    ImageProcessor result = algo.distanceMap(image);

    assertNotNull(result);
    assertEquals(image.getWidth(), result.getWidth());
    assertEquals(image.getHeight(), result.getHeight());
    assertEquals(42, result.get(0, 0));
    assertEquals(32, result.get(8, 0));
    assertEquals(32, result.get(0, 8));
    assertEquals(14, result.get(8, 8));

    assertEquals(30, result.get(0, 6));
  }
  @Test
  public final void testDistanceMap_ChessBoard() {
    ByteProcessor image = new ByteProcessor(12, 10);
    image.setBackgroundValue(0);
    image.setValue(0);
    image.fill();
    for (int y = 2; y < 8; y++) {
      for (int x = 2; x < 10; x++) {
        image.set(x, y, 255);
      }
    }

    short[] weights = ChamferWeights.CHESSBOARD.getShortWeights();
    DistanceTransform5x5Short algo = new DistanceTransform5x5Short(weights, true);
    ImageProcessor result = algo.distanceMap(image);

    assertNotNull(result);
    assertEquals(image.getWidth(), result.getWidth());
    assertEquals(image.getHeight(), result.getHeight());
    assertEquals(3, result.get(4, 4));
  }
Ejemplo n.º 6
0
  public void run(ImageProcessor ip) {
    ImageUtils.initMMorph4J();

    if (ip instanceof ByteProcessor) {
      GrayScaleImage img = ImageJAdapter.toGrayScaleImage((ByteProcessor) ip);

      Object obj[] = BuilderTreeOfShapeByUnionFindParallel.getImageInterpolate(img);
      short interpolation0[] = (short[]) obj[0];
      short interpolation1[] = (short[]) obj[1];

      int interpWidth = (img.getWidth() * 4 - 3);
      int interpHeight = (img.getHeight() * 4 - 3);

      ByteProcessor imgOut0 = new ByteProcessor(interpWidth, interpHeight);
      ByteProcessor imgOut1 = new ByteProcessor(interpWidth, interpHeight);

      for (int i = 0; i < interpolation0.length; i++) {
        // imgOut.setPixel(i, (interpolation0[i] + interpolation1[i]) / 2);
        imgOut0.set(i, (interpolation0[i] & 0xFF));
        imgOut1.set(i, (interpolation1[i] & 0xFF));
      }

      ImagePlus imgPlus0 = new ImagePlus("interpolation - max", imgOut0);
      imgPlus0.show("image interpolation - max");
      ImagePlus imgPlus1 = new ImagePlus("interpolation - min", imgOut1);
      imgPlus1.show("image interpolation - min");
    } else if (ip instanceof ColorProcessor) {
      ColorImage img = ImageJAdapter.toColorImage((ColorProcessor) ip);

      int interpWidth = (img.getWidth() * 4 - 3);
      int interpHeight = (img.getHeight() * 4 - 3);

      GrayScaleImage imgR = img.getRed();
      GrayScaleImage imgG = img.getGreen();
      GrayScaleImage imgB = img.getBlue();

      ColorProcessor imgOut0 = new ColorProcessor(interpWidth, interpHeight);
      ColorProcessor imgOut1 = new ColorProcessor(interpWidth, interpHeight);

      Object objR[] = BuilderTreeOfShapeByUnionFindParallel.getImageInterpolate(imgR);
      short interpolation0R[] = (short[]) objR[0];
      short interpolation1R[] = (short[]) objR[1];

      Object objG[] = BuilderTreeOfShapeByUnionFindParallel.getImageInterpolate(imgG);
      short interpolation0G[] = (short[]) objG[0];
      short interpolation1G[] = (short[]) objG[1];

      Object objB[] = BuilderTreeOfShapeByUnionFindParallel.getImageInterpolate(imgB);
      short interpolation0B[] = (short[]) objB[0];
      short interpolation1B[] = (short[]) objB[1];

      for (int i = 0; i < interpolation0R.length; i++) {

        int valor0 =
            ((0 & 0xFF) << 24)
                | ((interpolation0R[i] & 0xFF) << 16)
                | ((interpolation0G[i] & 0xFF) << 8)
                | ((interpolation0B[i] & 0xFF) << 0);

        imgOut0.set(i, valor0);

        int valor1 =
            ((0 & 0xFF) << 24)
                | ((interpolation1R[i] & 0xFF) << 16)
                | ((interpolation1G[i] & 0xFF) << 8)
                | ((interpolation1B[i] & 0xFF) << 0);
        imgOut1.set(i, valor1);
      }

      ImagePlus imgPlus0 = new ImagePlus("interpolation - max", imgOut0);
      imgPlus0.show("image interpolation - max");
      ImagePlus imgPlus1 = new ImagePlus("interpolation - min", imgOut1);
      imgPlus1.show("image interpolation - min");
    }
  }