0

In Fiji (ImageJ) I have two images open (Img1 and Img2). I want run a script that adds both images and stores the result in Img1. I'm boing to do this in a sequence of images so I would like to try to avoid creating and closing many images.

Would this be possible? I tried the code below but it crashes when I call the second Sum3and50.show() after the first AddSlice() call. Basically I would love to just be able to to Sum3and50+=imp[Slice]

from __future__ import division
from ij import IJ
from ij import plugin
import time

def AddSlice(Stack,SumImg,Slice):
    Stack.setSlice(Slice)
    ic = plugin.ImageCalculator()
    SliceImg = ic.run("Copy create", Stack, Stack)
    SliceImg.show()
    time.sleep(SLEEP_TIME)  
    SumImg=ic.run("Add RGB", SumImg, SliceImg)
    return SumImg

SLEEP_TIME=1 #seconds    

#imp = IJ.getImage()
imp = IJ.openImage("http://imagej.nih.gov/ij/images/flybrain.zip");
W,H,NCh,NSl,NFr = imp.getDimensions()
imp.show()
Sum3and50 = IJ.createImage("Sum3and50", "RGB black", W, H, 1)
Sum3and50.show()
time.sleep(SLEEP_TIME)  

Sum3and50 = AddSlice(imp,Sum3and50,3)
Sum3and50.show()
time.sleep(SLEEP_TIME)  

Sum3and50 = AddSlice(imp,Sum3and50,5)
Sum3and50.show()
Miguel
  • 1,293
  • 1
  • 13
  • 30

1 Answers1

1

To avoid windows popping up I tend to avoid plugins and work with ImageProcessor directly. Such a function that takes the sum of each pixel pair of two images overwriting the first input would look like:

def pixel_pair_sum(pro1, pro2):
    for x in range(pro1.getWidth()):
        for y in range(pro1.getHeight()):
            v1 = pro1.get(x, y)
            v2 = pro2.get(x, y)    
            pro1.set(x, y, v1 + v2)

pro1 and pro1 are ImageProcessors [1]. So you need to to get those first from the ImagePlus before calling the function above:

...
sum3and50 = IJ.createImage("Sum3and50", "RGB black", W, H, 1)
p1 = sum3and50.getProcessor()

stk = imp.getStack()

p2 = stk.getProcessor(1) # get the processor for the first slice [2]
pixel_pair_sum(p1, p2) # add the pixel values of slice 1 to sum3and50

p2 = stk.getProcessor(2) # add another slice to sum3and50
pixel_pair_sum(p1, p2)
...
sum3and50.show()

for reference: https://imagej.nih.gov/ij/developer/api/ij/ImageStack.html
[1] https://imagej.nih.gov/ij/developer/api/ij/process/ImageProcessor.html
[2] also see: https://imagej.nih.gov/ij/developer/api/ij/ImagePlus.html#setPositionWithoutUpdate-int-int-int-

felix the cat
  • 165
  • 2
  • 9
  • Thank you for the nice answer. And thank you for referring the `setPositionWithoutUpdate`. I ended up using it as: `imp.setPositionWithoutUpdate(1,slice,1) p2 = imp.getProcessor()`. The only thing that I'm surprised is that the `ImageProcessor` class doesn't have a function `add(ImageProcessor input)` – Miguel Jan 04 '18 at 23:19
  • @Miguel the `add(ImageProcessor p)` method does probably not exist, because of the ambiguity for different images types (grey, rgb, etc., see https://imagej.nih.gov/ij/developer/api/ij/process/ImageProcessor.html#getNChannels--) – felix the cat Jan 05 '18 at 08:48