How to rotate a 3D plane? - matlab

I have a 3d plane (made up of number of points) which is rotated at weird angle. I want to make it flat i.e lie on xy-plane. I have plane equation but I think my calculated angles are not correct or might be using wrong rotation matrix. By wrong rotation matrix is that I meant that I am not sure about which axis should I rotate. attached is the picture of my plane:
I tried to calculate by using following formulas:
theta=-acosd((dot(n1,n2))/(norm(n1)*norm(n2)));
Calculate spherical angles: theta and phi;
both methods are giving same angle, I rotated my plane first about z-axis and then about y-axis. The resulted plane is almost flat but it still has some anlge.
I tried both rotation matrix and Rodrigues' rotation matrix. It would be really helpful if someone could suggest how to rotate this plane to make it flat.

When a plane is not parallel to the xy-plane, then it's normal vector will not be parallel to the z-axis. So the cross product of the normal vector and the z-axis (unit) vector will be non-zero. This vector is in the plane and parallel to the xy-plane. Take it as rotation axis. The rotation angle to make the plane parallel to the xy-plane is the same as the angle between the normal vector and the z-axis.

Related

Rotate an image around x or y axis in Matlab

If a camera is looking at say 6 evenly spaced dots in the real world it would look like the image below if the camera is looking at the image straight on with no rotation in the x, y or z axis
The z-axis is perpendicular to the image sensor so rotation around the z-axis is simple, it's just a tilt of the image. If I were to rotate the camera (or objects being looked at) around the x axis (if the x-axis is up down) the rows and columns will no longer be parallel and would project off to a vanishing point, like this.
What I would like to do is take a 2 dimensional image of say, dots, and be able to apply different rotations around the x,y and z axes independently. I've experimented with reading my image in Matlab and multiplying by a rotation matrix, or even a full camera matrix but I can't figure out how to take a 2D image, simulate rotating it around the x axis and then saving that back to an image. So that my original grid of dots would look like the bottom image with lines going off to a vanishing point. I've seen some examples using imwarp but I didn't see how I can set the angle of rotation. I'm working on camera calibration so I really want to be able to specify an angle of rotation around each axis.
Thanks for any help.

Find 3D coordinate with respect to the camera using 2D image coordinates

I need to calculate the X,Y coordinates in the world with respect to the camera using u,v coordinates in the 2D image. I am using an S7 edge camera to send a 720x480 video feed to MATLAB.
What I know: Z i.e the depth of the object from the camera, size of the camera pixels (1.4um), focal length (4.2mm)
Let's say the image point is at (u,v) = (400,400).
My approach is as follows:
Subtract the pixel value of center point (240,360) from the u,v pixel coordinates of the point in the image. This should give us the pixel coordinates with respect to the camera's optical axis (z axis). The origin is now at the center of the image. So new coordinates are: (160, -40)
Multiply the new u,v pixel values with pixel size to obtain the distance of the point from the origin in physical units. Let's call it (x,y). We get (x,y) = (0.224,-0.056) in mm units.
Use the formula X = xZ/f & Y = yZ/f to calculate X,Y coordinates in the real world with respect to the camera's optical axis.
Is my approach correct?
Your approach is going in the right way, but it would be easier if you use a more standardize approach. What we usually do is use Pinhole Camera Model to give you a transformation between the world coordinates [X, Y, Z] to the pixel [x, y]. Take a look in this guide which describes step-by-step the process of building your transformation.
Basically you have to define you Internal Camera Matrix to do the transformation:
fx and fy are your focal length scaled to use as pixel distance. You can calculate this with your FOV and the total pixel in each direction. Take a look here and here for more info.
u0 and v0 are the piercing point. Since our pixels are not centered in the [0, 0] these parameters represents a translation to the center of the image. (intersection of the optical axis with the image plane provided in pixel coordinates).
If you need, you can also add a the skew factor a, which you can use to correct shear effects of your camera. Then, the Internal Camera Matrix will be:
Since your depth is fixed, just fix your Z and continue the transformation without a problem.
Remember: If you want the inverse transformation (camera to world) just invert you Camera Matrix and be happy!
Matlab has also a very good guide for this transformation. Take a look.

Transform an object so its normal vector align z axis

I want to transform 3d mesh surface in a way that normal vector is parallel to z axis.
I try the following steps:
1- find the rotation angle using
atan2d(norm(cross(n,z)),dot(n,z));
where n is the normal vector and z is the z-axis
2- find axis of rotation using cross product of n and z
3- create rotation matrix
however the resulted transformation is wrong.
Thanks for your help

Rotate nxn matrix around x-axis by an angle theta in Matlab

I have nxn matrix in 2D space; I would like to rotate the matrix around the x-axis using matlab. Where the x-axis pass through the center of the matrix (pass through the point [n/2 n/2].
I found the Matlab function B = rot90(A) which rotate the matrix A by 90 degree. But I’m looking for a method that rotate matrix A by any given angle (e.g. 30, 45, 170 degree) around the x-axis.
You can as well try imrotate(). This function is from the Image Processing Toolbox, but since its main input is a matrix (either real or logical) it'll work also for non picture-related matrices (I've tried with a magic matrix).
The syntax is:
B=imrotate(A,theta);
where A is you matrix, B is the rotated version of A and theta is the rotation in degrees. The rotation is performed in counterclockwise direction around its center point; to rotate the matrix clockwise, specify a negative value for theta.

I need to fit a best circle to the 3D data in matlab

Basically, I have a many irregular circle on the ground in the form of x,y,z coordinates (of 200*3 matrix). but I want to fix a best circle in to the data of x,y,z coordinates (of 200*3 matrix).
Any help will be greatly appreciated.
I would try using the RANSAC algorithm which finds the parameters of your model (in your case a circle) given noisy data. The algorithm is quite easy to understand and robust against outliers.
The wikipedia article has a Matlab example for fitting a line but it shouldn't be too hard to adapt it to fit a circle.
These slides give a good introduction to the RANSAC algorithm (starting from page 42). They even show examples for fitting a circle.
Though this answer is late, I hope this helps others
To fit a circle to 3d points
Find the centroid of the 3d points (nx3 matrix)
Subtract the centroid from the 3D points.
Using RANSAC, fit a plane to the 3D points. You can refer here for the function to fit plane using RANSAC
Apply SVD to the 3d points (nx3 matrix) and get the v matrix
Generate the axes along the RANSAC plane using the axes from SVD. For example, if the plane norm is along the z-direction, then cross product between the 1st column of v matrix and the plane norm will generate the vector along the y-direction, then the cross product between the generated y-vector and plane norm will generate a vector along the x-direction. Using the generated vectors, form a Rotation matrix [x_vector y_vector z_vector]
Multiply the Rotation matrix with the centroid subtracted 3d points so that the points will be parallel to the XY plane.
Project the points to XY plane by simply removing the Z-axes from the 3d points
fit a circle using Least squares circle fit
Rotate the center of the circle using the inverse of the rotation matrix obtained from step 5
Translate back the center to the original location using the centroid
The circle in 3D will have the center, the radius will be the same as the 2D circle we obtained from step 8, the circle plane will be the RANSAC plane we obtained from step 3