Search code examples
pythonvideopixeltiffscikit-image

Calculate average pixel intensity for each frame of tif movie


I imported a tif movie into python which has the dimensions (150,512,512). I would like to calculate the mean pixel intensity for each of the 150 frames and then plot it over time. I could figure out how to calculate the mean intensity over the whole stack (see below), but I am struggling to calculate it for each frame individually.

from skimage import io

im1 = io.imread('movie.tif')


print("The mean of the TIFF stack is:")
print(im1.mean())

How can I get the mean pixel intensities for each frame?


Solution

  • You could slice the matrix and obtain the mean for each frame like below

    from skimage import io
    
    im1 = io.imread('movie.tif')
    for i in range(im1.shape[0]):
        print(im1[i,:,:].mean())
    

    To plot it you can use a library like matplotlib

    from skimage import io
    import matplotlib.pyplot as plt
    
    im1 = io.imread('movie.tif')
    y = []
    for i in range(im1.shape[0]):
        y.append(im1[i,:,:].mean())
    
    x = [*range(0, im1.shape[0], 1)]
    plt.plot(x,y)
    plt.show()