processing.filtering.pixel_clusters

pywi.processing.filtering.pixel_clusters.get_pixels_clusters(array, threshold=0)[source]

Return pixels clusters in the given image array.

Parameters:
  • array (array_like) – The input image where pixels clusters are searched. array should be a 2D Numpy array. A pixel cluster is a group of consecutive pixels having a value strictly greater than 0. Consecutive pixels are vertically or horizontally connected pixels, i.e. vertical or horizontal neighbors pixels; diagonal neighbors pixels are ignored.
  • threshold (float) – A filtering is applied to the array image before pixels clusters are searched. All pixels strictly lower than threshold in array are set to 0. If threshold value is None or lower than 0 then threshold is automatically set to 0 before the filtering is applied.
Returns:

  • filtered_array (array_like) – The array image after the pre-processing filtering i.e. with all pixels below threshold put to 0 (may contain NaN values).
  • label_array (array_like) – An integer Numpy array where each unique pixels cluster in input has a unique ID. This array defines the pixels cluster ID each pixel belongs to This array never contains NaN values.
  • num_clusters (int) – The number of pixels clusters in the array image.

Examples

Lets search pixels clusters in the following img image:

>>> import numpy as np
>>> img = np.array([[0, 0, 1, 3, 0, -1],
...                 [0, 0, 0, 5, 0,  0],
...                 [4, 3, 0, 0, 1,  0],
...                 [0, 0, 0, 8, 0,  0]])
>>> filtered_array, label_array, num_clusters = get_pixels_clusters(img)

The default filtering threshold is applied here; the top right pixel is put to 0 before pixels clusters are searched:

>>> print(filtered_array)
... 
[[ 0.  0.  1.  3.  0.  0.]
 [ 0.  0.  0.  5.  0.  0.]
 [ 4.  3.  0.  0.  1.  0.]
 [ 0.  0.  0.  8.  0.  0.]]

This image contains 4 pixels clusters:

>>> print(num_clusters)
4

Each of the 4 clusters are labeled with a different integer:

>>> print(label_array)
[[0 0 1 1 0 0]
 [0 0 0 1 0 0]
 [2 2 0 0 3 0]
 [0 0 0 4 0 0]]

See also

scipy.ndimage.measurements.label()
The underlying function used for pixels clusters detection (https://docs.scipy.org/doc/scipy-0.16.0/reference/generated/scipy.ndimage.measurements.label.html)
pywi.processing.filtering.pixel_clusters.filter_pixels_clusters(array, threshold=0)[source]

Keep only pixels belonging to the largest cluster of pixels and put all others pixels to 0.

Parameters:
  • array (array_like) – The input image to filter. array should be a 2D Numpy array.
  • threshold (float) – A filtering is applied to the array image before pixels clusters are searched. See get_pixels_clusters documentation for more details.
Returns:

The input image array where only pixels belonging to the largest cluster of pixels are kept and where all others pixels are put to 0.

Return type:

Numpy array

Examples

Lets search pixels clusters in the following img image:

>>> import numpy as np
>>> img = np.array([[0, 0, 1, 3, 0, -1],
...                 [0, 0, 0, 5, 0,  0],
...                 [4, 3, 0, 0, 1,  0],
...                 [0, 0, 0, 8, 0,  0]])
>>> filtered_array = filter_pixels_clusters(img)

This image contains 4 pixels clusters. Only the biggest one is kept:

>>> print(filtered_array)
... 
[[ 0.  0.  1.  3.  0.  0.]
 [ 0.  0.  0.  5.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.]]

Notes

See get_pixels_clusters documentation for more details.

pywi.processing.filtering.pixel_clusters.filter_pixels_clusters_stats(array, threshold=0)[source]

Return statistics about pixels clusters in the given image array.

Parameters:
  • array (array_like) – The image to analyse.
  • threshold (float) – A filtering is applied to the array image before pixels clusters are searched. See get_pixels_clusters documentation for more details.
Returns:

  • delta_value (float) – The sum of pixels value removed if filter_pixels_clusters is applied on the image array.
  • delta_abs_value (float) – The sum of the absolute value of pixels removed if filter_pixels_clusters is applied on the image array.
  • delta_num_pixels (int) – The number of pixel put to 0 if filter_pixels_clusters is applied on the image array.

Notes

See get_pixels_clusters documentation for more details.

Examples

Lets check stats about pixels clusters in the following img image:

>>> import numpy as np
>>> img = np.array([[0, 0, 1, 3, 0, -1],
...                 [0, 0, 0, 5, 0,  0],
...                 [4, 3, 0, 0, 1,  0],
...                 [0, 0, 0, 8, 0,  0]])
>>> delta_value, delta_abs_value, delta_num_pixels = filter_pixels_clusters_stats(img)

After filtering, the sum of removed pixels is 15:

>>> print(delta_value)
15.0

After filtering, the sum of the absolute values of removed pixels is 17:

>>> print(delta_abs_value)
17.0

After filtering, 5 pixels have been put to 0: >>> print(delta_num_pixels) 5

pywi.processing.filtering.pixel_clusters.number_of_pixels_clusters(array, threshold=0)[source]

Return the number of pixels clusters in the given image array.

Parameters:
  • array (array_like) – The image to analyse.
  • threshold (float) – A filtering is applied to the array image before pixels clusters are searched. See get_pixels_clusters documentation for more details.
Returns:

The number of pixel clusters in the image array.

Return type:

int

Examples

Lets count pixels clusters in the following img image:

>>> import numpy as np
>>> img = np.array([[0, 0, 1, 3, 0, -1],
...                 [0, 0, 0, 5, 0,  0],
...                 [4, 3, 0, 0, 1,  0],
...                 [0, 0, 0, 8, 0,  0]])
>>> num_clusters = number_of_pixels_clusters(img)

This image contains 4 pixels clusters:

>>> print(num_clusters)
4

Notes

See get_pixels_clusters documentation for more details.