-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmultipass_cleaner.py
executable file
·55 lines (43 loc) · 1.66 KB
/
multipass_cleaner.py
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
52
53
54
55
from typing import (List, Any)
import numpy
import logging
from skimage import morphology
def remove_noise(
original_img: List[List[int]],
passes: int=7,
images: List[Any]=[]
) -> List[List[int]]:
"""
Larger blobs must be increasingly separated to be labeled as noise
"""
cleaned_orig = original_img
cleaned_eroded = original_img
radius_step = 2
for index in range(passes):
cumulative_dilation_size = radius_step ** index
cumulative_disk = morphology.disk(cumulative_dilation_size)
logging.info(f'Cumulative dilation size: {cumulative_dilation_size}')
current_dilation_size = radius_step ** (index - 1) \
if index > 0 \
else 1
# dilation_disk = morphology.disk(current_dilation_size)
logging.info(f'Current dilation size: {current_dilation_size}')
# Noise blobs with up to 80 % more area
# than the structuring element will get deleted
max_noise_size = numpy.count_nonzero(cumulative_disk) * 1.5
logging.info(f'Maximum noise size: {max_noise_size}')
eroded = morphology.dilation(
cleaned_eroded,
footprint=morphology.disk(current_dilation_size)
)
if images:
images.append((f'eroded {index}', eroded))
cleaned_eroded = morphology.remove_small_objects(
eroded,
max_noise_size
)
if images:
images.append((f'cleaned eroded {index}', cleaned_eroded))
cleaned_orig = numpy.logical_and(cleaned_orig, cleaned_eroded)
logging.info(f'Finished cleaning pass {index}\n')
return cleaned_orig