Skip to content
Snippets Groups Projects
Commit 0aa4ce89 authored by fima's avatar fima :beers:
Browse files

Simple threshold mask

parent f5341747
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:1b1c18fb tags: %% Cell type:markdown id:0f62fd3b tags:
   
# Simple 3D binary mask # Simple 3D binary mask
author: Felipe Delestro (fima@dtu.dk) author: Felipe Delestro (fima@dtu.dk)
   
%% Cell type:code id:ca8099b3 tags: %% Cell type:code id:0a23b431 tags:
   
``` python ``` python
import qim3d import qim3d
import numpy as np import numpy as np
import scipy.ndimage import scipy.ndimage
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from skimage import filters from skimage import filters
``` ```
   
%% Cell type:markdown id:ed302618 tags: %% Cell type:markdown id:f9ad6ad1 tags:
   
## Helper functions and parameters ## Helper functions
   
%% Cell type:code id:c0963af5 tags: %% Cell type:code id:7a3aa8a7 tags:
   
``` python ``` python
def viz(vol, cmap="viridis"): def viz(vol, cmap="viridis"):
qim3d.viz.slice_viz(vol, position=np.linspace(50,100, num=6).astype(int), img_height=4, cmap=cmap) qim3d.viz.slice_viz(vol, position=np.linspace(50,100, num=6).astype(int), img_height=4, cmap=cmap)
   
def histogram(vol, vline=None): def histogram(vol, vline=None):
   
# Calculate histogram # Calculate histogram
vol_hist, bin_edges = np.histogram(vol, bins=16) vol_hist, bin_edges = np.histogram(vol, bins=16)
   
# Plot histogram # Plot histogram
fig, ax = plt.subplots(figsize=(6, 4)) fig, ax = plt.subplots(figsize=(6, 4))
ax.bar(bin_edges[:-1], vol_hist, width=np.diff(bin_edges), ec="white", align="edge") ax.bar(bin_edges[:-1], vol_hist, width=np.diff(bin_edges), ec="white", align="edge")
   
if vline: if vline:
ax.axvline(vline, color="#FF3366", linestyle="--") ax.axvline(vline, color="#FF3366", linestyle="--")
   
# Adjustments # Adjustments
ax.spines["right"].set_visible(False) ax.spines["right"].set_visible(False)
ax.spines["top"].set_visible(False) ax.spines["top"].set_visible(False)
ax.set_yscale("log") ax.set_yscale("log")
   
plt.show() plt.show()
``` ```
   
%% Cell type:markdown id:1bb8ba0a tags: %% Cell type:markdown id:7baf5d58 tags:
   
## Load data ## Load data
   
%% Cell type:code id:5074851d tags: %% Cell type:code id:1f5d1fb6 tags:
   
``` python ``` python
# Load data # Load data
vol = qim3d.io.load("../resources/img3d/shell_225x128x128.tif") vol = qim3d.io.load("../resources/img3d/shell_225x128x128.tif")
viz(vol) viz(vol)
histogram(vol) histogram(vol)
``` ```
   
%% Output %% Output
   
   
   
%% Cell type:markdown id:95d6db5e tags: %% Cell type:markdown id:66d5bcc7 tags:
   
## Apply median filter ## Apply median filter
   
%% Cell type:code id:cb5ae710 tags: %% Cell type:code id:47b7de23 tags:
   
``` python ``` python
# Median filter # Median filter
vol = scipy.ndimage.median_filter(vol, size=3) vol = scipy.ndimage.median_filter(vol, size=3)
viz(vol) viz(vol)
histogram(vol) histogram(vol)
``` ```
   
%% Output %% Output
   
   
   
%% Cell type:markdown id:38a17cba tags: %% Cell type:markdown id:06e9af21 tags:
   
## Get the threshold value ## Get the threshold value
   
%% Cell type:code id:44837b36 tags: %% Cell type:code id:06bba77b tags:
   
``` python ``` python
# Get the Otsu thresold # Get the Otsu thresold
thresh = filters.threshold_otsu(vol) thresh = filters.threshold_otsu(vol)
print (f"Threshold value: {thresh}") print (f"Threshold value: {thresh}")
histogram(vol, vline=thresh) histogram(vol, vline=thresh)
``` ```
   
%% Output %% Output
   
Threshold value: 63 Threshold value: 63
   
   
%% Cell type:markdown id:2b750470 tags: %% Cell type:markdown id:b2dc8937 tags:
   
## Make binary mask ## Make binary mask
   
%% Cell type:code id:01d59c26 tags: %% Cell type:code id:9971ddcb tags:
   
``` python ``` python
# Make it binary # Make it binary
vol = vol > thresh vol = vol > thresh
viz(vol, cmap="gray") viz(vol, cmap="gray")
``` ```
   
%% Output %% Output
   
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[8], line 3
1 # Make it binary
2 vol = vol > thresh
----> 3 viz(vol, cmap="gray")
TypeError: viz() got an unexpected keyword argument 'cmap'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment