fuzzyclust.image.SpatialFCM#

class fuzzyclust.image.SpatialFCM(n_clusters=3, method='flicm', m=2.0, alpha=1.0, window_size=3, lambda_s=3.0, lambda_g=6.0, n_levels=256, tol=0.0001, max_iter=100, init='random', n_init=1, random_state=None, verbose=0)[source]#

Bases: ClusterMixin, BaseEstimator

Spatially constrained fuzzy c-means for image segmentation.

Segments a gray-level or multichannel image into n_clusters regions by fuzzy c-means on the pixel values, with the spatial context of every pixel brought into the objective in one of the published ways:

"fcm"

Plain pixel-wise FCM (the reference; every pixel on its own).

"fcm_s"

Ahmed et al. (2002): the distance of a pixel to a prototype is augmented by the mean distance of its neighbors to the same prototype, weighted by \(\alpha\). (The paper’s second component, the estimation of a smooth intensity bias field of MRI data, is not included here.) Cost: a neighborhood sum per cluster and iteration.

"fcm_s1", "fcm_s2"

Chen and Zhang (2004): the neighborhood sum is replaced by the mean- (S1) or median-filtered (S2) image computed once in advance; the filtered pixel is a second observation weighted by \(\alpha\). The median variant is robust to salt-and-pepper noise.

"enfcm"

Szilágyi et al. (2003): the linearly weighted sum image \(\xi\) of pixel and neighborhood mean is formed once, and its gray-level histogram is clustered – the cost per iteration is \(O(q\,c)\) for \(q\) gray levels instead of \(O(Nc)\).

"fgfcm"

Cai, Chen and Zhang (2007): like EnFCM, but the summed image uses locally adaptive weights \(S_{kr}\) that combine spatial and gray-level similarity (local_similarity_image()), so that edges are kept and noise pixels do not spread.

"flicm"

Krinidis and Chatzis (2010): a fuzzy factor \(G_{ki}\) that weighs each neighbor’s distance to the prototype by its spatial distance and by \((1-\mu_{ir})^m\), its lack of membership in the cluster, is added to the distance; the factor needs no parameter and is recomputed every iteration.

Parameters:
n_clustersint, default=3

Number of regions \(c\).

method{“fcm”, “fcm_s”, “fcm_s1”, “fcm_s2”, “enfcm”, “fgfcm”, “flicm”}, default=”flicm”
mfloat, default=2.0

Fuzziness exponent \(m>1\).

alphafloat, default=1.0

Weight \(\alpha\) of the neighborhood term (fcm_s, fcm_s1, fcm_s2, enfcm). 0 gives plain FCM; larger values smooth more and blur fine structure.

window_sizeint, default=3

Side of the square neighborhood \(N_k\) (odd, \(\ge3\)); \(N_R=\) window_size**2 - 1 neighbors.

lambda_sfloat, default=3.0
lambda_gfloat, default=6.0

Scales of the spatial and gray-level similarity of fgfcm.

n_levelsint, default=256

Number of gray-level bins of the histogram methods (enfcm, fgfcm); the summed image is quantized into this many equal bins between its minimum and maximum.

tolfloat, default=1e-4

Termination tolerance on \(\max|\Delta\mu_{ik}|\).

max_iterint, default=100
init{“random”, “kmeans++”} or ndarray, default=”random”

"random" draws prototypes inside the range of the pixel values, "kmeans++" seeds them on the pixels; an array of shape (n_clusters, n_channels) gives initial prototypes, an array of shape (H, W, n_clusters) or (H*W, n_clusters) an initial partition.

n_initint, default=1

Random restarts; the run with the lowest final objective is kept.

random_stateint, RandomState instance or None, default=None
verboseint, default=0
Attributes:
cluster_centers_ndarray of shape (n_clusters, n_channels)

Prototypes \(\mathbf{v}_i\) in pixel-value units.

membership_ndarray of shape (H, W, n_clusters)

Membership image of every cluster.

labels_ndarray of shape (H, W)

Label image (argmax of the memberships).

filtered_image_ndarray of shape (H, W) or (H, W, C)

The filtered/summed image the method works with (mean or median image for fcm_s1/fcm_s2, \(\xi\) for enfcm and fgfcm); absent for the other methods.

histogram_ndarray of shape (n_levels,)

Pixel counts \(\gamma_l\) per gray-level bin (histogram methods).

level_values_ndarray of shape (n_levels,)

Gray level of every bin (bin centers).

level_membership_ndarray of shape (n_levels, n_clusters)

Memberships of the gray levels (histogram methods).

objective_float
objective_history_ndarray of shape (n_iter_,)
n_iter_int
image_shape_tuple

(H, W) of the training image.

n_channels_int

Notes

Let \(\mathbf{x}_k\) be the value of pixel \(k\) (a scalar gray level or a vector of \(C\) channels), \(N_k\) the set of its \(N_R\) neighbors in the window, and \(\mathbf{v}_i\) the prototype of region \(i\). The methods minimize, by alternating optimization,

\[\begin{split}J_{\text{FCM\_S}}&=\sum_{i=1}^{c}\sum_{k=1}^{N}\mu_{ik}^m\norm{\mathbf{x}_k-\mathbf{v}_i}^2 +\frac{\alpha}{N_R}\sum_{i=1}^{c}\sum_{k=1}^{N}\mu_{ik}^m \sum_{r\in N_k}\norm{\mathbf{x}_r-\mathbf{v}_i}^2,\\ J_{\text{FCM\_S1,2}}&=\sum_{i=1}^{c}\sum_{k=1}^{N}\mu_{ik}^m\norm{\mathbf{x}_k-\mathbf{v}_i}^2 +\alpha\sum_{i=1}^{c}\sum_{k=1}^{N}\mu_{ik}^m\norm{\bar{\mathbf{x}}_k-\mathbf{v}_i}^2,\\ J_{\text{EnFCM,FGFCM}}&=\sum_{i=1}^{c}\sum_{l=1}^{q}\gamma_l\,\mu_{il}^m\,(\xi_l-v_i)^2,\\ J_{\text{FLICM}}&=\sum_{i=1}^{c}\sum_{k=1}^{N}\Big[\mu_{ik}^m\norm{\mathbf{x}_k-\mathbf{v}_i}^2+G_{ki}\Big], \qquad G_{ki}=\sum_{r\in N_k}\frac{1}{d_{kr}+1}(1-\mu_{ir})^m\norm{\mathbf{x}_r-\mathbf{v}_i}^2,\end{split}\]

where \(\bar{\mathbf{x}}_k\) is the mean (S1) or median (S2) of the window, \(\xi_l\) the gray level of histogram bin \(l\) with \(\gamma_l\) pixels, and \(d_{kr}\) the Euclidean distance of the pixel coordinates. All membership updates have the FCM form with an effective squared distance,

\[\mu_{ik}=\frac{1}{\sum_{j=1}^{c}\big(\tilde D^2_{ik}/\tilde D^2_{jk}\big)^{1/(m-1)}},\]

with

\[\begin{split}\tilde D^2_{ik}=\begin{cases} \norm{\mathbf{x}_k-\mathbf{v}_i}^2 +\frac{\alpha}{N_R}\sum_{r\in N_k}\norm{\mathbf{x}_r-\mathbf{v}_i}^2 & \text{FCM\_S}\\ \norm{\mathbf{x}_k-\mathbf{v}_i}^2 +\alpha\norm{\bar{\mathbf{x}}_k-\mathbf{v}_i}^2 & \text{FCM\_S1, S2}\\ (\xi_l-v_i)^2 & \text{EnFCM, FGFCM}\\ \norm{\mathbf{x}_k-\mathbf{v}_i}^2+G_{ki} & \text{FLICM.} \end{cases}\end{split}\]

The prototype updates are

