-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshear.py
69 lines (53 loc) · 1.82 KB
/
shear.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import numpy as np
def vertical_shear(origin_img, sv = 0.5):
"""Shears an image vertically
Parameters:
----------
origin_img
original image to shear iamge vertically
sv: float, optional
shearing percentage
Returns:
-----
out_img
vertically sheared image
"""
# find height and width
im_height, im_width = origin_img.shape[:2]
# set size for output image
out_img = np.zeros_like(origin_img)
# build a vertical shear matrix
vert_shear_matrix = np.array([[1, sv, 0], [0, 1, 0], [0, 0, 1]])
# transform each pixels
for i_coor, j_coor in np.ndindex(origin_img.shape[:2]):
x, y, _ = vert_shear_matrix @ np.array([i_coor, j_coor, 1])
if 0 < round(x) < origin_img.shape[0] and 0 < round(y) < origin_img.shape[1]:
out_img[i_coor][j_coor] = origin_img[round(x)][round(y)]
# return rotated image
return out_img
def horizontal_shear(origin_img, sh = 0.5):
"""Shears an image horizontal
Parameters:
----------
origin_img
original image to shear iamge horizontally
sv: float, optional
shearing percentage
Returns:
-----
out_img
horizontally sheared image
"""
# find height and width
im_height, im_width = origin_img.shape[:2]
# set size for output image
out_img = np.zeros_like(origin_img)
# build a vertical shear matrix
hori_shear_matrix = np.array([[1, 0, 0], [sh, 1, 0], [0, 0, 1]])
# transform each pixels
for i_coor, j_coor in np.ndindex(origin_img.shape[:2]):
x, y, _ = hori_shear_matrix @ np.array([i_coor, j_coor, 1])
if 0 < round(x) < origin_img.shape[0] and 0 < round(y) < origin_img.shape[1]:
out_img[i_coor][j_coor] = origin_img[round(x)][round(y)]
# return rotated image
return out_img