Example #1
0
 void lineToArea(ImagePlus imp) {
   Roi roi = imp.getRoi();
   if (roi == null || !roi.isLine()) {
     IJ.error("Line to Area", "Line selection required");
     return;
   }
   if (roi.getType() == Roi.LINE && roi.getStrokeWidth() == 1) {
     IJ.error("Line to Area", "Straight line width must be > 1");
     return;
   }
   ImageProcessor ip2 = new ByteProcessor(imp.getWidth(), imp.getHeight());
   ip2.setColor(255);
   if (roi.getType() == Roi.LINE) ip2.fillPolygon(roi.getPolygon());
   else {
     roi.drawPixels(ip2);
     // BufferedImage bi = new BufferedImage(imp.getWidth(), imp.getHeight(),
     // BufferedImage.TYPE_BYTE_GRAY);
     // Graphics g = bi.getGraphics();
     // Roi roi2 = (Roi)roi.clone();
     // roi2.setStrokeColor(Color.white);
     // roi2.drawOverlay(g);
     // ip2 = new ByteProcessor(bi);
   }
   // new ImagePlus("ip2", ip2.duplicate()).show();
   ip2.setThreshold(255, 255, ImageProcessor.NO_LUT_UPDATE);
   ThresholdToSelection tts = new ThresholdToSelection();
   Roi roi2 = tts.convert(ip2);
   imp.setRoi(roi2);
   Roi.previousRoi = (Roi) roi.clone();
 }
Example #2
0
 void createEllipse(ImagePlus imp) {
   IJ.showStatus("Fitting ellipse");
   Roi roi = imp.getRoi();
   if (roi == null) {
     noRoi("Fit Ellipse");
     return;
   }
   if (roi.isLine()) {
     IJ.error("Fit Ellipse", "\"Fit Ellipse\" does not work with line selections");
     return;
   }
   ImageProcessor ip = imp.getProcessor();
   ip.setRoi(roi);
   int options = Measurements.CENTROID + Measurements.ELLIPSE;
   ImageStatistics stats = ImageStatistics.getStatistics(ip, options, null);
   double dx = stats.major * Math.cos(stats.angle / 180.0 * Math.PI) / 2.0;
   double dy = -stats.major * Math.sin(stats.angle / 180.0 * Math.PI) / 2.0;
   double x1 = stats.xCentroid - dx;
   double x2 = stats.xCentroid + dx;
   double y1 = stats.yCentroid - dy;
   double y2 = stats.yCentroid + dy;
   double aspectRatio = stats.minor / stats.major;
   imp.killRoi();
   imp.setRoi(new EllipseRoi(x1, y1, x2, y2, aspectRatio));
 }
Example #3
0
 void getCentroid(ImageProcessor ip, int minThreshold, int maxThreshold) {
   byte[] pixels = (byte[]) ip.getPixels();
   byte[] mask = ip.getMaskArray();
   boolean limit = minThreshold > 0 || maxThreshold < 255;
   double xsum = 0, ysum = 0;
   int count = 0, i, mi, v;
   for (int y = ry, my = 0; y < (ry + rh); y++, my++) {
     i = y * width + rx;
     mi = my * rw;
     for (int x = rx; x < (rx + rw); x++) {
       if (mask == null || mask[mi++] != 0) {
         if (limit) {
           v = pixels[i] & 255;
           if (v >= minThreshold && v <= maxThreshold) {
             count++;
             xsum += x;
             ysum += y;
           }
         } else {
           count++;
           xsum += x;
           ysum += y;
         }
       }
       i++;
     }
   }
   xCentroid = xsum / count + 0.5;
   yCentroid = ysum / count + 0.5;
   if (cal != null) {
     xCentroid = cal.getX(xCentroid);
     yCentroid = cal.getY(yCentroid, height);
   }
 }
  // Used to get text in a image store at the location specified by the path
  public static String getText(String path) {
    // read a low quality image to save memory
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 4;
    try {

      Bitmap temp_bitmap = BitmapFactory.decodeFile(path, options);
      ExifInterface exif = new ExifInterface(path);

      // Correct the orientation of the bitmap
      temp_bitmap = ImageProcessor.correctOrientation(temp_bitmap, exif);
      Bitmap bitmap = ImageProcessor.optimizeBitmap(temp_bitmap);

      String recognizedText = BookSearchApp.scanPhoto(bitmap);

      // remove some wrong results
      if (BookSearchApp.lang.equalsIgnoreCase("eng")) {
        recognizedText = recognizedText.replaceAll("[^a-zA-Z0-9]+", "");
      }

      recognizedText = recognizedText.trim();
      return recognizedText;

    } catch (IOException e) {
      // if there is a error with the SD card
      Log.d(TAG, "Error reading from SD Card");
      e.printStackTrace();
    }
    return "";
  }
