Skip to content

Commit

Permalink
update prepro pt2map, find_contours, dilation
Browse files Browse the repository at this point in the history
  • Loading branch information
zsdonghao committed Jun 5, 2017
1 parent e851c85 commit 615bfca
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/modules/prepro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ Some of the code in this package are borrowed from Keras.

array_to_img

find_contours
pt2map
binary_dilation
dilation

pad_sequences
process_sequences
sequences_add_start_id
Expand Down Expand Up @@ -144,6 +149,21 @@ Numpy and PIL
^^^^^^^^^^^^^^
.. autofunction:: array_to_img

Find contours
^^^^^^^^^^^^^^
.. autofunction:: find_contours

Points to Image
^^^^^^^^^^^^^^^^^
.. autofunction:: pt2map

Binary dilation
^^^^^^^^^^^^^^^^^
.. autofunction:: binary_dilation

Greyscale dilation
^^^^^^^^^^^^^^^^^^^^
.. autofunction:: dilation

Sequence
---------
Expand Down
64 changes: 64 additions & 0 deletions tensorlayer/prepro.py
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,70 @@ def array_to_img(x, dim_ordering=(0,1,2), scale=True):
raise Exception('Unsupported channel number: ', x.shape[2])




def find_contours(x, level=0.8, fully_connected='low', positive_orientation='low'):
""" Find iso-valued contours in a 2D array for a given level value, returns list of (n, 2)-ndarrays
see `skimage.measure.find_contours <http://scikit-image.org/docs/dev/api/skimage.measure.html#skimage.measure.find_contours>`_ .
Parameters
------------
x : 2D ndarray of double. Input data in which to find contours.
level : float. Value along which to find contours in the array.
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.)
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.
"""
return skimage.measure.find_contours(x, level, fully_connected='low', positive_orientation='low')

def pt2map(list_points=[], size=(100, 100), val=1):
""" Inputs a list of points, return a 2D image.
Parameters
--------------
list_points : list of [x, y].
size : tuple of (w, h) for output size.
val : float or int for the contour value.
"""
i_m = np.zeros(size)
if list_points == []:
return i_m
for xx in list_points:
for x in xx:
# print(x)
i_m[int(np.round(x[0]))][int(np.round(x[1]))] = val
return i_m

def binary_dilation(x, radius=3):
""" Return fast binary morphological dilation of an image.
see `skimage.morphology.binary_dilation <http://scikit-image.org/docs/dev/api/skimage.morphology.html#skimage.morphology.binary_dilation>`_.
Parameters
-----------
x : 2D array image.
radius : int for the radius of mask.
"""
from skimage.morphology import disk, binary_dilation
mask = disk(radius)
x = binary_dilation(image, selem=mask)
return x

def dilation(x, radius=3):
""" Return greyscale morphological dilation of an image,
see `skimage.morphology.dilation <http://scikit-image.org/docs/dev/api/skimage.morphology.html#skimage.morphology.dilation>`_.
Parameters
-----------
x : 2D array image.
radius : int for the radius of mask.
"""
from skimage.morphology import disk, dilation
mask = disk(radius)
x = dilation(x, selem=mask)
return x




## Sequence
def pad_sequences(sequences, maxlen=None, dtype='int32', padding='post', truncating='pre', value=0.):
"""Pads each sequence to the same length:
Expand Down

0 comments on commit 615bfca

Please sign in to comment.