资源简介
The *Morphological Snakes* are a family of related methods for image-guided evolution of curves and surfaces represented as a level-set of an embedding
function. They have application in several computer vision areas, such as
tracking and image segmentation.
代码片段和文件信息
# -*- coding: utf-8 -*-
“““
morphsnakes
===========
This is a Python implementation of the algorithms introduced in the paper
Márquez-Neila P. Baumela L. Álvarez L. “A morphological approach
to curvature-based evolution of curves and surfaces“. IEEE Transactions
on Pattern Analysis and Machine Intelligence (PAMI) 2013.
This implementation is intended to be as brief understandable and self-contained
as possible. It does not include any enhancement to make it fast or efficient.
Any practical implementation of this algorithm should work only over the
neighbor pixels of the 0.5-levelset not over all the embedding function
and perhaps should feature multi-threading or GPU capabilities.
The classes MorphGAC and MorphACWE provide most of the functionality of this
module. They implement the Morphological Geodesic Active Contours and the
Morphological Active Contours without Edges respectively. See the
aforementioned paper for full details.
See test.py for examples of usage.
“““
__author__ = “P. Márquez Neila “
from itertools import cycle
import numpy as np
from scipy import ndimage
from scipy.ndimage import binary_dilation binary_erosion \
gaussian_filter gaussian_gradient_magnitude
class fcycle(object):
def __init__(self iterable):
“““Call functions from the iterable each time it is called.“““
self.funcs = cycle(iterable)
def __call__(self *args **kwargs):
f = next(self.funcs)
return f(*args **kwargs)
# SI and IS operators for 2D and 3D.
_P2 = [np.eye(3) np.array([[010]]*3) np.flipud(np.eye(3)) np.rot90([[010]]*3)]
_P3 = [np.zeros((333)) for i in range(9)]
_P3[0][::1] = 1
_P3[1][:1:] = 1
_P3[2][1::] = 1
_P3[3][:[012][012]] = 1
_P3[4][:[012][210]] = 1
_P3[5][[012]:[012]] = 1
_P3[6][[012]:[210]] = 1
_P3[7][[012][012]:] = 1
_P3[8][[012][210]:] = 1
_aux = np.zeros((0))
def SI(u):
“““SI operator.“““
global _aux
if np.ndim(u) == 2:
P = _P2
elif np.ndim(u) == 3:
P = _P3
else:
raise ValueError(“u has an invalid number of dimensions (should be 2 or 3)“)
if u.shape != _aux.shape[1:]:
_aux = np.zeros((len(P)) + u.shape)
for _aux_i P_i in zip(_aux P):
_aux_i[:] = binary_erosion(u P_i)
return _aux.max(0)
def IS(u):
“““IS operator.“““
global _aux
if np.ndim(u) == 2:
P = _P2
elif np.ndim(u) == 3:
P = _P3
else:
raise ValueError(“u has an invalid number of dimensions (should be 2 or 3)“)
if u.shape != _aux.shape[1:]:
_aux = np.zeros((len(P)) + u.shape)
for _aux_i P_i in zip(_aux P):
_aux_i[:] = binary_dilation(u P_i)
return _aux.min(0)
# SIoIS operator.
SIoIS = lambda u: SI(IS(u))
ISoSI = lambda u: IS(SI(u))
curvop = fcycle([SIoIS ISoSI])
# Stopping factors (function g(I) in the paper).
def gborders(img a
评论
共有 条评论