Example #5
0
  /** Generate output image whose type is same as input image. */
  private ImagePlus makeOutputImage(ImagePlus imp, FloatProcessor fp, int ptype) {
    int width = imp.getWidth();
    int height = imp.getHeight();
    float[] pixels = (float[]) fp.getPixels();
    ImageProcessor oip = null;

    // Create output image consistent w/ type of input image.
    int size = pixels.length;
    switch (ptype) {
      case BYTE_TYPE:
        oip = imp.getProcessor().createProcessor(width, height);
        byte[] pixels8 = (byte[]) oip.getPixels();
        for (int i = 0; i < size; i++) pixels8[i] = (byte) pixels[i];
        break;
      case SHORT_TYPE:
        oip = imp.getProcessor().createProcessor(width, height);
        short[] pixels16 = (short[]) oip.getPixels();
        for (int i = 0; i < size; i++) pixels16[i] = (short) pixels[i];
        break;
      case FLOAT_TYPE:
        oip = new FloatProcessor(width, height, pixels, null);
        break;
    }

    // Adjust for display.
    // Calling this on non-ByteProcessors ensures image
    // processor is set up to correctly display image.
    oip.resetMinAndMax();

    // Create new image plus object. Don't use
    // ImagePlus.createImagePlus here because there may be
    // attributes of input image that are not appropriate for
    // projection.
    return new ImagePlus(makeTitle(), oip);
  }
Example #6
0
 /**
  * Constructs a ByteStatistics object from a ByteProcessor using the specified measurement and
  * calibration.
  */
 public ByteStatistics(ImageProcessor ip, int mOptions, Calibration cal) {
   ByteProcessor bp = (ByteProcessor) ip;
   histogram = bp.getHistogram();
   setup(ip, cal);
   double minT = ip.getMinThreshold();
   int minThreshold, maxThreshold;
   if ((mOptions & LIMIT) == 0 || minT == ImageProcessor.NO_THRESHOLD) {
     minThreshold = 0;
     maxThreshold = 255;
   } else {
     minThreshold = (int) minT;
     maxThreshold = (int) ip.getMaxThreshold();
   }
   float[] cTable = cal != null ? cal.getCTable() : null;
   if (cTable != null) getCalibratedStatistics(minThreshold, maxThreshold, cTable);
   else getRawStatistics(minThreshold, maxThreshold);
   if ((mOptions & MIN_MAX) != 0) {
     if (cTable != null) getCalibratedMinAndMax(minThreshold, maxThreshold, cTable);
     else getRawMinAndMax(minThreshold, maxThreshold);
   }
   if ((mOptions & ELLIPSE) != 0) fitEllipse(ip);
   else if ((mOptions & CENTROID) != 0) getCentroid(ip, minThreshold, maxThreshold);
   if ((mOptions & (CENTER_OF_MASS | SKEWNESS | KURTOSIS)) != 0)
     calculateMoments(ip, minThreshold, maxThreshold, cTable);
   if ((mOptions & MEDIAN) != 0) calculateMedian(histogram, minThreshold, maxThreshold, cal);
   if ((mOptions & AREA_FRACTION) != 0) calculateAreaFraction(ip, histogram);
 }
