Skip to content

Commit 0723003

Browse files
authored
cv2 transform support 2 modes; (#885)
* cv2 transform support 2 modes; * update docs
1 parent 1e7b806 commit 0723003

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

tensorlayer/prepro.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ def affine_transform(x, transform_matrix, channel_index=2, fill_mode='nearest',
551551
apply_transform = affine_transform
552552

553553

554-
def affine_transform_cv2(x, transform_matrix, flags=None, borderMode=None):
554+
def affine_transform_cv2(x, transform_matrix, flags=None, border_mode='constant'):
555555
"""Return transformed images by given an affine matrix in OpenCV format (x is width). (Powered by OpenCV2, faster than ``tl.prepro.affine_transform``)
556556
557557
Parameters
@@ -560,21 +560,28 @@ def affine_transform_cv2(x, transform_matrix, flags=None, borderMode=None):
560560
An image with dimension of [row, col, channel] (default).
561561
transform_matrix : numpy.array
562562
A transform matrix, OpenCV format.
563+
border_mode : str
564+
- `constant`, pad the image with a constant value (i.e. black or 0)
565+
- `replicate`, the row or column at the very edge of the original is replicated to the extra border.
563566
564567
Examples
565568
--------
566569
>>> M_shear = tl.prepro.affine_shear_matrix(intensity=0.2, is_random=False)
567570
>>> M_zoom = tl.prepro.affine_zoom_matrix(zoom_range=0.8)
568571
>>> M_combined = M_shear.dot(M_zoom)
569-
>>> result = affine_transform_cv2(image, M_combined)
572+
>>> result = tl.prepro.affine_transform_cv2(image, M_combined)
570573
"""
571574
rows, cols = x.shape[0], x.shape[1]
572575
if flags is None:
573576
flags = cv2.INTER_AREA
574-
if borderMode is None:
575-
borderMode = cv2.BORDER_CONSTANT
577+
if border_mode is 'constant':
578+
border_mode = cv2.BORDER_CONSTANT
579+
elif border_mode is 'replicate':
580+
border_mode = cv2.BORDER_REPLICATE
581+
else:
582+
raise Exception("unsupport border_mode, check cv.BORDER_ for more details.")
576583
return cv2.warpAffine(x, transform_matrix[0:2,:], \
577-
(cols,rows), flags=flags, borderMode=borderMode)
584+
(cols,rows), flags=flags, borderMode=border_mode)
578585

579586

580587
def affine_transform_keypoints(coords_list, transform_matrix):

0 commit comments

Comments
 (0)