\[\begin{split}\mathbf{v}_i&=\frac{\sum_k\mu_{ik}^m\big(\mathbf{x}_k +\frac{\alpha}{N_R}\sum_{r\in N_k}\mathbf{x}_r\big)}{(1+\alpha)\sum_k\mu_{ik}^m}\ \text{(FCM\_S)},\\ \mathbf{v}_i&=\frac{\sum_k\mu_{ik}^m(\mathbf{x}_k+\alpha\bar{\mathbf{x}}_k)}{(1+\alpha)\sum_k\mu_{ik}^m} \ \text{(FCM\_S1, S2)},\end{split}\]
\[v_i=\frac{\sum_l\gamma_l\mu_{il}^m\xi_l}{\sum_l\gamma_l\mu_{il}^m}\ \text{(EnFCM, FGFCM)},\qquad \mathbf{v}_i=\frac{\sum_k\mu_{ik}^m\mathbf{x}_k}{\sum_k\mu_{ik}^m}\ \text{(FLICM)}.\]

The histogram methods are weighted FCM (WeightedFCM) on the gray levels with the histogram as weights; a pixel inherits the membership of its gray level. All neighborhood operations use scipy.ndimage with reflected borders.

References

[1]

M. N. Ahmed, S. M. Yamany, N. Mohamed, A. A. Farag, T. Moriarty, “A modified fuzzy c-means algorithm for bias field estimation and segmentation of MRI data”, IEEE Transactions on Medical Imaging 21(3), 193–199, 2002.

[2]

S. Chen, D. Zhang, “Robust image segmentation using FCM with spatial constraints based on new kernel-induced distance measure”, IEEE Transactions on Systems, Man, and Cybernetics, Part B 34(4), 1907–1916, 2004.

[3]

L. Szilágyi, Z. Benyó, S. M. Szilágyi, H. S. Adam, “MR brain image segmentation using an enhanced fuzzy c-means algorithm”, Proc. 25th Annual International Conference of the IEEE EMBS, 724–726, 2003.

[4]

W. Cai, S. Chen, D. Zhang, “Fast and robust fuzzy c-means clustering algorithms incorporating local information for image segmentation”, Pattern Recognition 40(3), 825–838, 2007.

[5]

S. Krinidis, V. Chatzis, “A robust fuzzy local information c-means clustering algorithm”, IEEE Transactions on Image Processing 19(5), 1328–1337, 2010.

Examples

>>> from fuzzyclust.image import SpatialFCM, make_synthetic_image
>>> data = make_synthetic_image(shape=(48, 48), n_classes=3, noise_level=0.1, random_state=0)
>>> seg = SpatialFCM(n_clusters=3, method="flicm", random_state=0).fit(data.image)
>>> seg.labels_.shape, seg.membership_.shape, seg.cluster_centers_.shape
((48, 48), (48, 48, 3), (3, 1))
fit(X, y=None)[source]#

Segment an image.

Parameters:
Xndarray of shape (H, W) or (H, W, C)

Gray-level or multichannel image (any float range; integer images are converted).

yIgnored
Returns:
selfobject
fit_predict(X, y=None, **kwargs)[source]#

Segment the image and return its label image.

predict(X)[source]#

Label image of a (new) image (argmax of predict_proba()).

predict_proba(X)[source]#

Membership images of a (new) image for the fitted prototypes.

The method’s own membership formula is applied with the prototypes held fixed: one step for the FCM_S family and the histogram methods (whose filtered image is recomputed for the new image), and an iteration to convergence of the membership update for FLICM, whose fuzzy factor depends on the memberships themselves.

Parameters:
Xndarray of shape (H, W) or (H, W, C)
Returns:
Undarray of shape (H, W, n_clusters)

Examples using fuzzyclust.image.SpatialFCM#

Image segmentation under noise: six spatially constrained FCM variants

Image segmentation under noise: six spatially constrained FCM variants

Spatial FCM: the neighborhood weight and the window size

Spatial FCM: the neighborhood weight and the window size