Example #7
0
 void lineToArea(ImagePlus imp) {
   Roi roi = imp.getRoi();
   if (roi == null || !roi.isLine()) {
     IJ.error("Line to Area", "Line selection required");
     return;
   }
   Undo.setup(Undo.ROI, imp);
   Roi roi2 = null;
   if (roi.getType() == Roi.LINE) {
     double width = roi.getStrokeWidth();
     if (width <= 1.0) roi.setStrokeWidth(1.0000001);
     FloatPolygon p = roi.getFloatPolygon();
     roi.setStrokeWidth(width);
     roi2 = new PolygonRoi(p, Roi.POLYGON);
     roi2.setDrawOffset(roi.getDrawOffset());
   } else {
     ImageProcessor ip2 = new ByteProcessor(imp.getWidth(), imp.getHeight());
     ip2.setColor(255);
     roi.drawPixels(ip2);
     // new ImagePlus("ip2", ip2.duplicate()).show();
     ip2.setThreshold(255, 255, ImageProcessor.NO_LUT_UPDATE);
     ThresholdToSelection tts = new ThresholdToSelection();
     roi2 = tts.convert(ip2);
   }
   transferProperties(roi, roi2);
   roi2.setStrokeWidth(0);
   Color c = roi2.getStrokeColor();
   if (c != null) // remove any transparency
   roi2.setStrokeColor(new Color(c.getRed(), c.getGreen(), c.getBlue()));
   imp.setRoi(roi2);
   Roi.previousRoi = (Roi) roi.clone();
 }
Example #8
0
 private ImagePlus duplicateImage(ImageProcessor iProcessor) {
   int w = iProcessor.getWidth();
   int h = iProcessor.getHeight();
   ImagePlus iPlus = NewImage.createByteImage("Image", w, h, 1, NewImage.FILL_BLACK);
   ImageProcessor imageProcessor = iPlus.getProcessor();
   imageProcessor.copyBits(iProcessor, 0, 0, Blitter.COPY);
   return iPlus;
 }
 /**
  * Updates this stack so its attributes, such as min, max, calibration table and color model, are
  * the same as 'ip'.
  */
 public void update(ImageProcessor ip) {
   if (ip != null) {
     min = ip.getMin();
     max = ip.getMax();
     cTable = ip.getCalibrationTable();
     cm = ip.getColorModel();
   }
 }
Example #10
0
 void skeletonize(ImageProcessor ip) {
   if (Prefs.blackBackground) ip.invert();
   boolean edgePixels = hasEdgePixels(ip);
   ImageProcessor ip2 = expand(ip, edgePixels);
   ((ByteProcessor) ip2).skeletonize();
   ip = shrink(ip, ip2, edgePixels);
   if (Prefs.blackBackground) ip.invert();
 }
Example #11
0
 ImageProcessor shrink(ImageProcessor ip, ImageProcessor ip2, boolean hasEdgePixels) {
   if (hasEdgePixels) {
     int width = ip.getWidth();
     int height = ip.getHeight();
     for (int y = 0; y < height; y++)
       for (int x = 0; x < width; x++) ip.putPixel(x, y, ip2.getPixel(x + 1, y + 1));
   }
   return ip;
 }
Example #12
0
 /** Constructs a Wand object from an ImageProcessor. */
 public Wand(ImageProcessor ip) {
   this.ip = ip;
   if (ip instanceof ByteProcessor) bpixels = (byte[]) ip.getPixels();
   else if (ip instanceof ColorProcessor) cpixels = (int[]) ip.getPixels();
   else if (ip instanceof ShortProcessor) spixels = (short[]) ip.getPixels();
   else if (ip instanceof FloatProcessor) fpixels = (float[]) ip.getPixels();
   width = ip.getWidth();
   height = ip.getHeight();
 }
Example #13
0
 /** Draws an outline of this OvalRoi on the image. */
 public void drawPixels(ImageProcessor ip) {
   Polygon p = getPolygon();
   if (p.npoints > 0) {
     int saveWidth = ip.getLineWidth();
     if (getStrokeWidth() > 1f) ip.setLineWidth((int) Math.round(getStrokeWidth()));
     ip.drawPolygon(p);
     ip.setLineWidth(saveWidth);
   }
   if (Line.getWidth() > 1 || getStrokeWidth() > 1) updateFullWindow = true;
 }
