-
Notifications
You must be signed in to change notification settings - Fork 8
/
star_image_class.py
187 lines (155 loc) · 6.58 KB
/
star_image_class.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
from math import sin,cos,tan,radians,degrees,atan,sqrt
import numpy as np
import pandas as pd
import cv2
import matplotlib.pyplot as plt
class StarImage():
#Default settings properties
l = 3280
w = 2464
f = 0.00304
myu = 1.12*(10**-6)
star_catalogue_path = 'filtered_catalogue/Below_6.0_SAO.csv'
def __init__(self,ra,de,roll):
self.ra = ra
self.de = de
self.roll = roll
def create_M_matrix(self):
"""[summary]
Args:
ra ([int]): [right ascension of sensor center]
de ([int]): [declination of sensor center]
roll ([int]): [roll angle of star sensor]
method ([int]): [1 for method 1(Calculating each elements),2 for method 2(calculating rotation matrices)]
"""
ra = self.ra
de = self.de
roll = self.roll
a1 = (sin(ra)*cos(roll)) - (cos(ra)*sin(de)*sin(roll))
a2 = -(sin(ra)*sin(roll)) - (cos(ra)*sin(de)*cos(roll))
a3 = -(cos(ra)*cos(de))
b1 = -(cos(ra)*cos(roll)) - (sin(ra)*sin(de)*sin(roll))
b2 = (cos(ra)*sin(roll)) - (sin(ra)*sin(de)*cos(roll))
b3 = -(sin(ra)*cos(de))
c1 = (cos(ra)*sin(roll))
c2 = (cos(ra)*cos(roll))
c3 = -(sin(de))
M = np.array([[a1,a2,a3],[b1,b2,b3],[c1,c2,c3]])
return M
def dir_vector_to_star_sensor(self,ra,de,M_transpose):
"""[Converts direction vector to star sensor coordinates]
Args:
ra ([int]): [right ascension of the object vector]
de ([int]): [desclination of the object vector]
M_transpose ([numpy array]): [rotation matrix from direction vector to star sensor transposed]
"""
x_dir_vector = (cos(ra)*cos(de))
y_dir_vector = (sin(ra)*cos(de))
z_dir_vector = (sin(de))
dir_vector_matrix = np.array([[x_dir_vector],[y_dir_vector],[z_dir_vector]])
return M_transpose.dot(dir_vector_matrix)
def draw_star(self,x,y,magnitude,gaussian,background,ROI=5):
mag = abs(magnitude-7)
radius = int(round((mag/9)*(5)+3))
color = int(round((mag/9)*(155)+100))
cv2.circle(background,(x,y),radius,color,thickness=-1)
return background
def add_noise(self,low,high,background):
"""[Adds noise to an image]
Args:
low ([int]): [lower threshold of the noise generated]
high ([int]): [maximum pixel value of the noise generated]
background ([numpy array]): [the image that is put noise on]
"""
row,col = np.shape(background)
background = background.astype(int)
noise = np.random.randint(low,high=high,size=(row,col))
noised_img = cv2.addWeighted(noise,0.1,background,0.9,0)
return noised_img
def create_star_image(self):
ra = radians(float(self.ra))
de = radians(float(self.de))
roll = radians(float(self.roll))
FOVy = degrees(2*atan((self.myu*self.w/2)/self.f))
FOVx = degrees(2*atan((self.myu*self.l/2)/self.f))
print(FOVx,FOVy)
M = self.create_M_matrix()
M_transpose = np.round(np.matrix.transpose(M),decimals=5)
col_list = ["Star ID","RA","DE","Magnitude"]
star_catalogue = pd.read_csv(self.star_catalogue_path,usecols=col_list)
R = (sqrt((radians(FOVx)**2)+(radians(FOVy)**2))/2)
alpha_start = (ra - (R/cos(de)))
alpha_end = (ra + (R/cos(de)))
delta_start = (de - R)
delta_end = (de + R)
star_within_ra_range = (alpha_start <= star_catalogue['RA']) & (star_catalogue['RA'] <= alpha_end)
star_within_de_range = (delta_start <= star_catalogue['DE']) & (star_catalogue['DE'] <= delta_end)
star_in_ra = star_catalogue[star_within_ra_range]
star_in_de = star_catalogue[star_within_de_range]
star_in_de = star_in_de[['Star ID']].copy()
stars_within_FOV = pd.merge(star_in_ra,star_in_de,on="Star ID")
#Converting to star sensor coordinate system
ra_i = list(stars_within_FOV['RA'])
de_i = list(stars_within_FOV['DE'])
star_sensor_coordinates = []
for i in range(len(ra_i)):
coordinates = self.dir_vector_to_star_sensor(ra_i[i],de_i[i],M_transpose=M_transpose)
star_sensor_coordinates.append(coordinates)
#Conversion of star sensor coordinate system to image coordinate system
star_loc = []
for coord in star_sensor_coordinates:
x = self.f*(coord[0]/coord[2])
y = self.f*(coord[1]/coord[2])
star_loc.append((x,y))
xtot = 2*tan(radians(FOVx)/2)*self.f
ytot = 2*tan(radians(FOVy)/2)*self.f
xpixel = self.l/xtot
ypixel = self.w/ytot
magnitude_mv = list(stars_within_FOV['Magnitude'])
filtered_magnitude = []
#Rescaling to pixel sizes
pixel_coordinates = []
delete_indices = []
for i,(x1,y1) in enumerate(star_loc):
x1 = float(x1)
y1 = float(y1)
x1pixel = round(xpixel*x1)
y1pixel = round(ypixel*y1)
if abs(x1pixel) > self.l/2 or abs(y1pixel) > self.w/2:
delete_indices.append(i)
continue
pixel_coordinates.append((x1pixel,y1pixel))
filtered_magnitude.append(magnitude_mv[i])
background = np.zeros((self.w,self.l))
for i in range(len(filtered_magnitude)):
x = round(self.l/2 + pixel_coordinates[i][0])
y = round(self.w/2 - pixel_coordinates[i][1])
background = self.draw_star(x,y,filtered_magnitude[i],False,background)
#Adding noise
background = self.add_noise(0,50,background=background)
return background
def config_settings(self,l,w,f,myu,star_catalogue_path):
"""[Configure the sensor settings]
Args:
l ([int]): [pixel length]
w ([int]): [pixel width]
f ([float]): [focal length in meters]
myu ([float]): [length/pixel]
star_catalogue_path ([str]): [the path in which the star catalogue is in]
"""
self.l = l
self.w = w
self.f = f
self.myu = myu
self.star_catalogue_path = star_catalogue_path
def displayImg(self,cmap='gray'):
"""[Displays image]
Args:
img ([numpy array]): [the pixel values in the form of numpy array]
cmap ([string], optional): [can be 'gray']. Defaults to None.
"""
img = self.create_star_image()
fig = plt.figure(figsize=(12,10))
ax = fig.add_subplot(111)
ax.imshow(img,cmap)
plt.show()