Skip to content

Commit

Permalink
Minor changes to filter_mean_skimage_napari.py
Browse files Browse the repository at this point in the history
  • Loading branch information
tischi committed May 6, 2024
1 parent c739c2b commit f7b8156
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 42 deletions.
32 changes: 12 additions & 20 deletions _includes/auto_threshold/auto_threshold_act1_skimage_napari.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# %% [markdown]
# ## Apply manual and automated thresholds
# %%
# Apply manual and automated thresholds

# %%
# initial imports
Expand All @@ -8,31 +8,23 @@
from OpenIJTIFF import open_ij_tiff

# %%
# Read the images
image1, axes1, scales1, units1 = open_ij_tiff('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__nuclei_without_offset.tif')
image2, axes2, scales2, units2 = open_ij_tiff('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__nuclei_with_offset.tif')
# Read two images
image1, *_ = open_ij_tiff('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__nuclei_without_offset.tif')
image2, *_ = open_ij_tiff('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__nuclei_with_offset.tif')

# %%
# Inspect image data type and values
print(image1.dtype, image1.shape, np.min(image1), np.max(image1))
print(image2.dtype, image2.shape, np.min(image2), np.max(image2))
# Inspect image data type and value range
print(image1.dtype, image1.shape, image1.min(), image1.min())
print(image2.dtype, image2.shape, image2.min(), image2.max())

# %%
# Instantiate the napari viewer and display the images
viewer = napari.Viewer()


# %%
# Add the images
viewer.add_image(image1, name='image1')
viewer.add_image(image2, name='image2')

# %%
# Explore the values of the image
info_type = np.iinfo(image1.dtype)
print('\n', info_type)
print('\n Min image1 %d max image1 %d' % (image1.min(), image1.max()))
print('\n Min image2 %d max image2 %d' % (image2.min(), image2.max()))
viewer.add_image(image1, name="image1")
viewer.add_image(image2, name="image2")

# %%
# Show the histogram
Expand Down Expand Up @@ -73,6 +65,6 @@
viewer.add_image(mean_thresholded1, name='mean_thresholded1', opacity = 0.4, colormap='magenta')
viewer.add_image(mean_thresholded2, name='mean_thresholded2', opacity = 0.4, colormap='magenta')

# %% [markdown]
# Explore other thresholding options \
# %%
# Explore other thresholding options
# Note that there exists a threshold_multiotsu function to handle cases with multi-peaks histograms
46 changes: 24 additions & 22 deletions _includes/filter_neighbourhood/filter_mean_skimage_napari.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# %% [markdown]
# ## Applying mean filters to an image
# %%
# Apply mean filters to an image to aid foreground background segmentation

# %%
# Instantiate the napari viewer
Expand All @@ -9,46 +9,48 @@

# %%
# Read the intensity image
image, axes, scales, units = open_ij_tiff('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__nuclei_very_noisy.tif')
image, *_ = open_ij_tiff('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__nuclei_very_noisy.tif')

# %%
# View the intensity image
# View the image
# - Appreciate that is is quite noisy
# - Inspect the pixel values to find a threshold that separates the nuclei from the background
viewer.add_image(image)

# %%
# Appreciate that one cannot segment the nuclei by a simple intensity threshold
# Binarise the image
# - Appreciate that one cannot segment the nuclei by a simple intensity threshold
binary_image = image > 40
viewer.add_image(binary_image)

# %%
# Define a circular structural element with a radius of 1 pixel
# Prepare filtering the image by defining a circular structural element with a radius of 1 pixel
from skimage.morphology import disk
radius = 1
disk_radius1 = disk(radius)
print(disk_radius1)
disk_radius_1 = disk(1)
print(disk_radius_1)

# %%
# Apply a mean filter with the structural element
# Apply a mean filter to the image with the above structural element
from skimage.filters.rank import mean
mean_disk_radius1 = mean(image, disk_radius1)
mean_image_1 = mean(image, disk_radius_1)

# Add the filtered image to napari
# Napari: Zoom in to appreciate that the filtered image
# contains the local mean values of the raw image
viewer.add_image(mean_disk_radius1)
# Napari:
# - Zoom in to appreciate that the filtered image contains the local mean values of the raw image
viewer.add_image(mean_image_1)

# %%
# One still cannot readily segment the nuclei
# (rerun the below code with different thresholds)
binary_mean_disk_radius1 = mean_disk_radius1 > 35
viewer.add_image(binary_mean_disk_radius1)
# Binarise the filtered image
# - Appreciate that one still cannot readily segment the nuclei
binary_image_1 = mean_image_1 > 35
viewer.add_image(binary_image_1)

# %%
# Apply mean filter with a disk of radius 3
mean_disk_radius3 = mean(image, disk(3))
viewer.add_image(mean_disk_radius3)
mean_image_3 = mean(image, disk(3))
viewer.add_image(mean_image_3)

# %%
# Now the nuclei can be segmented
binary_mean_disk_radius3 = mean_disk_radius3 > 32
viewer.add_image(binary_mean_disk_radius3)
binary_image_3 = mean_image_3 > 32
viewer.add_image(binary_image_3)

0 comments on commit f7b8156

Please sign in to comment.