Example #14
0
 void lineWidth() {
   int width = (int) IJ.getNumber("Line Width:", Line.getWidth());
   if (width == IJ.CANCELED) return;
   Line.setWidth(width);
   LineWidthAdjuster.update();
   ImagePlus imp = WindowManager.getCurrentImage();
   if (imp != null && imp.isProcessor()) {
     ImageProcessor ip = imp.getProcessor();
     ip.setLineWidth(Line.getWidth());
     Roi roi = imp.getRoi();
     if (roi != null && roi.isLine()) imp.draw();
   }
 }
 boolean setThresholdLevels(ImagePlus imp, ImageProcessor ip) {
   double t1 = ip.getMinThreshold();
   double t2 = ip.getMaxThreshold();
   boolean invertedLut = imp.isInvertedLut();
   boolean byteImage = ip instanceof ByteProcessor;
   if (ip instanceof ShortProcessor) imageType = SHORT;
   else if (ip instanceof FloatProcessor) imageType = FLOAT;
   else imageType = BYTE;
   if (t1 == ImageProcessor.NO_THRESHOLD) {
     ImageStatistics stats = imp.getStatistics();
     if (imageType != BYTE || (stats.histogram[0] + stats.histogram[255] != stats.pixelCount)) {
       IJ.error(
           "Particle Analyzer",
           "A thresholded image or 8-bit binary image is\n"
               + "required. Threshold levels can be set using\n"
               + "the Image->Adjust->Threshold tool.");
       canceled = true;
       return false;
     }
     boolean threshold255 = invertedLut;
     if (Prefs.blackBackground) threshold255 = !threshold255;
     if (threshold255) {
       level1 = 255;
       level2 = 255;
       fillColor = 64;
     } else {
       level1 = 0;
       level2 = 0;
       fillColor = 192;
     }
   } else {
     level1 = t1;
     level2 = t2;
     if (imageType == BYTE) {
       if (level1 > 0) fillColor = 0;
       else if (level2 < 255) fillColor = 255;
     } else if (imageType == SHORT) {
       if (level1 > 0) fillColor = 0;
       else if (level2 < 65535) fillColor = 65535;
     } else if (imageType == FLOAT) fillColor = -Float.MAX_VALUE;
     else return false;
   }
   imageType2 = imageType;
   if (redirectIP != null) {
     if (redirectIP instanceof ShortProcessor) imageType2 = SHORT;
     else if (redirectIP instanceof FloatProcessor) imageType2 = FLOAT;
     else if (redirectIP instanceof ColorProcessor) imageType2 = RGB;
     else imageType2 = BYTE;
   }
   return true;
 }
