I want to create a rotation matrix in tensorflow where all parts of it are tensors.
What I have:
def rotate(tf, points, theta):
rotation_matrix = [[tf.cos(theta), -tf.sin(theta)],
[tf.sin(theta), tf.cos(theta)]]
return tf.matmul(points, rotation_matrix)
But this says that rotation_matrix
is a list of tensors instead of a tensor itself. theta
is also a tensor object that is passed in at run time.
A rotation matrix is a tensor which rotates one Cartesian coordinate system into another. Any rotation can thus be constructed out of these primitive rotations, about coordinate axes.
The most general three-dimensional rotation matrix represents a counterclockwise rotation by an angle θ about a fixed axis that lies along the unit vector n. The rotation matrix operates on vectors to produce rotated vectors, while the coordinate axes are held fixed. This is called an active transformation.
getRotationMatrix2D() function is used to make the transformation matrix M which will be used for rotating a image. Syntax: cv2.getRotationMatrix2D(center, angle, scale) Parameters: center: Center of rotation.
TensorFlow provides shortcuts to creating matrices the most commonly used matrices, an example is the Identity matrix, this is created using tf.eye () Another matrix in which TensorFlow provides a shortcut for creating is the Diagonal matrix.
Multiplication of rotation matrices will result in a rotation matrix. If we take the cross product of two rows of a rotation matrix it will be equal to the third. The dot product of a row with a column of a rotation matrix will be equal to 1. A rotation matrix rotates a vector such that the coordinate axes remain fixed.
The easiest way to expanding the size of a matrix is by using the tf.expand_dims () attribute of TensorFlow. The expand_dims () function takes in a matrix and the number of dimensions at which it should expand as its input, starting it’s index from zero.
A rotation matrix is always a square matrix with real entities. This implies that it will always have an equal number of rows and columns. Moreover, rotation matrices are orthogonal matrices with a determinant equal to 1. Suppose we have a square matrix P. Then P will be a rotation matrix if and only if P T = P -1 and |P| = 1.
An Option I found that works is to use pack but if there is a better way please post an answer:
def rotate(tf, points, theta):
top = tf.pack([tf.cos(theta), -tf.sin(theta)])
bottom = tf.pack([tf.sin(theta), tf.cos(theta)])
rotation_matrix = tf.pack([top, bottom])
return tf.matmul(points, rotation_matrix)
with two operations:
def rotate(tf, points, theta):
rotation_matrix = tf.pack([tf.cos(theta),
-tf.sin(theta),
tf.sin(theta),
tf.cos(theta)])
rotation_matrix = tf.reshape(rotation_matrix, (2,2))
return tf.matmul(points, rotation_matrix)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With