Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. import ij.IJ;
  2. import ij.ImagePlus; // for ImagePlus
  3. import ij.gui.GenericDialog;
  4. import ij.gui.Overlay;
  5. import ij.gui.PolygonRoi;
  6. import ij.gui.Roi;
  7. import ij.plugin.filter.PlugInFilter; // for PlugInFilter
  8. import ij.process.ByteProcessor;
  9. import ij.process.ImageProcessor; // for ImageProcessor
  10.  
  11. import java.awt.Color;
  12. import java.awt.Point;
  13. import java.awt.Polygon;
  14.  
  15. public class darkest_Spot implements PlugInFilter {
  16.  
  17.     int blockSize = 15;
  18.  
  19.     public int setup(String arg, ImagePlus imp) {
  20.         return PlugInFilter.DOES_RGB + PlugInFilter.NO_UNDO
  21.                 + PlugInFilter.NO_CHANGES;
  22.     }
  23.  
  24.     public void run(ImageProcessor ip) {
  25.         if (!ip.isGrayscale()) {
  26.             ip = new Color_2_Gray().color2Gray(ip);
  27.         }
  28.         blockSize = Dialog.getBlocksizeFromDialog();
  29.         long startTime, stopTime, diffTime;
  30.         startTime = System.currentTimeMillis();
  31.         Point corner = getDarkestSpot(ip, blockSize,0);
  32.         stopTime = System.currentTimeMillis();
  33.         diffTime = stopTime - startTime;
  34.         IJ.log("time: " + diffTime + " ms");
  35.         ImagePlus erg = createOverlayBlock(ip, "Poly",
  36.                 getPoly(corner, blockSize));
  37.         erg.show();
  38.     }
  39.    
  40.     private Polygon getPoly(Point corner, int blockSize) {
  41.         Polygon myPoly = new Polygon();
  42.         myPoly.addPoint(corner.x, corner.y);
  43.         myPoly.addPoint(corner.x - blockSize, corner.y);
  44.         myPoly.addPoint(corner.x - blockSize, corner.y - blockSize);
  45.         myPoly.addPoint(corner.x, corner.y - blockSize);
  46.         myPoly.addPoint(corner.x, corner.y);
  47.         return myPoly;
  48.     }
  49.     /**
  50.      * returns bottom right Corner of darkest Spot in Pic
  51.      * @param ip_gray
  52.      * @param blockSize
  53.      * @return
  54.      */
  55.     public Point getDarkestSpot(ImageProcessor ip_gray, int blockSize,int pixelcutoff) {
  56.         long[][] asb = createAsb(ip_gray);
  57.         Point darkestSpot = new Point(0, 0);
  58.         long darkestValue = Long.MAX_VALUE;
  59.         long value = 0;
  60.  
  61.         for (int cols = blockSize + pixelcutoff; cols < ip_gray.getWidth() - pixelcutoff; cols++) {
  62.             for (int rows = blockSize + pixelcutoff; rows < ip_gray.getHeight()-pixelcutoff; rows++) {
  63.                 value = (asb[rows][cols] - asb[rows][cols - blockSize]
  64.                         - asb[rows - blockSize][cols] + asb[rows - blockSize][cols
  65.                         - blockSize]);
  66.                 if (darkestValue > value) {
  67.                     darkestSpot.setLocation(cols, rows);
  68.                     darkestValue = value;
  69.                 }
  70.             }
  71.         }
  72.  
  73.         System.out.println(darkestSpot.x + ": " + darkestSpot.y);
  74.         return darkestSpot;
  75.     }
  76.  
  77.     private long[][] createAsb(ImageProcessor ip) {
  78.         int cols = ip.getWidth();
  79.         int rows = ip.getHeight();
  80.  
  81.         long[][] asb = new long[rows][cols];
  82.  
  83.         byte[] pic = (byte[]) ip.getPixels();
  84.         long sum_row = 0;
  85.  
  86.         // First Row
  87.         for (int i = 0; i < cols; i++) {
  88.             sum_row = sum_row + (pic[i] & 0xff);
  89.             asb[0][i] = sum_row;
  90.         }
  91.  
  92.         // Rest
  93.         for (int i = 1; i < (rows); i++) {
  94.             sum_row = 0;
  95.             for (int j = 0; j < cols; j++) {
  96.                 sum_row = sum_row + (pic[(i * cols + j)] & 0xff);
  97.                 asb[i][j] = sum_row + asb[i - 1][j];
  98.             }
  99.         }
  100.         return asb;
  101.     }
  102.  
  103.     public ImagePlus createOverlayBlock(ImageProcessor ip, String title,
  104.             Polygon poly) {
  105.         Roi myRoi = new PolygonRoi(poly, Roi.POLYLINE);
  106.         myRoi.setStrokeColor(Color.RED);
  107.         myRoi.setStrokeWidth(1);
  108.  
  109.         Overlay myOverlay = new Overlay(myRoi);
  110.         ImagePlus impOverlay = new ImagePlus(title, ip);
  111.         impOverlay.setOverlay(myOverlay);
  112.         return impOverlay;
  113.     }
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement