Skip to content

Instantly share code, notes, and snippets.

@CrisDamian
Created May 7, 2018 09:34
Show Gist options
  • Save CrisDamian/2fa3fdd0074d3c7e3f98e02c124eda69 to your computer and use it in GitHub Desktop.
Save CrisDamian/2fa3fdd0074d3c7e3f98e02c124eda69 to your computer and use it in GitHub Desktop.
Usage example for `aexpansion_grid` from PyMaxflow
import numpy as np
import matplotlib.pyplot as plt
import imageio
from maxflow.fastmin import aexpansion_grid
# Loading image
I = imageio.imread('imageio:astronaut.png')
I = I[:,:,1]/I.max()
# Generates 16 gray levels for nearsest prototype labeling
L = 16
levs = np.arange(0.5/L, 1, 1/L)
# Calculate data cost as the absolute difference between the label prototype and the pixel value
D = np.abs(I.reshape(I.shape+(1,)) - levs.reshape((1,1,-1)))
# Generate nearest prototype labeling
Id = np.argmin(D,2)
fg = plt.figure("Direct labeling")
ax1 = fg.add_subplot(1,1,1)
ax1.imshow(Id)
# Calculate neighbourhood cost as absolute difference between prototypes
alpha = 1
V = alpha * np.abs( levs.reshape((-1,1)) - levs.reshape((1,-1)))
# Mimimise data + neighbourhood cost
labels = aexpansion_grid(D,V)
fg = plt.figure("Regularised labeling")
ax1 = fg.add_subplot(1,1,1)
ax1.imshow(labels)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment