Login  Register

Re: Copying an 8-bit image

Posted by Michael Schmid on Nov 09, 2010; 9:12am
URL: http://imagej.273.s1.nabble.com/Copying-an-8-bit-image-tp3686474p3686477.html

Hi Florian,

in newImage.setStack you use the original stack (containing the  
original data), not a copy of it.

Note that objects in Java are passed to methods by reference  
(pointer), they are not duplicated. Only the primitive variables like  
int, double, etc., are duplicated when they appear in the argument list.

You could use the ij.plugins.Duplicator.run(ImagePlus imp) method to  
duplicate an ImagePlus (containing a stack or not), This also  
duplicates the image data.

If you don't want to copy the image properties (calibration,  
info...), just the bare data, you have to extract the slices one by  
one, duplicate the ImageProcessors, and add them to the new ImagePlus.
ImageProcessor.duplicate() creates a copy of the data. (There is no  
method that I am aware of that duplicates an ImageStack with a single  
command. ImageStack.clone() won't duplicate the data).

Hope this helps,

Michael
________________________________________________________________

On 9 Nov 2010, at 09:41, Florian Leitner wrote:

> Hi everybody,
>
> I wrote a plugin. One part of the plugin copies the current image and
>
> does some transformations to the copied image. Everything works as
>
> it is supposed to work but if the original image is an 8-bit image
>
> all the transformations to the copied image will also be applied
>
> to the original image.
>
> e.g.:
> 1. open an image, convert it to 8-bit
> 2. execute Test_BDP.java [1]
> 3. go to e.g. process -> binary -> make binary
> 4. save the original image - it'll be a binary picture
> 5. repeat steps 1.-4. with e.g. a 16-bit image
>
>
> I tried it with different ImageJ versions and OS.
>
> Thanks for your help.
>  Florian
>
>
> [1]:
>
> import ij.*;
>
> public class Test_BDP {
>
> public Test_BDP() {
>
> ImagePlus imageCopy;
>
> ImagePlus imp = WindowManager.getCurrentImage();
> imageCopy = copyImage(imp);
> // imageCopy = copyImage2(imp);
> imageCopy.show();
> }
>
>   ImagePlus copyImage(ImagePlus imp) {
> ImagePlus newImage;
>
> newImage = imp.createImagePlus();
> newImage.setStack("Copy " + imp.getTitle(), imp.getStack());
> return newImage;
> }
>
>
> ImagePlus copyImage2(ImagePlus imp) {
> ImageProcessor ip = imp.getProcessor();
> return new ImagePlus("Copy " + imp.getTitle(), ip);
> }
>
> }