Classical Computer Vision: Image Segmentation with Mumford & Shah

     The Mumford & Shah is another segmentation algorithm that is much more efficient than K-Means, that uses the following math equation to decide which pixels of an image should belong to which segment



    The idea here is not to go deep under the hood for this equation, because we're not trying to implement it, but to use it. 
    
    So, lets get the example extracted from professor Aldo von Wangenheim  (@UFSC/Brazil) for Mumford & Shah image segmentation:

from ipywidgets import interact, interactive, interact_manual
import cv2, scipy
from skimage import data
from matplotlib import pyplot as plt
# Import the M&S-equivalent Ambrosio Tortorelli Minimizer (we'll call it A&Tmin)
# You have to download it before from
https://github.com/jacobgil/Ambrosio-Tortorelli-Minimizer
# or from my mirror (is NOT updated):
https://github.com/awangenh/Ambrosio-Tortorelli-Minimizer
from AmbrosioTortorelliMinimizer import *

image = cv2.imread("../data/ct-02.jpg", cv2.IMREAD_GRAYSCALE)


def my_ATmin(iterations, solver_maxiterations, tol, alpha, beta, epsilon, colormap):
global image
colormap = eval('plt.cm.' + colormap)
solver = AmbrosioTortorelliMinimizer(
image, iterations, solver_maxiterations, tol, alpha, beta, epsilon)
seg, edges = solver.minimize()

fig, axes = plt.subplots(ncols=3, figsize=(20, 7),
sharex=True, sharey=True)
ax = axes.ravel()

ax[0].imshow(image, cmap=plt.cm.gray)
ax[0].set_title('Original image')

ax[1].imshow(edges, cmap=plt.cm.gray)
ax[1].set_title('Edges')

ax[2].imshow(seg, cmap=colormap)
ax[2].set_title('Result')

for a in ax:
a.axis('off')

fig.tight_layout()
plt.show()


interact_manual(my_ATmin, iterations=(1, 100, 1), \
    solver_maxiterations=(1, 100), tol=(0.01, 1.0), alpha=(100, 10000, 100), \
beta=(0.001, 0.1, 0.001), epsilon=(0.001, 0.1, 0.001), \
    colormap=['nipy_spectral', 'hot', 'magma', 'seismic'])

# Default values: iterations = 1, solver_maxiterations = 10, tol = 0.1, alpha = 1000, beta = 0.01, epsilon = 0.01

Aldo von Wangenheim
 






Comentários