Home » Python » pandas – Merging images into z-stack and making max-projection / in Python-Exceptionshub

pandas – Merging images into z-stack and making max-projection / in Python-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

It would be great if someone could me help me to write code in Python (which I am gradually learning) to solve the problem I am facing.

We are using high-throughput microscope, and I am imaging 96 well plates – a z-stacks of various locations in multiple wells. When I extract images, their name encodes row (rXX, it is from 01 to 08), column (cXX it is from 01 to 12) field (fxx, it is from 01 to any number), plane (pXX, its from 01 to a number of steps in z-stack), ch1 – ch6 imaging channels. Example is below:

r04c02f01p01-ch1sk1fk1fl1.tiff;
r04c02f01p01-ch2sk1fk1fl1.tiff;
r04c02f01p01-ch3sk1fk1fl1.tiff;
r04c02f01p01-ch4sk1fk1fl1.tiff;
r04c02f01p01-ch5sk1fk1fl1.tiff;
r04c02f01p01-ch6sk1fk1fl1.tiff

<…>

r04c02f01p16-ch1sk1fk1fl1.tiff;
r04c02f01p16-ch2sk1fk1fl1.tiff;
r04c02f01p16-ch3sk1fk1fl1.tiff;
r04c02f01p16-ch4sk1fk1fl1.tiff;
r04c02f01p16-ch5sk1fk1fl1.tiff;
r04c02f01p16-ch6sk1fk1fl1.tiff;

as following this example, I would like to get Z-stack of each channel and MAX intensity projection of each channel, likes this:

ST_r02c04f01-ch1.tiff;
ST_r02c04f01-ch2.tiff;
ST_r02c04f01-ch3.tiff;
ST_r02c04f01-ch4.tiff;
ST_r02c04f01-ch5.tiff;
ST_r02c04f01-ch6.tiff;

and

MAX_r02c04f01-ch1.tiff;
MAX_r02c04f01-ch2.tiff;
MAX_r02c04f01-ch3.tiff;
MAX_r02c04f01-ch4.tiff;
MAX_r02c04f01-ch5.tiff;
MAX_r02c04f01-ch6.tiff;

There are usually 30+ field imaged per one well, and about 60 wells, thus there about 1800 fields of view.

I tried to something like this:

import numpy as np
import os
import matplotlib.pyplot as plt
import numpy as np
import multiprocessing 
import pandas as pd
from tifffile import imsave
from skimage import io
import skimage.io
import skimage.exposure
import skimage.morphology
import skimage.measure
import skimage.transform
import skimage.segmentation
from PIL import Image, TiffTags

dir = 'D:/

listfiles =[]
for img_files in os.listdir(dir):
    if img_files.endswith(".tiff"):
        listfiles.append(img_files)

first_image = io.imread(dir+listfiles[0])

io.imshow(first_image)

first_image.shape

stack = np.zeros((5,first_image.shape[0],first_image.shape[1]),np.uint16)

for n in range(0,5):
    stack[n,:,:]= io.imread(dir+listfiles[n])  

io.imshow(stack[0])

IM_MAX= np.max(stack, axis=0)
plt.imshow(IM_MAX)

However, I want to select specific images as oppose to first 8 or something on a list.

Please let me know if you have any suggestions or solutions to this problem.

Thank you for the help in advance!

How to&Answers: