get Subimage from ImagePlus

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

get Subimage from ImagePlus

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

Re: get Subimage from ImagePlus

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

Re: get Subimage from ImagePlus

Wayne Rasband
In reply to this post by Michael Strupp
 > 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?

Yes. Here is an example (in JavaScript):

   imp = IJ.openImage("http://rsb.info.nih.gov/ij/images/ct.dcm.zip");
   ip = imp.getProcessor();
   ip.setRoi(233, 0, 400, 400);
   imp.setProcessor(null, ip.crop());
   imp.show();

-wayne
Reply | Threaded
Open this post in threaded view
|

Re: get Subimage from ImagePlus

Bill Mohler
Are the cropped-out pixels of the image automatically flushed from
memory when setProcessor(null,ip.crop()) is performed?

Also, can this be run on each slice of a virtual stack as it is called
up in an imageProcessor to be displayed?  In other words, can one
dynamically crop display of a VirtualStack without the data all being
loaded into memory?

Thanks,
Bill

Wayne Rasband wrote:

>  > 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?
>
> Yes. Here is an example (in JavaScript):
>
>    imp = IJ.openImage("http://rsb.info.nih.gov/ij/images/ct.dcm.zip");
>    ip = imp.getProcessor();
>    ip.setRoi(233, 0, 400, 400);
>    imp.setProcessor(null, ip.crop());
>    imp.show();
>
> -wayne
>
>  
Reply | Threaded
Open this post in threaded view
|

Re: get Subimage from ImagePlus

Wayne Rasband
In reply to this post by Michael Strupp
 > Are the cropped-out pixels of the image automatically
 > flushed from memory when setProcessor(null,ip.crop())
 > is performed?

The Java garbage collector should automatically reclaim the memory.

 > Also, can this be run on each slice of a virtual stack
 > as it is called up in an imageProcessor to be
 > displayed?  In other words, can one dynamically crop
 > display of a VirtualStack without the data all being
 > loaded into memory?

You can but the images must be saved to disk as they are cropped. This
is what the new Process>Batch>Virtual Stack command does. Here are the
steps needed to use it to crop a virtual stack:

1. Open a virtual stack
2. Run Process>Batch>Virtual Stack
3. Select an output folder and format
4. Select "Crop" from the "Add Macro Code" menu
5. Edit the macro as needed
6. Click "Test" to verify macro
7. Click "Process" to create the cropped virtual stack

-wayne