Can't find Auto Crop Plugin

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Can't find Auto Crop Plugin

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?
Reply | Threaded
Open this post in threaded view
|

Re: Can't find Auto Crop Plugin

gabejackson
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?

Reply | Threaded
Open this post in threaded view
|

Re: Can't find Auto Crop Plugin

Gabriel Landini
In reply to this post by gabejackson
On Friday 16 October 2009  16:41:40 gabejackson wrote:
> 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?

Yes, we should. I can write one, this weekend, but if somebody is already
writing one now, please:
 1. let me know so I do not waste any time,
 2. I suggest to have an option for autocropping "exactly" so the objects are
at the borders of the image as well as "framed" where there is a row/column of
empty pixels around the image.
Cheers

G.
Reply | Threaded
Open this post in threaded view
|

Re: Can't find Auto Crop Plugin

Gabriel Landini
In reply to this post by gabejackson
On Friday 16 October 2009  16:50:39 gabejackson wrote:
> Nevermind, i quickly wrote one... it's really ugly, but maybe somebody can
> use it :D

Ah, your message came after I sent mine.
Anyway, it would be also useful to use the options set in the Binary>Options
for foreground or background. I will add this if you do not mind.

Cheers
G
Reply | Threaded
Open this post in threaded view
|

Re: Can't find Auto Crop Plugin

aRUNArEDDY1
I am new to ImageJ and tried  Auto Crop  plug- it is not working for the  following image.




need help on this..
Reply | Threaded
Open this post in threaded view
|

Re: Can't find Auto Crop Plugin

andyg2
In reply to this post by Gabriel Landini
Gabriel Landini wrote
On Friday 16 October 2009  16:50:39 gabejackson wrote:
> Nevermind, i quickly wrote one... it's really ugly, but maybe somebody can
> use it :D

Ah, your message came after I sent mine.
Anyway, it would be also useful to use the options set in the Binary>Options
for foreground or background. I will add this if you do not mind.

Cheers
G
I'm very new to java but I've tried editing the autoCrop script to ignore single pixels by counting them. A kind of fuzziness factor. Alas, I've got nowhere. I think this would both compliment any revisions to this and help me out a great deal. If anyone wants to put me out of my sense of defeat it would mean a lot to me.

Here's the idea I have:


        int dotC = 0;
        for (int x=0; x<imp.getWidth(); x++) {
                for (int y=0; y<imp.getHeight(); y++) {
                                dotC++;
                                if(ip.get(x, y) == 255) {
                                        if(dotC == 50 || y >= imp.getHeight()) {
                                                maxX = x;
                                                break;
                                        }
                                }
                        }
                }
        }

50 being the number of dots to skip before triggering the crop location.

Adding the binary function would further assist me if you're planning that anyway :)
Reply | Threaded
Open this post in threaded view
|

Re: Can't find Auto Crop Plugin

Albert Cardona-2
2010/11/4 andyg2 <[hidden email]>:
> I'm very new to java but I've tried editing the autoCrop script to ignore
> single pixels by counting them. A kind of fuzziness factor. Alas, I've got
> nowhere. I think this would both compliment any revisions to this and help
> me out a great deal. If anyone wants to put me out of my sense of defeat it
> would mean a lot to me.


I would run a median filter first, for a given radius X, and then run
your autocrop on the filtered image. Then use that ROI to crop the
original image.

Albert
--
http://albert.rierol.net