Example #16
0
  public void mousePressed(MouseEvent e) {
    // super.mousePressed(e);
    ImageProcessor ip = imp.getProcessor();
    ip.setLineWidth(1);
    if (Toolbar.getToolId() == Toolbar.DROPPER) IJ.setTool(Toolbar.RECTANGLE);

    Rectangle flipperRect = new Rectangle(86, 268, 18, 18);
    Rectangle resetRect = new Rectangle(86, 294, 18, 18);
    Rectangle foreground1Rect = new Rectangle(9, 266, 45, 10);
    Rectangle foreground2Rect = new Rectangle(9, 276, 23, 25);
    Rectangle background1Rect = new Rectangle(33, 302, 45, 10);
    Rectangle background2Rect = new Rectangle(56, 277, 23, 25);
    int x = offScreenX(e.getX());
    int y = offScreenY(e.getY());
    long difference = System.currentTimeMillis() - mouseDownTime;
    boolean doubleClick = (difference <= 250);
    mouseDownTime = System.currentTimeMillis();
    if (flipperRect.contains(x, y)) {
      Color c = Toolbar.getBackgroundColor();
      Toolbar.setBackgroundColor(Toolbar.getForegroundColor());
      Toolbar.setForegroundColor(c);
    } else if (resetRect.contains(x, y)) {
      Toolbar.setForegroundColor(new Color(0x000000));
      Toolbar.setBackgroundColor(new Color(0xffffff));
    } else if ((background1Rect.contains(x, y)) || (background2Rect.contains(x, y))) {
      background = true;
      if (doubleClick) editColor();
      ((ColorGenerator) ip).refreshForeground();
      ((ColorGenerator) ip).refreshBackground();
    } else if ((foreground1Rect.contains(x, y)) || (foreground2Rect.contains(x, y))) {
      background = false;
      if (doubleClick) editColor();
      ((ColorGenerator) ip).refreshBackground();
      ((ColorGenerator) ip).refreshForeground();
    } else {
      // IJ.log(" " + difference + " " + doubleClick);
      if (doubleClick) editColor();
      else {
        setDrawingColor(offScreenX(e.getX()), offScreenY(e.getY()), background);
      }
    }
    if (ip instanceof ColorGenerator) {
      if (background) {
        ((ColorGenerator) ip).refreshForeground();
        ((ColorGenerator) ip).refreshBackground();
      } else {
        ((ColorGenerator) ip).refreshBackground();
        ((ColorGenerator) ip).refreshForeground();
      }
    }
  }
 void drawOutline(ImageProcessor ip, Roi roi, int count) {
   if (showChoice == OVERLAY_OUTLINES || showChoice == OVERLAY_MASKS) {
     if (overlay == null) {
       overlay = new Overlay();
       overlay.drawLabels(true);
       overlay.setLabelFont(new Font("SansSerif", Font.PLAIN, fontSize));
     }
     Roi roi2 = (Roi) roi.clone();
     roi2.setStrokeColor(Color.cyan);
     if (lineWidth != 1) roi2.setStrokeWidth(lineWidth);
     if (showChoice == OVERLAY_MASKS) roi2.setFillColor(Color.cyan);
     overlay.add(roi2);
   } else {
     Rectangle r = roi.getBounds();
     int nPoints = ((PolygonRoi) roi).getNCoordinates();
     int[] xp = ((PolygonRoi) roi).getXCoordinates();
     int[] yp = ((PolygonRoi) roi).getYCoordinates();
     int x = r.x, y = r.y;
     if (!inSituShow) ip.setValue(0.0);
     ip.moveTo(x + xp[0], y + yp[0]);
     for (int i = 1; i < nPoints; i++) ip.lineTo(x + xp[i], y + yp[i]);
     ip.lineTo(x + xp[0], y + yp[0]);
     if (showChoice != BARE_OUTLINES) {
       String s = ResultsTable.d2s(count, 0);
       ip.moveTo(r.x + r.width / 2 - ip.getStringWidth(s) / 2, r.y + r.height / 2 + fontSize / 2);
       if (!inSituShow) ip.setValue(1.0);
       ip.drawString(s);
     }
   }
 }
Example #18
0
 void reset(ImagePlus imp, ImageProcessor ip) {
   // Assign the pixels of ip to the data in the restore array, while
   // taking care to not give the address the restore array to the
   // image processor.
   int[] pixels = (int[]) ip.getPixels();
   for (int i = 0; i < numPixels; i++) pixels[i] = restore[i];
 }
 /**
  * Returns an ImageProcessor for the specified slice, were 1<=n<=nslices. Returns null if the
  * stack is empty.
  */
 public ImageProcessor getProcessor(int n) {
   ImageProcessor ip;
   if (n < 1 || n > nSlices) throw new IllegalArgumentException(outOfRange + n);
   if (nSlices == 0) return null;
   if (stack[0] == null) throw new IllegalArgumentException("Pixel array is null");
   if (stack[0] instanceof byte[]) ip = new ByteProcessor(width, height, null, cm);
   else if (stack[0] instanceof short[]) ip = new ShortProcessor(width, height, null, cm);
   else if (stack[0] instanceof int[]) ip = new ColorProcessor(width, height, null);
   else if (stack[0] instanceof float[]) ip = new FloatProcessor(width, height, null, cm);
   else throw new IllegalArgumentException("Unknown stack type");
   ip.setPixels(stack[n - 1]);
   if (min != Double.MAX_VALUE && ip != null && !(ip instanceof ColorProcessor))
     ip.setMinAndMax(min, max);
   if (cTable != null) ip.setCalibrationTable(cTable);
   return ip;
 }
