Skip to content

Commit 89410c6

Browse files
committed
Solved issue with calibration matrix
1 parent 69c7b5a commit 89410c6

File tree

1 file changed

+15
-56
lines changed

1 file changed

+15
-56
lines changed

scripts/Assignment3_Camera_Calibration.py

Lines changed: 15 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def generatesample():
4040
print(sample)
4141
return sample
4242

43-
def main():
43+
def main(noiter=15):
4444
rospy.init_node('camera_calibration', anonymous=True)
4545

4646
bridge = CvBridge()
@@ -50,10 +50,10 @@ def main():
5050
K_matrix = np.array(Calib.K)
5151
fk = forward_kinematics_extrinsics.forwardKinematics('arm_base_link', 'ar_tag')
5252

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))
5757
rad_from_deg = np.pi/180.
5858

5959
# Send to Home Position
@@ -80,7 +80,7 @@ def artagcallback(i):
8080

8181
# # Send nodes of the path as commands to controller
8282
count = 0
83-
while count < 15:
83+
while count < noiter:
8484

8585
joint_angle = generatesample()
8686
joint_angles[count, :] = joint_angle
@@ -108,17 +108,19 @@ def artagcallback(i):
108108

109109
count += 1
110110

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)
111113

112114
R = calculate_extrinsic(positions, K_matrix, end_effector_positions)
113115
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+
116117

117118
def calculate_extrinsic(positions, K_matrix, end_effector_positions):
118-
p = get_p(positions)
119+
p = get_p(K_matrix, positions)
119120
P = get_P(end_effector_positions)
120121
M = get_projection_matrix(p, P)
121122
K,R = linalg.rq(M[:,0:3])
123+
print(K/K[2,2])
122124
T = np.matmul(np.linalg.inv(K),M[:,3])
123125
H = np.vstack((np.hstack((R,T.reshape((3,1)))),np.array([[0,0,0,1]])))
124126
return H
@@ -140,10 +142,8 @@ def get_projection_matrix(p, P):
140142
return M
141143

142144

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))
147147
K_3_4 = np.hstack((intrinsic, np.array([[0],[0],[0]])))
148148
homogeneous_3D_point = np.hstack((position, np.ones((position.shape[0], 1))))
149149
homogeneous_pixel_coordinate = np.matmul(K_3_4,homogeneous_3D_point.T)
@@ -152,49 +152,8 @@ def get_p(position):
152152

153153

154154
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]
156156
return point_3D_world_frame
157157

158158
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

Comments
 (0)