Skip to content
Snippets Groups Projects
Commit 3c2fa313 authored by Vedrana Andersen Dahl's avatar Vedrana Andersen Dahl
Browse files

changed path

parent b6c4df6d
No related branches found
No related tags found
No related merge requests found
...@@ -30,7 +30,7 @@ def segmentation_histogram(ax, D, S, edges=None): ...@@ -30,7 +30,7 @@ def segmentation_histogram(ax, D, S, edges=None):
ax.plot(centers, np.histogram(D[S==k].ravel(), edges)[0]) ax.plot(centers, np.histogram(D[S==k].ravel(), edges)[0])
path = '../../data/week5/' path = '../data/week5/'
D = skimage.io.imread(path + 'noisy_circles.png').astype(float) D = skimage.io.imread(path + 'noisy_circles.png').astype(float)
# Ground-truth segmentation. # Ground-truth segmentation.
......
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Segmentation modelling # Segmentation modelling
The code provided in connection with the section 5.2 (MRF modelling for image segmentation) of the lecture notes. The code provided in connection with the section 5.2 (MRF modelling for image segmentation) of the lecture notes.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import skimage.io import skimage.io
import numpy as np import numpy as np
import scipy.ndimage import scipy.ndimage
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
You should write the code which computes segmentation energies (U1 and U2) as described in the lecture notes. The skeleton of the function is provided below. You should write the code which computes segmentation energies (U1 and U2) as described in the lecture notes. The skeleton of the function is provided below.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
def segmentation_energy(S, D, mu, beta): def segmentation_energy(S, D, mu, beta):
# TODO -- add your code here # TODO -- add your code here
# likelihood energy # likelihood energy
U1 = 0 U1 = 0
# prior energy # prior energy
U2 = 0 U2 = 0
return U1, U2 return U1, U2
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
A helping function for plotting the histograms of the data and the segmentation. A helping function for plotting the histograms of the data and the segmentation.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
def segmentation_histogram(ax, D, S, edges=None): def segmentation_histogram(ax, D, S, edges=None):
''' '''
Plot histogram for grayscale data and each segmentation label. Plot histogram for grayscale data and each segmentation label.
''' '''
if edges is None: if edges is None:
edges = np.linspace(D.min(), D.max(), 100) edges = np.linspace(D.min(), D.max(), 100)
ax.hist(D.ravel(), bins=edges, color = 'k') ax.hist(D.ravel(), bins=edges, color = 'k')
centers = 0.5 * (edges[:-1] + edges[1:]) centers = 0.5 * (edges[:-1] + edges[1:])
for k in range(S.max() + 1): for k in range(S.max() + 1):
ax.plot(centers, np.histogram(D[S==k].ravel(), edges)[0]) ax.plot(centers, np.histogram(D[S==k].ravel(), edges)[0])
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Get hold of the data (noisy image) and ground truth segmentation. Get hold of the data (noisy image) and ground truth segmentation.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
path = '../../data/week5/' path = '../data/week5/'
D = skimage.io.imread(path + 'noisy_circles.png').astype(float) D = skimage.io.imread(path + 'noisy_circles.png').astype(float)
# Ground-truth segmentation. # Ground-truth segmentation.
GT = skimage.io.imread(path + 'noise_free_circles.png') GT = skimage.io.imread(path + 'noise_free_circles.png')
(mu, S_gt) = np.unique(GT, return_inverse=True) (mu, S_gt) = np.unique(GT, return_inverse=True)
S_gt = S_gt.reshape(D.shape) S_gt = S_gt.reshape(D.shape)
segmentations = [S_gt] # list where I'll place different segmentations segmentations = [S_gt] # list where I'll place different segmentations
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Find some configurations (segmentations) using conventional segmentation methods. Find some configurations (segmentations) using conventional segmentation methods.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Simple thresholding # Simple thresholding
S_t = np.zeros(D.shape, dtype=int) + (D > 100) + (D > 160) # thresholded S_t = np.zeros(D.shape, dtype=int) + (D > 100) + (D > 160) # thresholded
segmentations += [S_t] segmentations += [S_t]
# Gaussian filtering followed by thresholding # Gaussian filtering followed by thresholding
D_s = scipy.ndimage.gaussian_filter(D, sigma=1, truncate=3, mode='nearest') D_s = scipy.ndimage.gaussian_filter(D, sigma=1, truncate=3, mode='nearest')
S_g = np.zeros(D.shape, dtype=int) + (D_s > 100) + (D_s > 160) S_g = np.zeros(D.shape, dtype=int) + (D_s > 100) + (D_s > 160)
segmentations += [S_g] segmentations += [S_g]
# Median filtering followed by thresholding # Median filtering followed by thresholding
D_m = scipy.ndimage.median_filter(D, size=(5, 5), mode='reflect') D_m = scipy.ndimage.median_filter(D, size=(5, 5), mode='reflect')
S_t = np.zeros(D.shape, dtype=int) + (D_m > 100) + (D_m > 160) # thresholded S_t = np.zeros(D.shape, dtype=int) + (D_m > 100) + (D_m > 160) # thresholded
segmentations += [S_t] segmentations += [S_t]
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Visualize the segmentations, associated histograms and error images. Once you implement the `segmentation_energy` you can look at whether there is a link between segmentation energies and the quality of the segmentation. Visualize the segmentations, associated histograms and error images. Once you implement the `segmentation_energy` you can look at whether there is a link between segmentation energies and the quality of the segmentation.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
#%% visualization #%% visualization
fig, ax = plt.subplots() fig, ax = plt.subplots()
ax.imshow(D, vmin=0, vmax=255, cmap=plt.cm.gray) ax.imshow(D, vmin=0, vmax=255, cmap=plt.cm.gray)
plt.show() plt.show()
fig, ax = plt.subplots(3, len(segmentations), figsize=(10, 10)) fig, ax = plt.subplots(3, len(segmentations), figsize=(10, 10))
beta = 100 beta = 100
for i, s in enumerate(segmentations): for i, s in enumerate(segmentations):
ax[0][i].imshow(s) ax[0][i].imshow(s)
U1, U2 = segmentation_energy(s, D, mu, beta) U1, U2 = segmentation_energy(s, D, mu, beta)
ax[0][i].set_title(f'likelihood: {int(U1)}\nprior: {U2}\nposterior: {int(U1)+U2}') ax[0][i].set_title(f'likelihood: {int(U1)}\nprior: {U2}\nposterior: {int(U1)+U2}')
segmentation_histogram(ax[1][i], D, s) segmentation_histogram(ax[1][i], D, s)
ax[1][i].set_xlabel('Intensity') ax[1][i].set_xlabel('Intensity')
ax[1][i].set_ylabel('Count') ax[1][i].set_ylabel('Count')
err = S_gt - s err = S_gt - s
ax[2][i].imshow(err, vmin=-2, vmax=2, cmap=plt.cm.bwr) ax[2][i].imshow(err, vmin=-2, vmax=2, cmap=plt.cm.bwr)
ax[2][i].set_title(f'Pixel error: {(err != 0).sum()}') ax[2][i].set_title(f'Pixel error: {(err != 0).sum()}')
fig.tight_layout() fig.tight_layout()
plt.show() plt.show()
``` ```
%% Output %% Output
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment