-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimg_helper.py
149 lines (116 loc) · 3.7 KB
/
img_helper.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#https://alyssaq.github.io/2015/computing-the-axes-or-orientation-of-a-blob/
#https://en.wikipedia.org/wiki/Image_moment
import math
import numpy as np
from astropy.io import fits
from PIL import Image
def raw_moment(img, x, y, i, j):
return (img[y,x] * x**i * y**j).sum()
def central_moment(img, x, xc, y, yc, p, q):
return (img[y,x] * (x-xc)**p * (y-yc)**q).sum()
def img_center(img, src_map):
y, x = np.where(src_map)
moment = lambda i, j: raw_moment(img, x, y, i, j)
m00 = moment(0, 0)
m01 = moment(0, 1)
m10 = moment(1, 0)
x_centroid = m10 / m00
y_centroid = m01 / m00
return (y_centroid, x_centroid)
def moments_cov(img, src_map):
y, x = np.where(src_map)
moment = lambda i, j: raw_moment(img, x, y, i, j)
m00 = moment(0, 0)
m10 = moment(1, 0)
m01 = moment(0, 1)
x_centroid = m10 / m00
y_centroid = m01 / m00
u11 = (moment(1, 1) - x_centroid * m01) / m00
u20 = (moment(2, 0) - x_centroid * m10) / m00
u02 = (moment(0, 2) - y_centroid * m01) / m00
cov = np.array([[u20, u11], [u11, u02]])
return cov
def get_theta(img, src_map):
cy, cx = img_center(img, src_map)
cov = moments_cov(img, src_map)
evals, evecs = np.linalg.eig(cov)
pairs = {}
for i in range(2):
pairs[evals[i]] = evecs[:, i]
major_x, major_y = pairs[evals.max()]
theta = np.arctan(major_y/major_x)
return theta
def get_params(img, src_map):
y, x = np.where(src_map)
# raw moments
M = lambda i, j: raw_moment(img, x, y, i, j)
M00 = M(0, 0)
M10 = M(1, 0)
M01 = M(0, 1)
xc = M10/M00
yc = M01/M00
# cenral moments
μ = lambda p, q: central_moment(img, x, xc, y, yc, p, q)
μ11 = μ(1, 1)
μ20 = μ(2, 0)
μ02 = μ(0, 2)
# second order central moments
μμ = lambda p, q: μ(p, q) / μ(0, 0)
μμ20 = μμ(2, 0)
μμ02 = μμ(0, 2)
μμ11 = μμ(1, 1)
# angle
Θ = 0.5 * np.arctan((2 * μμ11) / (μμ20 - μμ02))
# axis ratio
cov = np.array([
[μμ20, μμ11],
[μμ11, μμ02]
])
evals, _ = np.linalg.eig(cov)
axis_ratio = np.sqrt(evals.min()/evals.max())
# γ1 and γ2
y, x = np.where(np.ones_like(img, dtype=np.bool))
M_seg = lambda i, j: raw_moment(src_map, x, y, i, j)
Ixx = M_seg(1, 0)
Iyy = M_seg(0, 1)
print(Ixx, Iyy)
γ1 = np.sqrt((Ixx + Iyy) / (1 + axis_ratio**2))
γ2 = axis_ratio * γ1
return Θ, axis_ratio, γ1, γ2, (yc, xc)
def _translate(u,v):
return np.array([
[1.0, 0.0, float(u)],
[0.0, 1.0, float(v)],
[0.0, 0.0, 1.0]
])
def _rotate(angle):
angle = -math.radians(angle)
cos = math.cos(angle)
sin = math.sin(angle)
return np.array([
[cos, sin, 0.0],
[-sin, cos, 0.0],
[0.0, 0.0, 1.0]
])
def rotate_img(img, src_map, theta):
cy, cx = img_center(img, src_map)
to_origin = _translate(cy, cx)
rotate = _rotate(-np.rad2deg(theta))
recenter = _translate(-cy, -cx)
trans = to_origin.dot(rotate).dot(recenter)
trans = tuple(trans.flatten()[:6])
tmp = Image.fromarray(img)
tmp = tmp.transform((84,84), Image.AFFINE, data=trans, resample=Image.BILINEAR)
img = np.asarray(tmp)
return img
# def rotate_img(img, src_map):
# cy, cx = img_center(img, src_map)
# to_origin = _translate(cy, cx)
# rotate = _rotate(-np.rad2deg(get_theta(img, src_map)))
# recenter = _translate(-cy, -cx)
# trans = to_origin.dot(rotate).dot(recenter)
# trans = tuple(trans.flatten()[:6])
# tmp = Image.fromarray(img)
# tmp = tmp.transform((84,84), Image.AFFINE, data=trans, resample=Image.BILINEAR)
# img = np.asarray(tmp)
# return img