Search code examples
pythonnumpyimage-processingdata-scienceimagej

How to do a Z-projection (like in ImageJ) using numpy arrays?


I'm currently processing Z-stack images of some cells I took. I want to project these images similar to the ZProjection function in ImageJ.

After importing them (see below), I have a 3d stack of images. I continue with perforing a numpy function on the 3rd / Z-dimension as follows:

import numpy as np
projection = np.**(img, axis=2)

** == max, std, sum, mean

When I plt.imshow the different mathematical operations, however, I don't see any differences and they most certainly don't look like the ImageJ ones (see comparison below). Maybe anyone has an idea on what is going wrong.

Thanks in advance, BBQuercus :)

–––––––––––

Image import:

import os
import numpy as np

# File path
root = '/Folder'

# Import all images in root
img = np.zeros((1002, 1004)) # Pixel size

for file in os.listdir(root):
    if not file.endswith('.tif'):
        continue
    _img = imread(os.path.join(root, file))
    if 'c1' in file:
        img = np.dstack((img, _img))

Images (full dataset is too large to show), Top – numpy.std, Bottom – ImageJ std (on full stack):

Numpy STD

ImageJ STD (full stack)


Solution

  • I answered it using the following, pretty cool commands:

    import fijibin.macro
    
    in_folder = '/Infolder'
    out_name = '/Outname'
    
    macro = """run("Image Sequence...", "open=[{}] file=c1 sort use");
    run("Z Project...", "projection=[Standard Deviation]");
    saveAs("Tiff", "{}");
    close();
    close();""".format(in_folder, out_name)
    
    fijibin.macro.run(macro)
    

    Then I just import the outfile into a new np.array.

    Maybe someone can use it for the future.