Skip to content

Commit 5eab80c

Browse files
committed
add transform applying transform matrix
1 parent 486adeb commit 5eab80c

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

uncleloader/dataloader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ def __len__(self):
294294
print(test)
295295
print(data.shape, label)
296296
test += 1
297-
break
297+
#break
298298
print(time.time() - a )
299299

300300

uncleloader/transform_tmp.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,24 @@
99
import cv2
1010
import random
1111

12-
def image_resize(img, size=(150, 150)):
13-
return cv2.resize(img, size, interpolation=cv2.INTER_CUBIC)
12+
paddings = {'z': cv2.BORDER_CONSTANT, 'r': cv2.BORDER_REFLECT_101}
13+
interpolations = {'bilinear': cv2.INTER_LINEAR, 'bicubic': cv2.INTER_CUBIC, 'nearest': cv2.INTER_NEAREST}
1414

15-
def resize_shorter(img, shorter_length=300):
15+
def _apply_perspective(img, M, shape, interp_mode='bilinear', padding_mode='r'):
16+
return cv2.warpPerspective(img, M, shape,
17+
flags=interpolations[interp_mode],
18+
borderMode=paddings[padding_mode])
19+
20+
def image_resize(img, size=(150, 150), interp_mode='bilinear'):
21+
return cv2.resize(img, size, interpolation=interpolations[interp_mode])
22+
23+
def resize_shorter(img, shorter_length=300, interp_mode='bilinear'):
1624
rows, cols = img.shape[:2]
1725
if rows >= cols:
18-
#size = (int(1.0*rows*shorter_length/cols), int(shorter_length))
1926
size = (int(shorter_length), int(1.0*rows*shorter_length/cols))
2027
else:
21-
#size = (int(shorter_length), int(1.0*cols*shorter_length/rows))
2228
size = (int(1.0*cols*shorter_length/rows), int(shorter_length))
23-
24-
return cv2.resize(img, size, interpolation=cv2.INTER_CUBIC)
29+
return cv2.resize(img, size, interpolation=interpolations[interp_mode])
2530

2631
def random_flip_left_right(img):
2732
if random.random() > 0.5:
@@ -33,7 +38,7 @@ def random_flip_up_down(img):
3338
return cv2.flip(img, 0)
3439
return img
3540

36-
def random_rotate(img, rotage_range=(0, 180), random_position=False):
41+
def random_rotate(img, rotage_range=(0, 180), random_position=False, interp_mode='bilinear', padding_mode='r'):
3742
angel = np.random.uniform(rotage_range[0], rotage_range[1])
3843
rows, cols = img.shape[:2]
3944

@@ -45,7 +50,8 @@ def random_rotate(img, rotage_range=(0, 180), random_position=False):
4550
cen_y = int(rows/2)
4651

4752
M = cv2.getRotationMatrix2D((cen_x, cen_y), angel, 1)
48-
img = cv2.warpAffine(img, M, (rows, cols))
53+
M = np.concatenate([M, [[0, 0, 1]]], axis=0)
54+
img = _apply_perspective(img, M, (rows, cols), interp_mode, padding_mode)
4955

5056
return img
5157

@@ -63,20 +69,20 @@ def random_crop(img, crop_size):
6369
return img[start_x:end_x, start_y:end_y, :]
6470

6571

66-
def random_shear(img, range_x=(-0.5, 0.5), range_y=(0, 0)):
72+
def random_shear(img, range_x=(-0.5, 0.5), range_y=(0, 0), interp_mode='bilinear', padding_mode='r'):
6773
rows, cols = img.shape[:2]
6874
shear_x = np.random.uniform(range_x[0], range_x[1])
6975
shear_y = np.random.uniform(range_y[0], range_y[1])
70-
M = np.array([1, shear_x, 0, shear_y, 1, 0]).reshape((2, 3)).astype(np.float32)
71-
img = cv2.warpAffine(img, M, (rows, cols))
76+
M = np.array([1, shear_x, 0, shear_y, 1, 0, 0, 0, 1]).reshape((3, 3)).astype(np.float32)
77+
img = _apply_perspective(img, M, (rows, cols), interp_mode, padding_mode)
7278
return img
7379

74-
def random_rescale(img, range_x=(0.5, 1.5), range_y=(1, 1)):
80+
def random_rescale(img, range_x=(0.5, 1.5), range_y=(1, 1), interp_mode='bilinear', padding_mode='r'):
7581
rows, cols = img.shape[:2]
7682
scale_x = np.random.uniform(range_x[0], range_x[1])
7783
scale_y = np.random.uniform(range_y[0], range_y[1])
78-
M = np.array([scale_x, 0, 0, 0, scale_y, 0]).reshape((2, 3)).astype(np.float32)
79-
img = cv2.warpAffine(img, M, (rows, cols))
84+
M = np.array([scale_x, 0, 0, 0, scale_y, 0, 0, 0, 1]).reshape((3, 3)).astype(np.float32)
85+
img = _apply_perspective(img, M, (rows, cols), interp_mode, padding_mode)
8086
return img
8187

8288

@@ -103,7 +109,7 @@ def random_hsv(img, h_range=(-720, 720), s_range=(-40, 40), v_range=(-40, 40)):
103109

104110
def transform(img, label):
105111
#img = random_rescale(img)
106-
#img = random_shear(img, range_x=(-0.5, 0.5), range_y=(-0.5, 0.5))
112+
img = random_shear(img, range_x=(-0.5, 0.5), range_y=(-0.5, 0.5))
107113
#img = random_rotate(img, random_position=False)
108114
#img = image_resize(img, size=(400, 400))
109115
img = resize_shorter(img, shorter_length=300)

0 commit comments

Comments
 (0)