OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Wed, 08 Aug 2018 03:04:43 -0500Adjust image rotation to camera rotationhttp://answers.opencv.org/question/197058/adjust-image-rotation-to-camera-rotation/Hi,
I have written a program which blends a space shuttle icon into an image which is projected. My program, additionally, uses feature matching, homography and perspective transform so that the icon can be moved as the camera moves. In other words, move the camera over the projected image to move the icon around.
I am done with everything describe above. One thing I want to accomplish is to rotate the icon image with the camera. That is, do not always draw the icon as it is, rotate it to left and right when the camera rotates so.
Here is my code which blends the icon into the image:
# blend the shuttleIcon into the image
processedImage = cv2.cvtColor(processedImage, cv2.COLOR_BGR2BGRA)
w, h, c = shuttleIcon.shape
for i in range(0, w):
for j in range(0, h):
if shuttleIcon[i, j][3] != 0:
processedImage[trackedCenterPoint[1] + i, trackedCenterPoint[0] + j] = shuttleIcon[i, j]
processedImage = cv2.cvtColor(processedImage, cv2.COLOR_BGRA2BGR)
How can I incorporate the camera rotation into this? Any thoughts?
NOTE: I do not have the calibration matrix.Tue, 07 Aug 2018 21:14:28 -0500http://answers.opencv.org/question/197058/adjust-image-rotation-to-camera-rotation/Answer by Raki for <p>Hi,</p>
<p>I have written a program which blends a space shuttle icon into an image which is projected. My program, additionally, uses feature matching, homography and perspective transform so that the icon can be moved as the camera moves. In other words, move the camera over the projected image to move the icon around.</p>
<p>I am done with everything describe above. One thing I want to accomplish is to rotate the icon image with the camera. That is, do not always draw the icon as it is, rotate it to left and right when the camera rotates so.</p>
<p>Here is my code which blends the icon into the image:</p>
<pre><code># blend the shuttleIcon into the image
processedImage = cv2.cvtColor(processedImage, cv2.COLOR_BGR2BGRA)
w, h, c = shuttleIcon.shape
for i in range(0, w):
for j in range(0, h):
if shuttleIcon[i, j][3] != 0:
processedImage[trackedCenterPoint[1] + i, trackedCenterPoint[0] + j] = shuttleIcon[i, j]
processedImage = cv2.cvtColor(processedImage, cv2.COLOR_BGRA2BGR)
</code></pre>
<p>How can I incorporate the camera rotation into this? Any thoughts?</p>
<p>NOTE: I do not have the calibration matrix.</p>
http://answers.opencv.org/question/197058/adjust-image-rotation-to-camera-rotation/?answer=197065#post-id-197065OK. I got it. The following does the job:
'''Takes 2 vectors and returns the rotation matrix between these 2 vectors'''
def get_camera_rotation(homographyMatrix):
# Points in the camera frame
camera_pts = np.float32([[round(CAM_WIDTH / 2), round(CAM_HEIGHT / 2)],
[round(10 + CAM_WIDTH / 2), round(10 + CAM_HEIGHT / 2)]]).reshape(-1, 1, 2)
# Find these points in the projector image
proj_pts = cv2.perspectiveTransform(camera_pts, homographyMatrix)
# Find the vectors between the sets of points
camera_vector = (camera_pts[0][0][0] - camera_pts[1][0][0], camera_pts[0][0][1] - camera_pts[1][0][1])
proj_vector = (proj_pts[0][0][0] - proj_pts[1][0][0], proj_pts[0][0][1] - proj_pts[1][0][1])
# change the vectors to unit vectors
camera_vector = camera_vector / np.absolute(np.linalg.norm(camera_vector))
proj_vector = proj_vector / np.absolute(np.linalg.norm(proj_vector))
# calculate the angle between the 2 vectors
# Change the sign of the angle if the rocket is turning the opposite way to desired
#sine of the angle
sinAngle = camera_vector[0] * proj_vector[1] - camera_vector[1] * proj_vector[0]
#angle between the vectors
angle = np.arcsin(np.clip(sinAngle, -1.0, 1.0))
#print("The angle between the camera and the projector is:")
#print(angle)
# calculate the 2D rotation matrix from this angle
rotation_matrix = np.matrix([[np.cos(angle), -1 * np.sin(angle)], [np.sin(angle), np.cos(angle)]])
return angle
to use:
# rotate the shuttle as the camera does
# first though, get a copy
toBeRotatedShuttle = shuttleIcon.copy()
rows, cols, w = toBeRotatedShuttle.shape
angle = get_camera_rotation(smoothenedMatrix)
angleInDegrees = round(math.degrees(math.asin(angle)),2) # convert radian to degrees
rotationMatrix = cv2.getRotationMatrix2D((cols / 2, rows / 2), angleInDegrees, 1)
shuttleIcon = cv2.warpAffine(toBeRotatedShuttle, rotationMatrix, (cols, rows), cv2.INTER_AREA)
Wed, 08 Aug 2018 03:04:43 -0500http://answers.opencv.org/question/197058/adjust-image-rotation-to-camera-rotation/?answer=197065#post-id-197065