Example #20
0
  /*------------------------------------------------------------------*/
  void putRow(ImageProcessor ip, int y, double[] row) {
    int rowLength = ip.getWidth();

    if (rowLength != row.length) {
      throw new IndexOutOfBoundsException("Incoherent array sizes");
    }
    y *= rowLength;
    if (ip.getPixels() instanceof float[]) {
      float[] floatPixels = (float[]) ip.getPixels();
      for (int i = 0; (i < rowLength); i++) {
        floatPixels[y++] = (float) row[i];
      }
    } else {
      throw new IllegalArgumentException("Float image required");
    }
  } /* end putRow */
Example #21
0
  /*------------------------------------------------------------------*/
  void putColumn(ImageProcessor ip, int x, double[] column) {
    int width = ip.getWidth();

    if (ip.getHeight() != column.length) {
      throw new IndexOutOfBoundsException("Incoherent array sizes");
    }
    if (ip.getPixels() instanceof float[]) {
      float[] floatPixels = (float[]) ip.getPixels();
      for (int i = 0; (i < column.length); i++) {
        floatPixels[x] = (float) column[i];
        x += width;
      }
    } else {
      throw new IllegalArgumentException("Float image required");
    }
  } /* end putColumn */
Example #22
0
 /** Opens a stack of images. */
 ImagePlus openStack(ColorModel cm, boolean show) {
   ImageStack stack = new ImageStack(fi.width, fi.height, cm);
   long skip = fi.getOffset();
   Object pixels;
   try {
     ImageReader reader = new ImageReader(fi);
     InputStream is = createInputStream(fi);
     if (is == null) return null;
     IJ.resetEscape();
     for (int i = 1; i <= fi.nImages; i++) {
       if (!silentMode) IJ.showStatus("Reading: " + i + "/" + fi.nImages);
       if (IJ.escapePressed()) {
         IJ.beep();
         IJ.showProgress(1.0);
         silentMode = false;
         return null;
       }
       pixels = reader.readPixels(is, skip);
       if (pixels == null) break;
       stack.addSlice(null, pixels);
       skip = fi.gapBetweenImages;
       if (!silentMode) IJ.showProgress(i, fi.nImages);
     }
     is.close();
   } catch (Exception e) {
     IJ.log("" + e);
   } catch (OutOfMemoryError e) {
     IJ.outOfMemory(fi.fileName);
     stack.trim();
   }
   if (!silentMode) IJ.showProgress(1.0);
   if (stack.getSize() == 0) return null;
   if (fi.sliceLabels != null && fi.sliceLabels.length <= stack.getSize()) {
     for (int i = 0; i < fi.sliceLabels.length; i++) stack.setSliceLabel(fi.sliceLabels[i], i + 1);
   }
   ImagePlus imp = new ImagePlus(fi.fileName, stack);
   if (fi.info != null) imp.setProperty("Info", fi.info);
   if (show) imp.show();
   imp.setFileInfo(fi);
   setCalibration(imp);
   ImageProcessor ip = imp.getProcessor();
   if (ip.getMin() == ip.getMax()) // find stack min and max if first slice is blank
   setStackDisplayRange(imp);
   if (!silentMode) IJ.showProgress(1.0);
   silentMode = false;
   return imp;
 }
Example #23
0
    void setHistogram(ImagePlus imp, int j) {
      ImageProcessor ip = imp.getProcessor();
      ImageStatistics stats = ImageStatistics.getStatistics(ip, AREA + MODE, null);
      int maxCount2 = 0;
      histogram = stats.histogram;
      for (int i = 0; i < stats.nBins; i++)
        if ((histogram[i] > maxCount2) && (i != stats.mode)) maxCount2 = histogram[i];
      hmax = stats.maxCount;
      if ((hmax > (maxCount2 * 1.5)) && (maxCount2 != 0)) { // GL 1.5 was 2
        hmax = (int) (maxCount2 * 1.1); // GL 1.1 was 1.5
        histogram[stats.mode] = hmax;
      }
      os = null;
      ColorModel cm = ip.getColorModel();
      if (!(cm instanceof IndexColorModel)) return;
      IndexColorModel icm = (IndexColorModel) cm;
      int mapSize = icm.getMapSize();
      if (mapSize != 256) return;
      byte[] r = new byte[256];
      byte[] g = new byte[256];
      byte[] b = new byte[256];
      icm.getReds(r);
      icm.getGreens(g);
      icm.getBlues(b);
      hColors = new Color[256];

      if (isRGB) {
        if (j == 0) {
          for (int i = 0; i < 256; i++) hColors[i] = new Color(i & 255, 0 & 255, 0 & 255);
        } else if (j == 1) {
          for (int i = 0; i < 256; i++) hColors[i] = new Color(0 & 255, i & 255, 0 & 255);
        } else if (j == 2) {
          for (int i = 0; i < 256; i++) hColors[i] = new Color(0 & 255, 0 & 255, i & 255);
        }
      } else {
        if (j == 0) {
          for (int i = 0; i < 256; i++) hColors[i] = new Color(r[i] & 255, g[i] & 255, b[i] & 255);
        } else if (j == 1) {
          for (int i = 0; i < 256; i++)
            // hColors[i] = new Color(127-i/2&255, 127+i/2&255, 127-i/2&255);
            hColors[i] = new Color(192 - i / 4 & 255, 192 + i / 4 & 255, 192 - i / 4 & 255);
        } else if (j == 2) {
          for (int i = 0; i < 256; i++) hColors[i] = new Color(i & 255, i & 255, 0 & 255);
        }
      }
    }
