Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Rotation Matrix in Tensorflow

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.

like image 679
dtracers Avatar asked May 05 '16 05:05

dtracers


People also ask

Is rotation matrix a tensor?

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.

What is 3x3 rotation matrix?

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.

What is a rotation matrix in Opencv?

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.

What are the different types of matrices in TensorFlow?

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.

What happens when you multiply rotation matrices?

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.

How do I expand a matrix in TensorFlow?

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.

What is a rotation matrix in MATLAB?

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.


2 Answers

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)
like image 103
dtracers Avatar answered Sep 29 '22 22:09

dtracers


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)
like image 41
fabrizioM Avatar answered Sep 29 '22 22:09

fabrizioM