ImageJ脚本:如何将一个图像添加到现有图像

时间:2018-01-04 00:28:06

标签: python imagej fiji

在斐济(ImageJ),我打开了两张图片(Img1和Img2)。我想运行一个脚本,添加两个图像并将结果存储在Img1中。我想在一系列图像中做到这一点,所以我想尽量避免创建和关闭许多图像。

这可能吗?我尝试了下面的代码,但在第一次Sum3and50.show()调用后拨打第二个AddSlice()时崩溃了。基本上我希望能够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()

1 个答案:

答案 0 :(得分:1)

为了避免窗口弹出,我倾向于避免使用插件并直接使用ImageProcessor。这种将两个图像的每个像素对的总和重写为第一个输入的函数看起来像:

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)

pro1pro1ImageProcessor s [1]。 因此,在调用上述函数之前,您需要先从ImagePlus获取这些内容:

...
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()

供参考:https://imagej.nih.gov/ij/developer/api/ij/ImageStack.html
[1] https://imagej.nih.gov/ij/developer/api/ij/process/ImageProcessor.html
[2]另见:https://imagej.nih.gov/ij/developer/api/ij/ImagePlus.html#setPositionWithoutUpdate-int-int-int-