Example #24
0
 void lineToArea(ImagePlus imp) {
   Roi roi = imp.getRoi();
   if (roi == null || !roi.isLine()) {
     IJ.error("Line to Area", "Line selection required");
     return;
   }
   ImageProcessor ip2 = new ByteProcessor(imp.getWidth(), imp.getHeight());
   ip2.setColor(255);
   if (roi.getType() == Roi.LINE && roi.getStrokeWidth() > 1) ip2.fillPolygon(roi.getPolygon());
   else roi.drawPixels(ip2);
   // new ImagePlus("ip2", ip2.duplicate()).show();
   ip2.setThreshold(255, 255, ImageProcessor.NO_LUT_UPDATE);
   ThresholdToSelection tts = new ThresholdToSelection();
   Roi roi2 = tts.convert(ip2);
   imp.setRoi(roi2);
   Roi.previousRoi = (Roi) roi.clone();
 }
Example #25
0
  /**
   * Execute the plugin functionality: duplicate and scale the given image.
   *
   * @return an Object[] array with the name and the scaled ImagePlus. Does NOT show the new, image;
   *     just returns it.
   */
  public Object[] exec(
      ImagePlus imp, String myMethod, int radius, double par1, double par2, boolean doIwhite) {

    // 0 - Check validity of parameters
    if (null == imp) return null;
    ImageProcessor ip = imp.getProcessor();
    int xe = ip.getWidth();
    int ye = ip.getHeight();

    // int [] data = (ip.getHistogram());

    IJ.showStatus("Thresholding...");
    long startTime = System.currentTimeMillis();
    // 1 Do it
    if (imp.getStackSize() == 1) {
      ip.snapshot();
      Undo.setup(Undo.FILTER, imp);
    }
    // Apply the selected algorithm
    if (myMethod.equals("Bernsen")) {
      Bernsen(imp, radius, par1, par2, doIwhite);
    } else if (myMethod.equals("Contrast")) {
      Contrast(imp, radius, par1, par2, doIwhite);
    } else if (myMethod.equals("Mean")) {
      Mean(imp, radius, par1, par2, doIwhite);
    } else if (myMethod.equals("Median")) {
      Median(imp, radius, par1, par2, doIwhite);
    } else if (myMethod.equals("MidGrey")) {
      MidGrey(imp, radius, par1, par2, doIwhite);
    } else if (myMethod.equals("Niblack")) {
      Niblack(imp, radius, par1, par2, doIwhite);
    } else if (myMethod.equals("Otsu")) {
      Otsu(imp, radius, par1, par2, doIwhite);
    } else if (myMethod.equals("Phansalkar")) {
      Phansalkar(imp, radius, par1, par2, doIwhite);
    } else if (myMethod.equals("Sauvola")) {
      Sauvola(imp, radius, par1, par2, doIwhite);
    }
    // IJ.showProgress((double)(255-i)/255);
    imp.updateAndDraw();
    imp.getProcessor().setThreshold(255, 255, ImageProcessor.NO_LUT_UPDATE);
    // 2 - Return the threshold and the image
    IJ.showStatus("\nDone " + (System.currentTimeMillis() - startTime) / 1000.0);
    return new Object[] {imp};
  }
