Re: Modified input image

Posted by Adrian Daerr-2 on
URL: http://imagej.273.s1.nabble.com/Modified-input-image-tp3686496p3686498.html

> Arne Seitz wrote:
>> [...] the input image is changed [...]
>
Johannes Schindelin answered:
>
> I think the problem is that you haven't duplicated the processor, and as
> such, you are reusing the very same pixels.

Just a small explanation because I think Johannes' statement is
confusing even though it is (unsurprisingly) correct: instantiating a
new Processer (with the 'new' keyword) /does/ give you a processor
distinct from the original. And it even becomes a duplicate of the
original by virtue of the initialisation from the original image's
properties. The problem is that by giving it a pointer to the initial
image's pixel array, Arne has it point to the same pixel data. So the
processor is a duplicate, but not the associated pixel data.

To phrase it differently: with 'new ShortProcessor(<initialisation from
imp's properties>)' Arne created a /shallow/ duplicate of the other
processor, whereas the processor's duplicate() method does a /deep/ copy
which also duplicates the pixel data. The source code of the
ShortProcessor.duplicate() method is self-explanatory.
http://rsb.info.nih.gov/ij/developer/source/index.html

Obviously Johannes suggestion to use duplicate() is way to go:

> ShortProcessor ip  = imp.getProcessor().duplicate();

but one could also have corrected Arne's code by explicitly copying the
pixel data. Compare the following

[original]
ShortProcessor ip = new ShortProcessor(imp.getWidth(), imp.getHeight(),
  (short[]) imp.getProcessor().getPixels(),
  imp.getProcessor().getColorModel());

[with arraycopy]
short[] pixels = (short[]) imp.getProcessor().getPixels();
ShortProcessor ip = new ShortProcessor(imp.getWidth(), imp.getHeight(),
  java.util.Arrays.copyOf(pixels, pixels.length),
  imp.getProcessor().getColorModel());


Hope this blabla will actually be insightful for somebody,
happy imagejing,
Adrian