<jupyter_start><jupyter_text>Segmenting nerve images Here is the starting point for processing ultrasound nerve segmentation data from Kaggle<jupyter_code>import os
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import glob
from skimage.io import imread
import matplotlib.pyplot as plt
IMAGE_DIR = '../../input/patient41/'<jupyter_output><empty_output><jupyter_text>Load and Preview Images<jupyter_code>us_image = plt.imread(IMAGE_DIR+'41_40.tif')
us_mask  = plt.imread(IMAGE_DIR+'41_40_mask.tif') != 0
%matplotlib inline

fig, (ax1, ax2 ,ax3) = plt.subplots(1,3, figsize = (15,4))
ax1.imshow(us_image)
ax1.set_title('Ultrasound Image')
ax2.hist(us_image.ravel(),bins=50)
ax2.set_title('Ultrasound Intensity Distribution')
ax3.imshow(us_mask, cmap = 'bone')
ax3.set_title('Segmented Nerve');<jupyter_output><empty_output><jupyter_text>Just focus on the nerveShow the segmentation of the nerve and the intensity inside that region<jupyter_code>fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2,2, figsize = (10,10))
ax1.imshow(us_image)
ax1.set_title('Ultrasound Image')
counts, bins, _ = ax2.hist(us_image.ravel(),bins=50)
ax2.set_title('Ultrasound Intensity Distribution')
ax3.imshow(us_image*us_mask)
ax3.set_title('Segmented Nerve Image')
ax4.hist(us_image[us_mask], bins = bins) # use the same bins again
ax4.set_title('Ultrasound Intensity Distribution')<jupyter_output><empty_output><jupyter_text>Simple SegmentationWe see too peaks in this histogram, lets try segmenting between them<jupyter_code>threshold = 120
thresholded = us_image > threshold
%matplotlib inline
fig, (ax1, ax2 ,ax3) = plt.subplots(1,3, figsize = (14,4))
ax1.imshow(us_image)
ax1.set_title('Ultrasound Image')
ax2.imshow(thresholded, interpolation='none')
ax2.set_title('Test Segmentation (>{0})'.format(threshold))
ax3.imshow(us_mask, cmap = 'bone')
ax3.set_title('Segmented Nerve')<jupyter_output><empty_output><jupyter_text>Scoring the ResultWe can use the ROC curve to score the result and see how accurately we classified the images. Here the ROC curve is obtained using [SciKit Learn](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.roc_curve.html).<jupyter_code>from sklearn.metrics import roc_curve, auc # roc curve tools

ground_truth_labels = us_mask.ravel() # we want to make them into vectors
score_value         = us_image.ravel()/255.0  # we want to make them into vectors and rescale to interval [0,1]

fpr, tpr, _ = roc_curve(ground_truth_labels,score_value)
roc_auc     = auc(fpr,tpr)
# Visualization
fig, ax = plt.subplots(1,1)
ax.plot(fpr, tpr, label='ROC curve (area = {0:02f})'.format(roc_auc))
ax.plot([0, 1], [0, 1], 'k--')
ax.set_xlim([0.0, 1.0])
ax.set_ylim([0.0, 1.05])
ax.set_xlabel('False Positive Rate')
ax.set_ylabel('True Positive Rate')
ax.set_title('Receiver operating characteristic example')
ax.legend(loc="lower right");<jupyter_output><empty_output><jupyter_text>Gaussian FilterHere we apply a gaussian filter and see how the performance changes<jupyter_code>%matplotlib inline
from skimage.filters import gaussian
gus_image = gaussian(us_image/255,sigma = 25)*255

test_threshold = 120
thresholded = gus_image>test_threshold
# Visualization
fig, (ax1, ax2 ,ax3) = plt.subplots(1,3, figsize = (14,4))
ax1.imshow(gus_image)
ax1.set_title('Gaussian Filtered Image')
ax2.imshow(thresholded, cmap = 'bone')
ax2.set_title('Test Segmentation')
ax3.imshow(us_mask, cmap = 'bone')
ax3.set_title('Segmented Nerve');
ground_truth_labels = us_mask.ravel() # we want to make them into vectors
score_value = gus_image.ravel()/255.0 # we want to make them into vectors
fpr_gus, tpr_gus, _ = roc_curve(ground_truth_labels,score_value)
roc_auc_gus = auc(fpr_gus,tpr_gus)

# Visualization
fig, ax = plt.subplots(1,1)
ax.plot(fpr, tpr, label='Raw ROC curve (area = %0.2f)' % roc_auc)
ax.plot(fpr_gus, tpr_gus, label='Gaussian ROC curve (area = %0.2f)' % roc_auc_gus)
ax.plot([0, 1], [0, 1], 'k--')
ax.set_xlim([0.0, 1.0])
ax.set_ylim([0.0, 1.05])
ax.set_xlabel('False Positive Rate')
ax.set_ylabel('True Positive Rate')
ax.set_title('Receiver operating characteristic example')
ax.legend(loc="lower right");<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13304822998336896, 'fraction_numerical': 0.03254929912093134, 'mean_word_length': 6.738970588235294, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 28, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '45122', 'license': 'Apache-2.0', 'n_tokens_mistral': 1656, 'n_tokens_neox': 1560, 'n_words': 362, 'path': 'Exercises/04-Segmentation/04-Exercises.ipynb', 'repo_name': 'ImagingLectures/Quantitative-Big-Imaging-2022', 'size': '4209'}