-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy paththresholding.py
More file actions
51 lines (44 loc) · 1.68 KB
/
thresholding.py
File metadata and controls
51 lines (44 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import itertools
import math
import numpy as np
from scipy import ndimage as ndi
def apply_hysteresis_threshold(image, low, high):
"""Apply hysteresis thresholding to `image`.
This algorithm finds regions where `image` is greater than `high`
OR `image` is greater than `low` *and* that region is connected to
a region greater than `high`.
Parameters
----------
image : array, shape (M,[ N, ..., P])
Grayscale input image.
low : float, or array of same shape as `image`
Lower threshold.
high : float, or array of same shape as `image`
Higher threshold.
Returns
-------
thresholded : array of bool, same shape as `image`
Array in which `True` indicates the locations where `image`
was above the hysteresis threshold.
Examples
--------
>>> image = np.array([1, 2, 3, 2, 1, 2, 1, 3, 2])
>>> apply_hysteresis_threshold(image, 1.5, 2.5).astype(int)
array([0, 1, 1, 1, 0, 0, 0, 1, 1])
References
----------
.. [1] J. Canny. A computational approach to edge detection.
IEEE Transactions on Pattern Analysis and Machine Intelligence.
1986; vol. 8, pp.679-698.
DOI: 10.1109/TPAMI.1986.4767851
"""
low = np.clip(low, a_min=None, a_max=high) # ensure low always below high
mask_low = image > low
mask_high = image > high
# Connected components of mask_low
labels_low, num_labels = ndi.label(mask_low)
# Check which connected components contain pixels from mask_high
sums = ndi.sum(mask_high, labels_low, np.arange(num_labels + 1))
connected_to_high = sums > 0
thresholded = connected_to_high[labels_low]
return thresholded