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); } } |
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); > } > > } |
In reply to this post by Florian Leitner
Hi Michael,
thank you for your answer. Now I use ij.plugins.Duplicator.run(ImagePlus imp) and it works fine. I understand your explanation but I still don't get it why it worked with all image types except 8-bit images. According to my understanding it should not have worked with any image types correctly? Thanks again, Florian |
Hi Florian,
you are right, it should not work with other data types either, ImagePlus.setStack should simply put the same image data into the new image without duplicating them, irrespective of image type. But you mention converting to 8 bits, maybe it is related to that? Of course, the 8-bit data are different from the original 16-bit or 32- bit data, so modifying the 8-bit images cannot affect the original data. Michael ________________________________________________________________ On 9 Nov 2010, at 11:25, Florian Leitner wrote: > Hi Michael, > > thank you for your answer. > Now I use ij.plugins.Duplicator.run(ImagePlus imp) and it works fine. > > I understand your explanation but I still don't get it why it > worked with all image types except 8-bit images. According to my > understanding it should not have worked with any image types > correctly? > > Thanks again, > Florian |
Free forum by Nabble | Edit this page |