Complex exercise
Contents
Complex exercise#
In this complex exercise we will setup a workflow for multiple processing images in a folder. We will measure properties of the segmented nuclei and stored them in one big table.
If you have any questions and we are not around, feel free to open a thread on image.sc
import napari
import pandas as pd
from skimage.io import imread
from napari_simpleitk_image_processing import label_statistics
import os
import seaborn
import matplotlib.pyplot as plt
data_path = "../../data/BBBC007_batch/"
Load an image from the specified folder and open it in napari.
viewer = napari.Viewer()
image = imread("../../data/BBBC007_batch/17P1_POS0013_D_1UL.tif")
viewer.add_image(image)
Image segmentation#
Set up an image segmentation workflow for this image to segment nuclei. Do this using the napari-assistant and export a notebook. Copy paste the code from this notebook into this function:
def segment_image(image):
# enter code here
return label_image
test_label_image = segment_image(image)
test_label_image
Feature extraction#
Use the following function body to program a routine that takes the image, a corresponding label image and retrieve measurements. You may for example use the function label_statistics
from napari_simpleitk_image_processing
. Make sure your function creates a pandas DataFrame.
def analyze_image(image, label_image):
# enter code here
return pd.DataFrame(statistics)
analyze_image(image, test_label_image)
Batch processing#
Use the following for-loop to iterate through the folder, segment all images, analyse the images, create data frames and concatenate all results in one table.
results = pd.DataFrame()
for file_name in os.listdir(data_path):
if file_name.endswith('.tif'):
file_path = os.path.join(data_path, file_name)
# enter code here
# save from which image the measurements were derived from
table['file_name'] = file_name
# collect results
results = pd.concat([results, table])
results
Save the resulting measurements to disk.
Plotting#
Extract area measurements from two different images from the table above and draw a box plot.
fig, ax = plt.subplots(figsize = (10, 5))
# enter code here