I am trying to find out the angle of rotation of an object using opencv python. For this purpose I have detected the target then I am able to find out the rotation vector and I am converting it to rotation matrix and from this matrix I am calculating the euler angle. But when I rotate the object in the video then all three angles (x, y, z) changes as well as when I move the the target in the plane even though it shows variation in angles. Kindly suggest me some way to get accurate result.
This is the image of target:
while True:
ret, img = cap.read()
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, corners=cv2.findCirclesGrid(gray,(5,5),None)
if ret== True:
corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
# Find the rotation and translation vectors.
_,_, rvecs, tvecs= cv2.solvePnPRansac(objp, corners2, mtx, dist)
dst,_ = cv2.Rodrigues(rvecs)
r = dst
x = math.atan2(r[2][1],r[2][2])
y = math.atan2(-r[2][0],math.sqrt((r[0][0])*r[0][0])+(r[1][0]*r[1][0]))
z= math.atan2(r[1][0],r[0][0])
print("x = ",math.degrees(x))
print("y = "math.degrees(y))
print("z = "math.degrees(z))
Desired Output: x = 5, y = 0, z = 0
Current Output: x = 6.1 , y = 4.7, z = -2.0