Nuclei Deconvolution and Segmentation#

This notebook shows deconvolution and segmentations of an image from the ABRF-LMRG Image Analysis Study. Details about the study can be found here

In this example we also learn how to use a theoretical PSF (instead of loading a PSF file).

Question: Does deconvolution really make downstream measurements more accurate? In this example we deconvolve, segment and then compare the segmentation to the ground truth segmentation provided by the study organizers. We use the stardist matching utility to score the segmentation against the ground truth segmentation.

Discovering whether deconvolution improves measurements is tricky, because the optimal segmentaton approach for original and deconvolved image may be different. Someone segmenting the original may come up with a sophisticated segmentation strategy that compensates for the blur without deconvolution, others may apply deep learning to the problem. We invite people to score their own solutions.

Open test images#

  1. Get images from this folder

  2. Beside the PoL-BioImage-Analysis-TS-GPU-Accelerated-Image-Analysis folder create an images folder and place the deconvolution folder inside of it.

  3. In the code snippet below change im_path to the local location on your machine where you put the above folder

  4. Print out the size of the images to verify they loaded propertly. Note that the ground truth and image are different sizes, that is something we will have to deal with (with a careful resizing operation) before comparing.

from import imread
from decon_helper import image_path


im=imread(image_path / image_name)
truth=imread(image_path / truth_name)
print(im.shape, truth.shape)
tnia available
stackview available
(100, 258, 258) (161, 258, 258)

Resize truth, show image and truth#

We need to resize the truth image as it was generated with isotropic voxels. We have to be careful when resizing a label image as we don’t want to interpolate label vales. Thus the order of the resizing operation needs to be 0 (which means no interpolation will be used).

from tnia.plotting.projections import show_xyz_max
from skimage.transform import resize

truth = resize(truth, [im.shape[0],im.shape[1], im.shape[2]], preserve_range=True, order=0, anti_aliasing=False).astype('int32')

../_images/cd62c26416bed5b3fb3cc82a106617c16b34bcc1db40ba44bd08361cce691964.png ../_images/2ce13653f254eba87a443680c0eec6047b6b3860a1587780376657a6a7c1a20f.png

Create Theoretical PSF#

In this example we use a theoretical PSF instead of loading a PSF file.

The image is a 3D Z-Stack Widefield fluorescence image, so deconvolution should help remove blur, increase contrast and lead to better segmentation and quantification. One hiccup I had was that the data did not deconvolve well using the meta data provided (20x/0.75NA, Voxel dimensions: 0.124x0.124x0.200 um, Emission peak wavelength: 500 nm). I followed up with the organizers about this and they discovered there was an error reading meta data from the psf file used in the simulation. This meant the effective spaces were likely 372 nm in XY and 2800 nm in Z.

from tnia.deconvolution.psfs import gibson_lanni_3D

x_voxel_size = 0.372
#x_voxel_size = 0.1



psf  = gibson_lanni_3D(NA, ni, ns, x_voxel_size, z_voxel_size, xy_psf_dim, z_psf_dim, 0, 0.5, False, True)
psf = psf.astype('float32')
fig = show_xyz_max(psf)

Import Deconvolotion and make a ‘deconvolver’#

    from clij2fft.richardson_lucy import richardson_lucy_nc
    print('clij2fft non-circulant rl imported')
    def deconvolver(img, psf, iterations):
        return richardson_lucy_nc(img, psf, iterations, regularization_factor)
except ImportError:
    print('clij2fft non-circulant rl not imported')
        import RedLionfishDeconv as rl
        print('redlionfish rl imported')
        def deconvolver(img, psf, iterations):
            return rl.doRLDeconvolutionFromNpArrays(img, psf, niter=iterations, method='gpu', resAsUint8=False )
    except ImportError:
        print('redlionfish rl not imported')
clij2fft non-circulant rl imported
im_decon = deconvolver(im, psf, 200)

fig = show_xyz_max(im_decon)
get lib

Richardson Lucy Started
0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 
Richardson Lucy Finished

Make a Segmenter#

This segmenter has been optimized for deconvolved data. The important steps are

  1. Resize the image to isotropic voxels (this helps the performance of the downstream watershed)

  2. Apply multiple levels of Otsu threshold to detect separate both the bright and dark parts of the nuclei from the background.

  3. After 2 levels of Otsu fill any remaining holes

  4. Call a watershed function passing in a ‘spot sigma’ and ‘distance sigma’. The spot sigma is used to blur the image before peak detection. The distance sigma is used to blur the image before calculating a distance map. Spot sigma is ussually larger than distance sigma.

  5. As a final step, loop through all detected objects and apply binary cleaning (erosion, opening and closing) to each object separately.

from tnia.morphology.fill_holes import fill_holes_3d_slicer, fill_holes_slicer
from skimage.filters import threshold_otsu
from tnia.morphology.fill_holes import fill_holes_3d_slicer, fill_holes_slicer
from skimage.measure import label
from tnia.segmentation.separate import separate_touching2, separate_touching
import numpy as np
from skimage.morphology import binary_closing, binary_opening, binary_erosion, ball
from skimage.measure import regionprops


def segmenter(im):
    # resize to isotropic voxels (watershed tends to work better with isotropic voxels)
    resized= resize(im, [int(sz*im.shape[0]/sx),im.shape[1], im.shape[2]])
    # apply 2 levels of Otsu threshold
    # first apply Otsu threshold to entire image
    binary = resized>threshold_otsu(resized)
    # now apply Otsu threshold to the background from 1st Otsu application
    # (the nuclei have dark regions for which a second application of Otsu is needed)
    binary = resized>threshold_otsu(resized[binary==0])
    # fill any remaining holes 
    # now call a customized watershed to seperate objects
    labels, _, = separate_touching2(binary, binary, 5, [15,15,15],[5,5,5])
    # resize back to original
    labels = resize(labels, [im.shape[0],im.shape[1], im.shape[2]], preserve_range=True, order=0, anti_aliasing=False).astype('int32')
    # get the regions

    # here we loop through each object detected applying opening and closing to them separately to 
    # clean noise from the surface and fill gaps in the surface
    for obj in object_list:
        temp=binary_erosion(temp, ball(1))
        temp=binary_opening(temp, ball(r))
        temp=binary_closing(temp, ball(r))
    return labels2

Apply the segmenter to the deconolved data#

from skimage.measure import label


fig = show_xyz_max(labels)
(5, 3)

Score the solution using the jaccard index#

from decon_helper import jaccard_index_binary

jaccard_index_binary(truth, labels)

Visualize in Napari#

# relabel before visualization to make sure the same label indexes are used

import napari

<Labels layer 'labels' at 0x311d80cd0>