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

week 1

parent 8af630cb
Branches
No related tags found
No related merge requests found
**/.DS_Store
**/__pycache__*
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
%% Cell type:markdown id: tags:
# Total variation exercise
%% Cell type:code id: tags:
``` python
import numpy as np
import skimage.io
from scipy.ndimage import gaussian_filter
```
%% Cell type:code id: tags:
``` python
# Function to compute total variation
def total_variation(im):
'''
Computes the total variation of an image.
Parameters
----------
im : ndarray
Image.
Returns
-------
float
Total variation of the image.
'''
return np.abs(im[:-1] - im[1:]).sum() + np.abs(im[:,:-1] - im[:,1:]).sum()
```
%% Cell type:code id: tags:
``` python
# Read data
in_dir = '../data/week1/'
im = skimage.io.imread(in_dir + 'fibres_xcth.png').astype(float)
# Compute total variation
tv_im = total_variation(im)
# Smooth the image
sigma = 2
im_g = gaussian_filter(im, sigma)
# Compute total variation
tv_im_g = total_variation(im_g)
# Print the results
print(f'Total variation of original image: {tv_im:0.4g}')
print(f'Total variation of smoothed image: {tv_im_g:0.4g}')
print(f'Total variation is reduced by: {tv_im-tv_im_g:0.4g}')
```
%% Output
Total variation of original image: 4.766e+09
Total variation of smoothed image: 1.606e+09
Total variation is reduced by: 3.16e+09
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