Skip to content

Commit 09eda2f

Browse files
authored
affine transform x and y (#904)
* update sampling layers * upadte zoom * fix bug zoom * typo * fix bug affine_transform_cv2 x and y * fix bug crop when crop size equal to image size
1 parent 17baf26 commit 09eda2f

File tree

2 files changed

+19
-25
lines changed

2 files changed

+19
-25
lines changed

docs/modules/prepro.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ The following is a typical Python program that applies rotation, shifting, flipp
165165
xx = tl.prepro.rotation(image, rg=-20, is_random=False)
166166
xx = tl.prepro.flip_axis(xx, axis=1, is_random=False)
167167
xx = tl.prepro.shear2(xx, shear=(0., -0.2), is_random=False)
168-
xx = tl.prepro.zoom(xx, zoom_range=1/0.8)
168+
xx = tl.prepro.zoom(xx, zoom_range=0.8)
169169
xx = tl.prepro.shift(xx, wrg=-0.1, hrg=0, is_random=False)
170170
171171
tl.vis.save_image(xx, '_result_slow.png')

tensorlayer/prepro.py

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ def affine_respective_zoom_matrix(w_range=0.8, h_range=1.1):
465465

466466

467467
# affine transform
468-
def transform_matrix_offset_center(matrix, x, y):
468+
def transform_matrix_offset_center(matrix, y, x):
469469
"""Convert the matrix from Cartesian coordinates (the origin in the middle of image) to Image coordinates (the origin on the top-left of image).
470470
471471
Parameters
@@ -817,12 +817,12 @@ def crop(x, wrg, hrg, is_random=False, row_index=0, col_index=1):
817817
"""
818818
h, w = x.shape[row_index], x.shape[col_index]
819819

820-
if (h <= hrg) or (w <= wrg):
821-
raise AssertionError("The size of cropping should smaller than the original image")
820+
if (h < hrg) or (w < wrg):
821+
raise AssertionError("The size of cropping should smaller than or equal to the original image")
822822

823823
if is_random:
824-
h_offset = int(np.random.uniform(0, h - hrg) - 1)
825-
w_offset = int(np.random.uniform(0, w - wrg) - 1)
824+
h_offset = int(np.random.uniform(0, h - hrg))
825+
w_offset = int(np.random.uniform(0, w - wrg))
826826
# tl.logging.info(h_offset, w_offset, x[h_offset: hrg+h_offset ,w_offset: wrg+w_offset].shape)
827827
return x[h_offset:hrg + h_offset, w_offset:wrg + w_offset]
828828
else: # central crop
@@ -857,12 +857,12 @@ def crop_multi(x, wrg, hrg, is_random=False, row_index=0, col_index=1):
857857
"""
858858
h, w = x[0].shape[row_index], x[0].shape[col_index]
859859

860-
if (h <= hrg) or (w <= wrg):
861-
raise AssertionError("The size of cropping should smaller than the original image")
860+
if (h < hrg) or (w < wrg):
861+
raise AssertionError("The size of cropping should smaller than or equal to the original image")
862862

863863
if is_random:
864-
h_offset = int(np.random.uniform(0, h - hrg) - 1)
865-
w_offset = int(np.random.uniform(0, w - wrg) - 1)
864+
h_offset = int(np.random.uniform(0, h - hrg))
865+
w_offset = int(np.random.uniform(0, w - wrg))
866866
results = []
867867
for data in x:
868868
results.append(data[h_offset:hrg + h_offset, w_offset:wrg + w_offset])
@@ -1479,10 +1479,7 @@ def zoom(x, zoom_range=(0.9, 1.1), flags=None, border_mode='constant'):
14791479
return x
14801480

14811481

1482-
def respective_zoom(
1483-
x, h_range=(0.9, 1.1), w_range=(0.9, 1.1), row_index=0, col_index=1, channel_index=2, fill_mode='nearest',
1484-
cval=0., order=1
1485-
):
1482+
def respective_zoom(x, h_range=(0.9, 1.1), w_range=(0.9, 1.1), flags=None, border_mode='constant'):
14861483
"""Zooming/Scaling a single image that height and width are changed independently.
14871484
14881485
Parameters
@@ -1497,14 +1494,9 @@ def respective_zoom(
14971494
The zooming/scaling ratio of width, greater than 1 means larger.
14981495
- float, a fixed ratio.
14991496
- tuple of 2 floats, randomly sample a value as the ratio between 2 values.
1500-
row_index col_index and channel_index : int
1501-
Index of row, col and channel, default (0, 1, 2), for theano (1, 2, 0).
1502-
fill_mode : str
1503-
Method to fill missing pixel, default `nearest`, more options `constant`, `reflect` or `wrap`, see `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`__
1504-
cval : float
1505-
Value used for points outside the boundaries of the input if mode='constant'. Default is 0.0.
1506-
order : int
1507-
The order of interpolation. The order has to be in the range 0-5. See ``tl.prepro.affine_transform`` and `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`__
1497+
border_mode : str
1498+
- `constant`, pad the image with a constant value (i.e. black or 0)
1499+
- `replicate`, the row or column at the very edge of the original is replicated to the extra border.
15081500
15091501
Returns
15101502
-------
@@ -1513,9 +1505,11 @@ def respective_zoom(
15131505
15141506
"""
15151507
zoom_matrix = affine_respective_zoom_matrix(h_range=h_range, w_range=w_range)
1516-
h, w = x.shape[row_index], x.shape[col_index]
1508+
h, w = x.shape[0], x.shape[1]
15171509
transform_matrix = transform_matrix_offset_center(zoom_matrix, h, w)
1518-
x = affine_transform(x, transform_matrix, channel_index, fill_mode, cval, order)
1510+
x = affine_transform_cv2(
1511+
x, transform_matrix, flags=flags, border_mode=border_mode
1512+
) #affine_transform(x, transform_matrix, channel_index, fill_mode, cval, order)
15191513
return x
15201514

15211515

@@ -1839,7 +1833,7 @@ def imresize(x, size=None, interp='bicubic', mode=None):
18391833
interp : str
18401834
Interpolation method for re-sizing (`nearest`, `lanczos`, `bilinear`, `bicubic` (default) or `cubic`).
18411835
mode : str
1842-
The PIL image mode (`P`, `L`, etc.) to convert arr before resizing.
1836+
The PIL image mode (`P`, `L`, etc.) to convert image before resizing.
18431837
18441838
Returns
18451839
-------

0 commit comments

Comments
 (0)