Skip to content

Commit 1897d37

Browse files
committed
[PREPRO] image projective transform
1 parent 48c9d25 commit 1897d37

File tree

2 files changed

+63
-6
lines changed

2 files changed

+63
-6
lines changed

docs/modules/prepro.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ Some of the code in this package are borrowed from Keras.
4040

4141
transform_matrix_offset_center
4242
apply_transform
43+
projective_transform_by_points
44+
4345
array_to_img
4446

4547
pad_sequences
@@ -114,6 +116,7 @@ Manual transform
114116
^^^^^^^^^^^^^^^^^
115117
.. autofunction:: transform_matrix_offset_center
116118
.. autofunction:: apply_transform
119+
.. autofunction:: projective_transform_by_points
117120

118121

119122
Numpy and PIL

tensorlayer/prepro.py

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
import scipy
2020
from scipy import linalg
2121
import scipy.ndimage as ndi
22-
22+
from skimage import transform
23+
# import skimage
2324
from skimage import exposure
2425

2526
# linalg https://docs.scipy.org/doc/scipy/reference/linalg.html
@@ -518,8 +519,6 @@ def zoom_multi(x, zoom_range=(0.9, 1.1), is_random=False,
518519
# image = tf.image.random_hue(image, max_delta=0.032)
519520
# image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
520521

521-
522-
523522
# brightness
524523
def brightness(x, gamma=1, gain=1, is_random=False):
525524
"""Change the brightness of a single image, randomly or non-randomly.
@@ -530,12 +529,10 @@ def brightness(x, gamma=1, gain=1, is_random=False):
530529
An image with dimension of [row, col, channel] (default).
531530
gamma : float, small than 1 means brighter.
532531
Non negative real number. Default value is 1.
533-
534-
- If is_random is True, gamma in a range of (1-gamma, 1+gamma).
532+
- If is_random is True, gamma in a range of (1-gamma, 1+gamma).
535533
gain : float
536534
The constant multiplier. Default value is 1.
537535
is_random : boolean, default False
538-
539536
- If True, randomly change brightness.
540537
541538
References
@@ -843,6 +840,63 @@ def apply_transform(x, transform_matrix, channel_index=2, fill_mode='nearest', c
843840
x = np.rollaxis(x, 0, channel_index+1)
844841
return x
845842

843+
844+
def projective_transform_by_points(x, src, dst, map_args={}, output_shape=None, order=1, mode='constant', cval=0.0, clip=True, preserve_range=False):
845+
"""Projective transform by given coordinates, usually 4 coordinates. see `scikit-image <http://scikit-image.org/docs/dev/auto_examples/applications/plot_geometric.html>`_.
846+
847+
Parameters
848+
-----------
849+
x : numpy array
850+
An image with dimension of [row, col, channel] (default).
851+
src : list or numpy
852+
The original coordinates, usually 4 coordinates of (x, y).
853+
dst : list or numpy
854+
The coordinates after transformation, the number of coordinates is the same with src.
855+
map_args : dict, optional
856+
Keyword arguments passed to inverse_map.
857+
output_shape : tuple (rows, cols), optional
858+
Shape of the output image generated. By default the shape of the input image is preserved. Note that, even for multi-band images, only rows and columns need to be specified.
859+
order : int, optional
860+
The order of interpolation. The order has to be in the range 0-5:
861+
- 0 Nearest-neighbor
862+
- 1 Bi-linear (default)
863+
- 2 Bi-quadratic
864+
- 3 Bi-cubic
865+
- 4 Bi-quartic
866+
- 5 Bi-quintic
867+
mode : {‘constant’, ‘edge’, ‘symmetric’, ‘reflect’, ‘wrap’}, optional
868+
Points outside the boundaries of the input are filled according to the given mode. Modes match the behaviour of numpy.pad.
869+
cval : float, optional
870+
Used in conjunction with mode ‘constant’, the value outside the image boundaries.
871+
clip : bool, optional
872+
Whether to clip the output to the range of values of the input image. This is enabled by default, since higher order interpolation may produce values outside the given input range.
873+
preserve_range : bool, optional
874+
Whether to keep the original range of values. Otherwise, the input image is converted according to the conventions of img_as_float.
875+
876+
Examples
877+
--------
878+
>>> Assume X is an image from CIFAR 10, i.e. shape == (32, 32, 3)
879+
>>> src = [[0,0],[0,32],[32,0],[32,32]]
880+
>>> dst = [[10,10],[0,32],[32,0],[32,32]]
881+
>>> x = projective_transform_by_points(X, src, dst)
882+
883+
References
884+
-----------
885+
- `scikit-image : geometric transformations <http://scikit-image.org/docs/dev/auto_examples/applications/plot_geometric.html>`_
886+
- `scikit-image : examples <http://scikit-image.org/docs/dev/auto_examples/index.html>`_
887+
"""
888+
if type(src) is list: # convert to numpy
889+
src = np.array(src)
890+
if type(dst) is list:
891+
dst = np.array(dst)
892+
if np.max(x)>1: # convert to [0, 1]
893+
x = x/255
894+
895+
m = transform.ProjectiveTransform()
896+
m.estimate(dst, src)
897+
warped = transform.warp(x, m, map_args=map_args, output_shape=output_shape, order=order, mode=mode, cval=cval, clip=clip, preserve_range=preserve_range)
898+
return warped
899+
846900
# Numpy and PIL
847901
def array_to_img(x, dim_ordering=(0,1,2), scale=True):
848902
"""Converts a numpy array to PIL image object (uint8 format).

0 commit comments

Comments
 (0)