Example #26
0
 void createMask(ImagePlus imp) {
   Roi roi = imp.getRoi();
   boolean useInvertingLut = Prefs.useInvertingLut;
   Prefs.useInvertingLut = false;
   if (roi == null || !(roi.isArea() || roi.getType() == Roi.POINT)) {
     createMaskFromThreshold(imp);
     Prefs.useInvertingLut = useInvertingLut;
     return;
   }
   ImagePlus maskImp = null;
   Frame frame = WindowManager.getFrame("Mask");
   if (frame != null && (frame instanceof ImageWindow))
     maskImp = ((ImageWindow) frame).getImagePlus();
   if (maskImp == null) {
     ImageProcessor ip = new ByteProcessor(imp.getWidth(), imp.getHeight());
     if (!Prefs.blackBackground) ip.invertLut();
     maskImp = new ImagePlus("Mask", ip);
     maskImp.show();
   }
   ImageProcessor ip = maskImp.getProcessor();
   ip.setRoi(roi);
   ip.setValue(255);
   ip.fill(ip.getMask());
   maskImp.updateAndDraw();
   Prefs.useInvertingLut = useInvertingLut;
 }
Example #27
0
  /*------------------------------------------------------------------*/
  public void getVerticalHessian(ImageProcessor ip, double tolerance) {
    if (!(ip.getPixels() instanceof float[])) {
      throw new IllegalArgumentException("Float image required");
    }

    float[] floatPixels = (float[]) ip.getPixels();
    int width = ip.getWidth();
    int height = ip.getHeight();
    double line[] = new double[height];

    for (int x = 0; (x < width); x++) {
      getColumn(ip, x, line);
      getSplineInterpolationCoefficients(line, tolerance);
      getHessian(line);
      putColumn(ip, x, line);
      stepProgressBar();
    }
  } /* end getVerticalHessian */
Example #28
0
  /*------------------------------------------------------------------*/
  public void getHorizontalGradient(ImageProcessor ip, double tolerance) {
    if (!(ip.getPixels() instanceof float[])) {
      throw new IllegalArgumentException("Float image required");
    }

    float[] floatPixels = (float[]) ip.getPixels();
    int width = ip.getWidth();
    int height = ip.getHeight();
    double line[] = new double[width];

    for (int y = 0; (y < height); y++) {
      getRow(ip, y, line);
      getSplineInterpolationCoefficients(line, tolerance);
      getGradient(line);
      putRow(ip, y, line);
      stepProgressBar();
    }
  } /* end getHorizontalGradient */
 /**
  * Processes an Image.
  *
  * @param img
  * @param attrs
  * @throws DocumentException
  * @since 5.0.6
  */
 public void processImage(final Image img, final Map<String, String> attrs)
     throws DocumentException {
   ImageProcessor processor = (ImageProcessor) providers.get(HTMLWorker.IMG_PROCESSOR);
   if (processor == null || !processor.process(img, attrs, chain, document)) {
     String align = attrs.get(HtmlTags.ALIGN);
     if (align != null) {
       carriageReturn();
     }
     if (currentParagraph == null) {
       currentParagraph = createParagraph();
     }
     currentParagraph.add(new Chunk(img, 0, 0, true));
     currentParagraph.setAlignment(HtmlUtilities.alignmentValue(align));
     if (align != null) {
       carriageReturn();
     }
   }
 }
Example #30
0
 // get a pixel value; returns Float.NaN if outside the field.
 private float getPixel(int x, int y) {
   if (x < 0 || x >= width || y < 0 || y >= height) return Float.NaN;
   if (bpixels != null) return bpixels[y * width + x] & 0xff;
   else if (spixels != null) return spixels[y * width + x] & 0xffff;
   else if (fpixels != null) return fpixels[y * width + x];
   else if (exactPixelValue) // RGB for exact match
   return cpixels[y * width + x] & 0xffffff; // don't care for upper byte
   else // gray value of RGB
   return ip.getPixelValue(x, y);
 }