0 Replies Latest reply: May 21, 2010 5:59 AM by 843802 RSS

    Help contrast stretch

    843802
      Hi all,
      How to apply a simple contrast stretch algorithm on grayscale Image?
       Image inImage = new ImageIcon(f.getAbsolutePath(), f.getName()).getImage();
                int maxDim = 110;
                      double scale = (double) maxDim / (double) inImage.getHeight(null);
                      if (inImage.getWidth(null) > inImage.getHeight(null))
                {
                          scale = (double) maxDim / (double) inImage.getWidth(null);
                }
                      int scaledW = (int) (scale * inImage.getWidth(null));
                int scaledH = (int) (scale * inImage.getHeight(null));
                      Image img = inImage.getScaledInstance(scaledW , scaledH, Image.SCALE_SMOOTH);
      
      
                      //Grayscale images
                      Image filtered = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(img.getSource(), new GrayscaleImageFilter()));
      
                      //Converto tipo Image in PlanarImage
                      ParameterBlock pd = new ParameterBlock();
                      pd.add(img);
                      PlanarImage img_original = JAI.create("awtImage", pd);
      
                      ParameterBlock pb = new ParameterBlock();
                      pb.add(filtered);
                      PlanarImage img_gray = JAI.create("awtImage", pb);
      
                      ParameterBlock pbMaxMin = new ParameterBlock();
                      pbMaxMin.addSource(img_gray);
                      RenderedOp extremaOp = JAI.create("extrema", pbMaxMin);
                      // Get the extrema of all bands !
                      double[] allMins = (double[])extremaOp.getProperty("minimum");
                      double[] allMaxs = (double[])extremaOp.getProperty("maximum");
                      for(int b=0;b<allMins.length;b++)
                      System.out.println("Band "+b+" values are between "+allMins[b]+" and "+allMaxs);

      //BufferedImage myBufImg = img_gray.getAsBufferedImage();

      // CONTRAST STRETCH ?????









      DisplayJAI original_image = null;
      original_image = new DisplayJAI(img_original);
      jToolBar1.add(original_image);
      jToolBar1.revalidate();

      DisplayJAI gray_image = null;
      gray_image = new DisplayJAI(img_gray);
      jToolBar3.add(gray_image);
      jToolBar3.revalidate();