Skip to content

Commit 615bfca

Browse files
committed
update prepro pt2map, find_contours, dilation
1 parent e851c85 commit 615bfca

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

docs/modules/prepro.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ Some of the code in this package are borrowed from Keras.
5151

5252
array_to_img
5353

54+
find_contours
55+
pt2map
56+
binary_dilation
57+
dilation
58+
5459
pad_sequences
5560
process_sequences
5661
sequences_add_start_id
@@ -144,6 +149,21 @@ Numpy and PIL
144149
^^^^^^^^^^^^^^
145150
.. autofunction:: array_to_img
146151

152+
Find contours
153+
^^^^^^^^^^^^^^
154+
.. autofunction:: find_contours
155+
156+
Points to Image
157+
^^^^^^^^^^^^^^^^^
158+
.. autofunction:: pt2map
159+
160+
Binary dilation
161+
^^^^^^^^^^^^^^^^^
162+
.. autofunction:: binary_dilation
163+
164+
Greyscale dilation
165+
^^^^^^^^^^^^^^^^^^^^
166+
.. autofunction:: dilation
147167

148168
Sequence
149169
---------

tensorlayer/prepro.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,70 @@ def array_to_img(x, dim_ordering=(0,1,2), scale=True):
12081208
raise Exception('Unsupported channel number: ', x.shape[2])
12091209

12101210

1211+
1212+
1213+
def find_contours(x, level=0.8, fully_connected='low', positive_orientation='low'):
1214+
""" Find iso-valued contours in a 2D array for a given level value, returns list of (n, 2)-ndarrays
1215+
see `skimage.measure.find_contours <http://scikit-image.org/docs/dev/api/skimage.measure.html#skimage.measure.find_contours>`_ .
1216+
1217+
Parameters
1218+
------------
1219+
x : 2D ndarray of double. Input data in which to find contours.
1220+
level : float. Value along which to find contours in the array.
1221+
fully_connected : str, {‘low’, ‘high’}. Indicates whether array elements below the given level value are to be considered fully-connected (and hence elements above the value will only be face connected), or vice-versa. (See notes below for details.)
1222+
positive_orientation : either ‘low’ or ‘high’. Indicates whether the output contours will produce positively-oriented polygons around islands of low- or high-valued elements. If ‘low’ then contours will wind counter-clockwise around elements below the iso-value. Alternately, this means that low-valued elements are always on the left of the contour.
1223+
"""
1224+
return skimage.measure.find_contours(x, level, fully_connected='low', positive_orientation='low')
1225+
1226+
def pt2map(list_points=[], size=(100, 100), val=1):
1227+
""" Inputs a list of points, return a 2D image.
1228+
1229+
Parameters
1230+
--------------
1231+
list_points : list of [x, y].
1232+
size : tuple of (w, h) for output size.
1233+
val : float or int for the contour value.
1234+
"""
1235+
i_m = np.zeros(size)
1236+
if list_points == []:
1237+
return i_m
1238+
for xx in list_points:
1239+
for x in xx:
1240+
# print(x)
1241+
i_m[int(np.round(x[0]))][int(np.round(x[1]))] = val
1242+
return i_m
1243+
1244+
def binary_dilation(x, radius=3):
1245+
""" Return fast binary morphological dilation of an image.
1246+
see `skimage.morphology.binary_dilation <http://scikit-image.org/docs/dev/api/skimage.morphology.html#skimage.morphology.binary_dilation>`_.
1247+
1248+
Parameters
1249+
-----------
1250+
x : 2D array image.
1251+
radius : int for the radius of mask.
1252+
"""
1253+
from skimage.morphology import disk, binary_dilation
1254+
mask = disk(radius)
1255+
x = binary_dilation(image, selem=mask)
1256+
return x
1257+
1258+
def dilation(x, radius=3):
1259+
""" Return greyscale morphological dilation of an image,
1260+
see `skimage.morphology.dilation <http://scikit-image.org/docs/dev/api/skimage.morphology.html#skimage.morphology.dilation>`_.
1261+
1262+
Parameters
1263+
-----------
1264+
x : 2D array image.
1265+
radius : int for the radius of mask.
1266+
"""
1267+
from skimage.morphology import disk, dilation
1268+
mask = disk(radius)
1269+
x = dilation(x, selem=mask)
1270+
return x
1271+
1272+
1273+
1274+
12111275
## Sequence
12121276
def pad_sequences(sequences, maxlen=None, dtype='int32', padding='post', truncating='pre', value=0.):
12131277
"""Pads each sequence to the same length:

0 commit comments

Comments
 (0)