@@ -70,6 +70,25 @@ def makePoseObject(temp):
7070 ),
7171 )
7272
73+ # ROT MUST BE A 3x3 ROTATION MATRIX
74+ def getTransform (trans , rot ):
75+ return np .concatenate (
76+ (np .concatenate ((rot , trans ), axis = 1 ), np .asarray ([[0 , 0 , 0 , 1 ]])), axis = 0
77+ )
78+
79+ def getTagTransform (jsonID ):
80+ pose = Processor .makePoseObject (jsonID )
81+ rot = pose .rotation ()
82+ translation = np .asarray ([pose .X (), pose .Y (), pose .Z ()]).reshape (3 , 1 )
83+ rot , _ = cv2 .Rodrigues (np .asarray ([rot .X (), rot .Y (), rot .Z ()]).reshape (3 , 1 ))
84+ return Processor .getTransform (translation , rot )
85+
86+ def getRotFromTransform (transform ):
87+ return transform [:3 , :3 ]
88+
89+ def getTranslationFromTransform (transform ):
90+ return transform [:3 , 3 ]
91+
7392 # Translate corner locations
7493 @staticmethod
7594 def addCorners (tagPos , cornerPos ):
@@ -183,7 +202,7 @@ def imagePose(self, images, K, D, layout, arucoDetector):
183202 flags = cv2 .SOLVEPNP_IPPE_SQUARE ,
184203 )
185204
186- # Find tag ambiguity
205+ # Find poses for one tag case
187206 if len (ids ) == 1 :
188207 ambig .append (reproj [0 ][0 ] / reproj [1 ][0 ])
189208
@@ -210,6 +229,55 @@ def imagePose(self, images, K, D, layout, arucoDetector):
210229 if len (ids ) > 1 :
211230 ambig .append (2767 )
212231
232+ if len (ids ) == 1 :
233+ tempc1 = np .asarray (
234+ [self .squareLength / 2 , self .squareLength / 2 , 0 ]
235+ )
236+ tempc2 = np .asarray (
237+ [- self .squareLength / 2 , self .squareLength / 2 , 0 ]
238+ )
239+ tempc3 = np .asarray (
240+ [self .squareLength / 2 , - self .squareLength / 2 , 0 ]
241+ )
242+ tempc4 = np .asarray (
243+ [- self .squareLength / 2 , - self .squareLength / 2 , 0 ]
244+ )
245+
246+ ret , rvecs , tvecs , reproj = cv2 .solvePnPGeneric (
247+ np .asarray ([tempc2 , tempc1 , tempc3 , tempc4 ]),
248+ corners [0 ][0 ],
249+ K [imgIndex ],
250+ D [imgIndex ],
251+ flags = cv2 .SOLVEPNP_IPPE_SQUARE ,
252+ )
253+ # Processor.logger.info(tvecs)
254+ curId = ids [0 ][0 ]
255+ camTvecs = tvecs [0 ].reshape (3 , 1 )
256+
257+ x = camTvecs [0 ]
258+ y = camTvecs [1 ]
259+ z = camTvecs [2 ]
260+
261+ camTvecs = np .asarray ([z , - x , - y ]).reshape (3 , 1 )
262+
263+ camRvecs = rvecs [0 ]
264+
265+ x = camRvecs [0 ]
266+ y = camRvecs [1 ]
267+ z = camRvecs [2 ]
268+
269+ camRvecs = np .asarray ([z , - x , - y ]).reshape (3 , 1 )
270+
271+ camRotMat , _ = cv2 .Rodrigues (camRvecs )
272+ cam2Tag = Processor .getTransform (camTvecs , camRotMat )
273+ world2Tag = Processor .getTagTransform (layout [curId - 1 ]["pose" ])
274+
275+ world2Cam = np .dot (world2Tag , np .linalg .inv (cam2Tag ))
276+
277+ transVec = Processor .getTranslationFromTransform (world2Cam )
278+ rvecs = Processor .getRotFromTransform (world2Cam )
279+
280+ else :
213281 # Calculate robot pose with 2d and 3d points
214282 ret , rvecs , tvecs = cv2 .solvePnP (
215283 tagLoc ,
@@ -222,23 +290,24 @@ def imagePose(self, images, K, D, layout, arucoDetector):
222290 # Grab the rotation matrix and find the translation vector
223291 rotMat , _ = cv2 .Rodrigues (rvecs )
224292 transVec = - np .dot (np .transpose (rotMat ), tvecs )
293+ transVec = np .asarray ([transVec [2 ], - transVec [0 ], - transVec [1 ]])
225294
226- # Convert the rotation matrix to a three rotation system (yaw, pitch, roll)
227- rot3D = wpi .Rotation3d (
228- np .array ([rvecs [2 ][0 ], - rvecs [0 ][0 ], + rvecs [1 ][0 ]]),
229- (rvecs [0 ][0 ] ** 2 + rvecs [1 ][0 ] ** 2 + rvecs [2 ][0 ] ** 2 ) ** 0.5 ,
230- )
295+ # Convert the rotation matrix to a three rotation system (yaw, pitch, roll)
296+ rot3D = wpi .Rotation3d (
297+ np .array ([rvecs [2 ][0 ], - rvecs [0 ][0 ], + rvecs [1 ][0 ]]),
298+ (rvecs [0 ][0 ] ** 2 + rvecs [1 ][0 ] ** 2 + rvecs [2 ][0 ] ** 2 ) ** 0.5 ,
299+ )
231300
232- # Append the pose
233- poses .append (
234- wpi .Pose3d (
235- # Translation between openCV and WPILib
236- wpi .Translation3d (transVec [2 ], - transVec [0 ], - transVec [1 ]),
237- rot3D ,
238- )
301+ # Append the pose
302+ poses .append (
303+ wpi .Pose3d (
304+ # Translation between openCV and WPILib
305+ wpi .Translation3d (transVec [0 ], transVec [1 ], transVec [2 ]),
306+ rot3D ,
239307 )
240- tags .append (curTags )
241- num += 1
308+ )
309+ tags .append (curTags )
310+ num += 1
242311 else :
243312 # If no tags
244313 poses .append (Processor .BAD_POSE )
0 commit comments