Login  Register

Re: get Subimage from ImagePlus

Posted by Burger Wilhelm on Oct 21, 2009; 4:34pm
URL: http://imagej.273.s1.nabble.com/get-Subimage-from-ImagePlus-tp3690681p3690685.html

Hello Michael,

incidentially I needed a similar functionality a few days back. Below is simple plugin for this task, perhaps it helps.

Regards,
Wilhelm
www.imagingbook.com


//---------------------------------------------------

import ij.ImagePlus;
import ij.gui.GenericDialog;
import ij.gui.Roi;
import ij.plugin.filter.PlugInFilter;
import ij.process.ImageProcessor;
import java.awt.Rectangle;

public class Extract_Subimage implements PlugInFilter {
       
        static int left = 0;
        static int top = 0;
        static int height = 100;
        static int width = 100;
       
        ImagePlus imp = null;
       
    public int setup(String arg, ImagePlus imp) {
    this.imp = imp;
        return DOES_ALL + NO_CHANGES;
    }

    public void run(ImageProcessor ip) {
        Roi roi = imp.getRoi();
        if (roi != null) {
                Rectangle rect = roi.getBoundingRect();
                left = rect.x;
                top = rect.y;
                width = rect.width;
                height = rect.height;
        }
       
    if (!getParameters())
    return;
   
    ip.setRoi(left,top,width,height);
    ImageProcessor ip2 = ip.crop();
    String title = imp.getTitle() + " (cropped)";
    ImagePlus imp2 = new ImagePlus(title,ip2);
    imp2.show();
    }
   
    boolean getParameters() {
                GenericDialog gd = new GenericDialog("Gaussian Filter");
                gd.addNumericField("Left", left, 0);
                gd.addNumericField("Top", top, 0);
                gd.addNumericField("Width", width, 0);
                gd.addNumericField("Height", height, 0);
                gd.showDialog();
                if(gd.wasCanceled()) return false;
                left = (int)gd.getNextNumber();
                top = (int)gd.getNextNumber();
                width = (int)gd.getNextNumber();
                height = (int)gd.getNextNumber();
                return true;
    }
}

//---------------------------------------------------

-----Original Message-----
From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of Michael Strupp
Sent: Wednesday, October 21, 2009 5:46 PM
To: [hidden email]
Subject: get Subimage from ImagePlus

Hello community

I need to cut a subimage from an ImagePlus object.
For example I have an ImagePlus object with the size 800X800 and I need just a part of this image of the size 400X400.

Is there a simple way to do this?

Kind regards,
Michael
--
Neu: GMX DSL bis 50.000 kBit/s und 200,- Euro Startguthaben!
http://portal.gmx.net/de/go/dsl02