Login  Register

Re: Can't find Auto Crop Plugin

Posted by gabejackson on Oct 16, 2009; 3:42pm
URL: http://imagej.273.s1.nabble.com/Can-t-find-Auto-Crop-Plugin-tp3686501p3686502.html

Nevermind, i quickly wrote one... it's really ugly, but maybe somebody can use it :D

import ij.*;
import ij.plugin.filter.PlugInFilter;
import ij.process.*;
import java.awt.*;

//
//  AutoCrop_.java
//  

public class AutoCrop_ implements PlugInFilter {
       
        protected ImagePlus imp;
       
    public int setup(String arg, ImagePlus imp) {
                this.imp = imp;
        return DOES_ALL+DOES_STACKS+SUPPORTS_MASKING;
    }
       
    public void run(ImageProcessor ip) {
                Rectangle r = ip.getRoi();

                       
            int minX = 0;
                int minY = 0;
                int maxX = 0;
                int maxY = 0;
               
                for (int y=0; y<imp.getHeight(); y++) {
                        for (int x=0; x<imp.getWidth(); x++) {
                                if(ip.get(x, y) == 255) {
                                        maxY = y;
                                    break;
                                }
                        }
                }
               
                for (int x=0; x<imp.getWidth(); x++) {
                        for (int y=0; y<imp.getHeight(); y++) {
                                if(ip.get(x, y) == 255) {
                                        maxX = x;
                                    break;
                                }
                        }
                }
               
                for (int y=imp.getHeight()-1; y>=0; y--) {
                        for (int x=0; x<imp.getWidth(); x++) {
                                if(ip.get(x, y) == 255) {
                                        minY = y;
                                    break;
                                }
                        }
                }
               
               
                for (int x=imp.getWidth()-1; x>=0; x--) {
                        for (int y=0; y<imp.getHeight(); y++) {
                                if(ip.get(x, y) == 255) {
                                        minX = x;
                                    break;
                                }
                        }
                }
               
                int newWidth = (imp.getWidth()-(imp.getWidth()-maxX-1));
                int newHeight = (imp.getHeight()-(imp.getHeight()-maxY-1));
               
               
               
                IJ.run(imp, "Canvas Size...", "width="+newWidth+" height="+newHeight+" position=Top-Left zero");
                IJ.run(imp, "Canvas Size...", "width="+(newWidth-minX)+" height="+(newHeight-minY)+" position=Bottom-Right zero");
               
                /*IJ.write("New Width: "+newWidth);
                IJ.write("New Height: "+newHeight);
               
                IJ.write("found min Y at: " + minY + "\n");
                IJ.write("found max Y at: " + maxY + "\n");
                IJ.write("found min X at: " + minX + "\n");
                IJ.write("found max X at: " + maxX + "\n");*/
    }
}


<quote author="gabejackson">
Do we have no such plugin that would automatically crop a binary image to the nearest white/black pixels on each side? Just automatically find the bounding box in a binary image. Seems like we should?