Ejemplo n.º 1
0
  /**
   * Converts an image (RGB, RGBA, ... whatever) to a binary one based on given threshold
   *
   * @param image the image to convert. Remains untouched.
   * @param threshold the threshold in [0,255]
   * @return a new BufferedImage instance of TYPE_BYTE_GRAY with only 0'S and 255's
   */
  private static BufferedImage thresholdImage(Image imgIn, Image imgOut, int threshold) {
    if (imgOut.getBufferedImage().getType() != BufferedImage.TYPE_BYTE_GRAY) {
      final BufferedImage result =
          new BufferedImage(imgIn.getWidth(), imgIn.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
      final Graphics g = result.getGraphics();
      g.drawImage(imgIn.getBufferedImage(), 0, 0, null);
      g.dispose();

      imgOut.setBufferedImage(result);
      System.err.println("Image was converted into BufferedImage.TYPE_BYTE_GRAY type");
    }

    BufferedImage result = imgOut.getBufferedImage();
    result.getGraphics().drawImage(imgIn.getBufferedImage(), 0, 0, null);
    WritableRaster raster = result.getRaster();
    int[] pixels = new int[imgIn.getWidth()];
    for (int y = 0; y < imgIn.getHeight(); y++) {
      raster.getPixels(0, y, imgIn.getWidth(), 1, pixels);
      for (int i = 0; i < pixels.length; i++) {
        pixels[i] = (pixels[i] < threshold) ? 0 : 255;
      }
      raster.setPixels(0, y, imgIn.getWidth(), 1, pixels);
    }
    return result;
  }
Ejemplo n.º 2
0
 public static int[][] toIntArray(Image img, int signal, int background) {
   int[][] ret = new int[img.getWidth()][img.getHeight()];
   for (int i = 0; i < img.getWidth(); i++) {
     for (int j = 0; j < img.getHeight(); j++) {
       ret[i][j] = img.getRed(i, j) < 128 ? signal : background;
     }
   }
   return ret;
 }
Ejemplo n.º 3
0
 @Override
 public void process(Image imgIn, Image imgOut) {
   res = compactLabeling(toIntArray(imgIn), imgIn.getWidth(), imgIn.getHeight(), true);
   final int scale = getMaxLabel() + 1;
   fromIntArray(res, scale, imgOut);
   setAttribute("result", res);
   setAttribute("nr", getMaxLabel());
 }
Ejemplo n.º 4
0
 public static void fromIntArray(int[][] im, int size, Image out) {
   Color[] c = ColorTable.randomColorArray(size);
   c[0] = Color.white;
   for (int i = 0; i < out.getWidth(); i++) {
     for (int j = 0; j < out.getHeight(); j++) {
       out.setRGB(i, j, c[im[i][j]].getRGB());
     }
   }
 }