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

Simple threshold mask

parent 0aa4ce89
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:0f62fd3b tags:
%% Cell type:markdown id:cc248896 tags:
 
# Simple 3D binary mask
author: Felipe Delestro (fima@dtu.dk)
 
%% Cell type:code id:0a23b431 tags:
%% Cell type:code id:871f793a tags:
 
``` python
import qim3d
import numpy as np
import scipy.ndimage
import matplotlib.pyplot as plt
from skimage import filters
```
 
%% Cell type:markdown id:f9ad6ad1 tags:
%% Cell type:markdown id:1854d8dd tags:
 
## Helper functions
 
%% Cell type:code id:7a3aa8a7 tags:
%% Cell type:code id:56ccd2a9 tags:
 
``` python
def viz(vol, cmap="viridis"):
qim3d.viz.slice_viz(vol, position=np.linspace(50,100, num=6).astype(int), img_height=4, cmap=cmap)
 
def histogram(vol, vline=None):
 
# Calculate histogram
vol_hist, bin_edges = np.histogram(vol, bins=16)
 
# Plot histogram
fig, ax = plt.subplots(figsize=(6, 4))
ax.bar(bin_edges[:-1], vol_hist, width=np.diff(bin_edges), ec="white", align="edge")
 
if vline:
ax.axvline(vline, color="#FF3366", linestyle="--")
 
# Adjustments
ax.spines["right"].set_visible(False)
ax.spines["top"].set_visible(False)
ax.set_yscale("log")
 
plt.show()
```
 
%% Cell type:markdown id:7baf5d58 tags:
%% Cell type:markdown id:107f92f4 tags:
 
## Load data
 
%% Cell type:code id:1f5d1fb6 tags:
%% Cell type:code id:a9b977b3 tags:
 
``` python
# Load data
vol = qim3d.io.load("../resources/img3d/shell_225x128x128.tif")
viz(vol)
histogram(vol)
```
 
%% Output
 
 
 
%% Cell type:markdown id:66d5bcc7 tags:
%% Cell type:markdown id:186f46cc tags:
 
## Apply median filter
 
%% Cell type:code id:47b7de23 tags:
%% Cell type:code id:1b1fcba9 tags:
 
``` python
# Median filter
vol = scipy.ndimage.median_filter(vol, size=3)
viz(vol)
histogram(vol)
```
 
%% Output
 
 
 
%% Cell type:markdown id:06e9af21 tags:
%% Cell type:markdown id:c17d275c tags:
 
## Get the threshold value
 
%% Cell type:code id:06bba77b tags:
%% Cell type:code id:aebf7d46 tags:
 
``` python
# Get the Otsu thresold
thresh = filters.threshold_otsu(vol)
print (f"Threshold value: {thresh}")
histogram(vol, vline=thresh)
```
 
%% Output
 
Threshold value: 63
 
 
%% Cell type:markdown id:b2dc8937 tags:
%% Cell type:markdown id:c6e49b28 tags:
 
## Make binary mask
 
%% Cell type:code id:9971ddcb tags:
%% Cell type:code id:ec9a9333 tags:
 
``` python
# Make it binary
vol = vol > thresh
viz(vol, cmap="gray")
```
 
%% Output
 
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment