2
2
### various add-ons to the SciPy morphology package
3
3
################################################################
4
4
5
-
6
-
7
5
from numpy import *
8
6
#from scipy.ndimage import morphology,measurements,filters
9
7
from scipy .ndimage import measurements
@@ -68,36 +66,40 @@ def check_binary(image):
68
66
assert amin (image )>= 0 and amax (image )<= 1 ,\
69
67
"array should be binary, has values %g to %g" % (amin (image ),amax (image ))
70
68
69
+ @checks (uintpair )
70
+ def brick (size ):
71
+ return ones (size , uint8 )
72
+
71
73
@checks (ABINARY2 ,uintpair )
72
74
def r_dilation (image ,size ,origin = 0 ):
73
75
"""Dilation with rectangular structuring element using fast OpenCV.dilate."""
74
- return cv2 .dilate (image .astype (uint8 ), ones (size , uint8 ))
76
+ return cv2 .dilate (image .astype (uint8 ), brick (size ))
75
77
# return filters.maximum_filter(image,size,origin=(size[0]%2-1,size[1]%2-1))
76
78
77
79
@checks (ABINARY2 ,uintpair )
78
80
def r_erosion (image ,size ,origin = - 1 ):
79
81
"""Erosion with rectangular structuring element using fast OpenCV.erode."""
80
- return cv2 .erode (image .astype (uint8 ), ones (size , uint8 ))
82
+ return cv2 .erode (image .astype (uint8 ), brick (size ))
81
83
# return filters.minimum_filter(image,size,origin=0, mode='constant', cval=1)
82
84
83
85
@checks (ABINARY2 ,uintpair )
84
86
def r_opening (image ,size ,origin = 0 ):
85
87
"""Opening with rectangular structuring element using fast OpenCV.morphologyEx."""
86
- return cv2 .morphologyEx (image .astype (uint8 ), cv2 .MORPH_OPEN , ones (size , uint8 ))
88
+ return cv2 .morphologyEx (image .astype (uint8 ), cv2 .MORPH_OPEN , brick (size ))
87
89
# image = r_erosion(image,size,origin=0)
88
90
# return r_dilation(image,size,origin=-1)
89
91
90
92
@checks (ABINARY2 ,uintpair )
91
93
def r_closing (image ,size ,origin = 0 ):
92
94
"""Closing with rectangular structuring element using fast OpenCV.morphologyEx."""
93
- return cv2 .morphologyEx (image .astype (uint8 ), cv2 .MORPH_CLOSE , ones (size , uint8 ))
95
+ return cv2 .morphologyEx (image .astype (uint8 ), cv2 .MORPH_CLOSE , brick (size ))
94
96
# image = r_dilation(image,size,origin=0)
95
97
# return r_erosion(image,size,origin=-1)
96
98
97
99
@checks (ABINARY2 ,uintpair )
98
100
def rb_dilation (image ,size ,origin = 0 ):
99
101
"""Binary dilation using linear filters."""
100
- return cv2 .dilate (image .astype (uint8 ), ones (size , uint8 ))
102
+ return cv2 .dilate (image .astype (uint8 ), brick (size ))
101
103
# output = zeros(image.shape,'f')
102
104
# filters.uniform_filter(image,size,output=output,origin=(size[0]%2-1,size[1]%2-1))
103
105
# # 0 creates rounding artifacts
@@ -106,22 +108,22 @@ def rb_dilation(image,size,origin=0):
106
108
@checks (ABINARY2 ,uintpair )
107
109
def rb_erosion (image ,size ,origin = - 1 ):
108
110
"""Binary erosion using linear filters."""
109
- return cv2 .erode (image .astype (uint8 ), ones (size , uint8 ))
111
+ return cv2 .erode (image .astype (uint8 ), brick (size ))
110
112
# output = zeros(image.shape,'f')
111
113
# filters.uniform_filter(image,size,output=output,origin=0, mode='constant', cval=1)
112
114
# return array(output==1,'i')
113
115
114
116
@checks (ABINARY2 ,uintpair )
115
117
def rb_opening (image ,size ,origin = 0 ):
116
118
"""Binary opening using linear filters."""
117
- return cv2 .morphologyEx (image .astype (uint8 ), cv2 .MORPH_OPEN , ones (size , uint8 ))
119
+ return cv2 .morphologyEx (image .astype (uint8 ), cv2 .MORPH_OPEN , brick (size ))
118
120
# image = rb_erosion(image,size,origin=0)
119
121
# return rb_dilation(image,size,origin=-1)
120
122
121
123
@checks (ABINARY2 ,uintpair )
122
124
def rb_closing (image ,size ,origin = 0 ):
123
125
"""Binary closing using linear filters."""
124
- return cv2 .morphologyEx (image .astype (uint8 ), cv2 .MORPH_CLOSE , ones (size , uint8 ))
126
+ return cv2 .morphologyEx (image .astype (uint8 ), cv2 .MORPH_CLOSE , brick (size ))
125
127
# image = rb_dilation(image,size,origin=0)
126
128
# return rb_erosion(image,size,origin=-1)
127
129
@@ -142,26 +144,26 @@ def rb_reconstruction(image,mask,step=1,maxsteps=None):
142
144
@checks (GRAYSCALE ,uintpair )
143
145
def rg_dilation (image ,size ,origin = 0 ):
144
146
"""Grayscale dilation using fast OpenCV.dilate."""
145
- return cv2 .dilate (image , ones (size , uint8 ))
147
+ return cv2 .dilate (image , brick (size ))
146
148
# return filters.maximum_filter(image,size,origin=origin)
147
149
148
150
@checks (GRAYSCALE ,uintpair )
149
151
def rg_erosion (image ,size ,origin = 0 ):
150
152
"""Grayscale erosion using fast OpenCV.erode."""
151
- return cv2 .erode (image , ones (size , uint8 ))
153
+ return cv2 .erode (image , brick (size ))
152
154
# return filters.minimum_filter(image,size,origin=origin, mode='constant', cval=1)
153
155
154
156
@checks (GRAYSCALE ,uintpair )
155
157
def rg_opening (image ,size ,origin = 0 ):
156
158
"""Grayscale opening using fast OpenCV.morphologyEx."""
157
- return cv2 .morphologyEx (image , cv2 .MORPH_OPEN , ones (size , uint8 ))
159
+ return cv2 .morphologyEx (image , cv2 .MORPH_OPEN , brick (size ))
158
160
# image = r_erosion(image,size,origin=origin)
159
161
# return r_dilation(image,size,origin=origin)
160
162
161
163
@checks (GRAYSCALE ,uintpair )
162
164
def rg_closing (image ,size ,origin = 0 ):
163
165
"""Grayscale closing using fast OpenCV.morphologyEx."""
164
- return cv2 .morphologyEx (image , cv2 .MORPH_CLOSE , ones (size , uint8 ))
166
+ return cv2 .morphologyEx (image , cv2 .MORPH_CLOSE , brick (size ))
165
167
# image = r_dilation(image,size,origin=0)
166
168
# return r_erosion(image,size,origin=-1)
167
169
@@ -196,7 +198,7 @@ def find_label_contours(labels):
196
198
contours [label ] = find_contours (labels == label )
197
199
return contours
198
200
199
- @checks (SEGMENTATION )
201
+ @checks (ALL ( SEGMENTATION , ANONNEG ) )
200
202
def spread_labels (labels ,maxdist = 9999999 ):
201
203
"""Spread the given labels to the background."""
202
204
#distances,features = morphology.distance_transform_edt(labels==0,return_distances=1,return_indices=1)
@@ -206,9 +208,21 @@ def spread_labels(labels,maxdist=9999999):
206
208
return labels
207
209
distances ,indexes = cv2 .distanceTransformWithLabels (array (labels == 0 ,uint8 ),cv2 .DIST_L2 ,cv2 .DIST_MASK_PRECISE ,labelType = cv2 .DIST_LABEL_PIXEL )
208
210
spread = labels [where (labels > 0 )][indexes - 1 ]
211
+ if maxdist is None :
212
+ return spread , distances
209
213
spread *= (distances < maxdist )
210
214
return spread
211
215
216
+ @checks (SEGMENTATION )
217
+ def dist_labels (labels ):
218
+ """Get the distance transformation of the segments."""
219
+ if not labels .any ():
220
+ return labels
221
+ return cv2 .distanceTransform (labels ,
222
+ distanceType = cv2 .DIST_L1 ,
223
+ maskSize = 3 ,
224
+ dstType = cv2 .CV_8U )
225
+
212
226
@checks (ABINARY2 ,ABINARY2 )
213
227
def keep_marked (image ,markers ):
214
228
"""Given a marker image, keep only the connected components
0 commit comments