@@ -40,7 +40,7 @@ def generatesample():
40
40
print (sample )
41
41
return sample
42
42
43
- def main ():
43
+ def main (noiter = 15 ):
44
44
rospy .init_node ('camera_calibration' , anonymous = True )
45
45
46
46
bridge = CvBridge ()
@@ -50,10 +50,10 @@ def main():
50
50
K_matrix = np .array (Calib .K )
51
51
fk = forward_kinematics_extrinsics .forwardKinematics ('arm_base_link' , 'ar_tag' )
52
52
53
- positions = np .zeros ((15 , 3 ))
54
- orientations = np .zeros ((15 , 4 ))
55
- joint_angles = np .zeros ((15 , 5 ))
56
- end_effector_positions = np .zeros ((15 , 4 , 4 ))
53
+ positions = np .zeros ((noiter , 3 ))
54
+ orientations = np .zeros ((noiter , 4 ))
55
+ joint_angles = np .zeros ((noiter , 5 ))
56
+ end_effector_positions = np .zeros ((noiter , 4 , 4 ))
57
57
rad_from_deg = np .pi / 180.
58
58
59
59
# Send to Home Position
@@ -80,7 +80,7 @@ def artagcallback(i):
80
80
81
81
# # Send nodes of the path as commands to controller
82
82
count = 0
83
- while count < 15 :
83
+ while count < noiter :
84
84
85
85
joint_angle = generatesample ()
86
86
joint_angles [count , :] = joint_angle
@@ -108,17 +108,19 @@ def artagcallback(i):
108
108
109
109
count += 1
110
110
111
+ np .savez ("../data/calibration/calibration_info.npz" , position = positions , orientation = orientations , camerainfo = K_matrix , \
112
+ joint_angles = joint_angles , forward_kinematics = end_effector_positions )
111
113
112
114
R = calculate_extrinsic (positions , K_matrix , end_effector_positions )
113
115
print (R )
114
- # np.savez("../data/calibration/calibration_info.npz", position=positions, orientation=orientations, camerainfo=K_matrix, \
115
- # joint_angles=joint_angles, forward_kinematics=end_effector_position)
116
+
116
117
117
118
def calculate_extrinsic (positions , K_matrix , end_effector_positions ):
118
- p = get_p (positions )
119
+ p = get_p (K_matrix , positions )
119
120
P = get_P (end_effector_positions )
120
121
M = get_projection_matrix (p , P )
121
122
K ,R = linalg .rq (M [:,0 :3 ])
123
+ print (K / K [2 ,2 ])
122
124
T = np .matmul (np .linalg .inv (K ),M [:,3 ])
123
125
H = np .vstack ((np .hstack ((R ,T .reshape ((3 ,1 )))),np .array ([[0 ,0 ,0 ,1 ]])))
124
126
return H
@@ -140,10 +142,8 @@ def get_projection_matrix(p, P):
140
142
return M
141
143
142
144
143
- def get_p (position ):
144
- intrinsic = np .array ([[615.8053588867188 , 0.0 , 328.7790222167969 ],
145
- [0.0 , 615.8865356445312 , 229.94618225097656 ],
146
- [0.0 , 0.0 , 1.0 ]])
145
+ def get_p (K_matrix , position ):
146
+ intrinsic = K_matrix .reshape ((3 ,3 ))
147
147
K_3_4 = np .hstack ((intrinsic , np .array ([[0 ],[0 ],[0 ]])))
148
148
homogeneous_3D_point = np .hstack ((position , np .ones ((position .shape [0 ], 1 ))))
149
149
homogeneous_pixel_coordinate = np .matmul (K_3_4 ,homogeneous_3D_point .T )
@@ -152,49 +152,8 @@ def get_p(position):
152
152
153
153
154
154
def get_P (end_effector_position ):
155
- point_3D_world_frame = end_effector_position [:,3 , 0 : 3 ]
155
+ point_3D_world_frame = end_effector_position [:,0 : 3 , 3 ]
156
156
return point_3D_world_frame
157
157
158
158
if __name__ == "__main__" :
159
- main ()
160
-
161
- # class camera_calibration:
162
- # def __init__(self):
163
- # self.positions = np.zeros((15, 3))
164
- # self.orientations = np.zeros((15, 4))
165
-
166
- # self.sub_img = message_filters.Subscriber("camera/color/image_raw", Image)
167
- # self.sub_artag = message_filters.Subscriber("ar_pose_marker", AlvarMarkers)
168
- # self.ts = message_filters.ApproximateTimeSynchronizer([self.sub_img, self.sub_artag], 10, 0.1, allow_headerless=True)
169
- # self.ts.registerCallback(self.callback)
170
-
171
- # self.sub_info = rospy.Subscriber("camera/color/camera_info", CameraInfo, self.infocallback)
172
- # self.K_matrix = None
173
-
174
- # def callback(self, img, artag):
175
- # print(flag, 'Hiiiiiiii')
176
- # if artag.markers == None:
177
- # return
178
- # else:
179
- # try:
180
- # cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
181
- # except CvBridgeError as e:
182
- # print(e)
183
- # if flag != 0:
184
- # print(flag)
185
- # pos = artag.markers[0].pose.pose.position
186
- # self.positions[i, :] = np.array([pos.x, pos.y, pos.z])
187
- # q = artag.markers[0].pose.pose.orientation
188
- # self.orientations[i, :] = np.array([q.x, q.y, q.z, q.w])
189
- # cv2.imwrite('../data/caloibration/image_%d.png'%flag, cv_image)
190
- # flag == 0
191
- # if flag == 15:
192
- # np.savez("../data/calibration/artag_pos.npz", position = self.positions, orientation=self.orientations)
193
- # # (rows,cols,channels) = cv_image.shape
194
-
195
- # # cv2.imshow("Image window", cv_image)
196
- # # cv2.waitKey(3)
197
-
198
- # def infocallback(self, data):
199
- # if self.K_matrix == None:
200
- # self.K_matrix = data.K
159
+ main (100 )
0 commit comments