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

solutions week 7

parent ee51d512
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:608c7b2a tags:
# QUIZ WEEK 7 SOLUTION
%% Cell type:code id:fc7238e9 tags:
``` python
import numpy as np
import matplotlib.pyplot as plt
import slgbuilder
I = np.array([[6, 1, 5, 4, 5, 7],
[2, 7, 2, 3, 4, 1],
[1, 5, 7, 4, 2, 6],
[5, 2, 3, 5, 4, 3],
[2, 4, 3, 6, 7, 7]])
line_cost = I[3].sum() # index 3 corresponds to z=2
zero_cost = I.sum(axis = 1).min()
k = I.argmin(axis = 0)
print(k) # check that change is never larger than 3
two_cost = I.min(axis = 0).sum()
print(f'{line_cost = }')
print(f'{zero_cost = }')
print(f'{two_cost = }')
```
%% Output
[2 0 1 1 2 1]
line_cost = 22
zero_cost = 19
two_cost = 10
%% Cell type:code id:94b1d764 tags:
``` python
# Just checing that solution using slgbuilder is giving the same result
layer = slgbuilder.GraphObject(I.astype(int))
np.bool = bool
np.int = int
helper = slgbuilder.MaxflowBuilder()
helper.add_object(layer)
helper.add_layered_boundary_cost()
helper.add_layered_smoothness(delta=3, wrap=False)
helper.solve()
segmentation = helper.what_segments(layer)
segmentation_line = segmentation.sum(axis=0) - 1
c = (I[segmentation_line, np.arange(segmentation.shape[1])]).sum()
fig, ax = plt.subplots()
ax.imshow(I)
ax.plot(segmentation_line, 'r')
ax.set_title(f'two_cost={c}')
plt.show()
```
%% Output
%% Cell type:code id:76b75526 tags:
``` python
```
This diff is collapsed.
This diff is collapsed.
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