-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcenterofmass.py
61 lines (45 loc) · 1.51 KB
/
centerofmass.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
#!/usr/bin/env python
"""Center of mass algorithm based on Fourier transform and filtering"""
import numpy as np
def center_of_mass(img):
"""Find the center of mass of a focused spot on a noisy background.
This is done by the Fourier transform method as discussed by Weisshaar et al.
(http://www.mnd-umwelttechnik.fh-wiesbaden.de/pig/weisshaar_u5.pdf), which
is insensitive to noise. This usually skews the result towards the center of
the image for the classical CoM algorithm. A tuple with the CoM coordinates
is returned.
**Inputs**
* img: 2D array, containing image data
**Outputs**
* com: tuple, containing the x,y coordinates of the center of mass
"""
img = np.matrix(img)
rbnd, cbnd = img.shape
i = np.matrix(np.arange(0, rbnd))
sin_a = np.sin((i-1)*2*np.pi / (rbnd-1))
cos_a = np.cos((i-1)*2*np.pi / (rbnd-1))
j = np.matrix(np.arange(0, cbnd)).transpose()
sin_b = np.sin((j-1)*2*np.pi / (cbnd-1))
cos_b = np.cos((j-1)*2*np.pi / (cbnd-1))
a = (cos_a * img).sum()
b = (sin_a * img).sum()
c = (img * cos_b).sum()
d = (img * sin_b).sum()
if a>0:
if b>0:
rphi = 0
else:
rphi = 2*np.pi
else:
rphi = np.pi
if c>0:
if d>0:
cphi = 0
else:
cphi = 2*np.pi
else:
cphi = np.pi
x = (np.arctan(b/a) + rphi) * (rbnd - 1)/(2*np.pi) + 1
y = (np.arctan(d/c) + cphi) * (cbnd - 1)/(2*np.pi) + 1
com = (x, y)
return com