Issue on copy and paste selection in Fiji


I am writing about an issue related to copy and pasting an image selection in Fiji

The problem: copying a certain selection from a duplicate back to its original image produces a random displacement of the selection, which results slightly shifted relative to it’s original position.

Below the full details.

I encountered the issue while writing a .ijm macro code that extract internal features of a grayscale image. In particular, the code does the following:

  1. Open an image containing an object internally divided in areas of specific grayscale intensity, as follow:
    * Internal feature 1
    * Internal feature 2
    * Internal feature 3
    * Area outside object (background)
  2. Make a duplicate
  3. threshold each particular grayscale intensity (i.e., an internal feature of the main object) on the duplicate image
  4. Clean, create and extract its selection by using the “Analyze particles…” command from the Analyze menu.
  5. Threshold the background of the original image, inverse the selection and fill it to create a homogeneous main object
  6. Repeat steps 3 and 4 for internal features 1 and 2, giving new grayscale intensities
  7. Finally, copy and past selections of internal features 1 and 2 on the homogeneous main object image obtained on step 5.

It’s important at this stage to remember that the image and its duplicate have the same size.
Now, as one should expect, pasting the selection from the duplicate back to the original image should place the selection exactly on top of its equivalent. Actually, performing a batch processing of multiple sample images of the same size and features produces the issue on a some of them while others are fine.

My thoughts are that the command copy and past selection somehow don’t constrain the exact position of the selection to the whole image canvas, causing the selection to be shifted randomly on pasting.

In attachment you can find two original images, with the first resulting shifted, whereas the second is ok.

The shifting occurs towards the NE and SW corner for two different internal features, respectively.

In my limited experience using Fiji, I do not think is a coding problem, as the actions leading to this issue are merely mechanic.

I am using Fiji on Microsoft Windows 10 Professional
Laptop HP ProBook 640 G2
x64-based PC

I stay at your disposal to provide any futher information and insight, and I look forward to this issue to be solved.


PierU-EM1_1p_E2_03_1_ORIG|nullxnull U-EM1_1p_E2_03_2_ORIG|nullxnull U-EM1_1p_E2_03_1_FINAL|nullxnull U-EM1_1p_E2_03_2_FINAL|nullxnull

Hello PPGeo -

I speculate that ImageJ is working correctly and the fact that your
copy-paste ever ends up where you want is an artifact of your
particular images.

ImagePlus.paste() (usually) pastes the contents of the clipboard
to the center of your ImagePlus.

From the javadoc for ImagePlus.paste():

Looking at the images you posted, it looks like the “object” in your
image is nicely, but not necessarily perfectly, centered in the overall
image. If the bounding box of your selection happens to be exactly
centered in your image, then when you copy-paste it back into your
original image, it will end up where you expect (in the center). But
if your selection is not quite in the center, say five pixels to the left,
then when you copy-paste it, it will still end up in the center, which
would be shifted five pixels from where you expected.

(If this does not explain what you are seeing, you will have to provide
more information – specifically, a shortened, minimal version of the
code you are running, together with sample images, that displays the

Here is a jython script that shows how ImagePlus.paste() pastes
into the center of the image, and then shows how you can explicity
move the pasted selection if you want to. (So, for your use case,
you could determine the upper left corner of your selection,
copy-paste it to the center, and then move your copy-pasted selection
so it has the desired upper left corner.)

from ij import IJ
imp = IJ.createImage ('copy-paste', '8-bit ramp', 512, 512, 1)
imp2 = imp.duplicate()
imp.setRoi (64, 64, 128, 128)
print imp2.getRoi()  # in center of image
imp2.getRoi().setLocation (320, 320)
print imp2.getRoi()  # moved to new location

Thanks, mm

1 Like

Hi mm,

Thank you very much for your answer!, this perfectly explains my problem and of course makes me feel a bit dull to have thought I found a bug. Haha.

I was just about to write a reply here as I thought that some Set Measurements features such as Centroid and center of mass might somehow cause the problem, then I saw your reply.

As my code is .ijm, do you know about a way to allow copy and paste centered, something that can be implemented between the step copy and paste commands to be precise.
Thanks a lot!!

Hello PPGeo -

I do not know how to do precisely what you ask – I’m not that
experienced with .ijm macros.

But I imagine that you could mimic in .ijm what I illustrated in the
jython script I posted, that is, let paste paste to the center of the
image, and then move the pasted selection (ROI) to where you
want it.

The .ijm documentation, Built-in Macro Functions, has a section
on ROI Functions. I haven’t tried it, but

looks promising.

Thanks, mm