Calculating a rotation matrix using rotate in Matlab - matlab

I'm using Matlab 2013a on Windows 7 Pro 64 bit.
In 2010 (using a different version of Matlab), I wrote the following code to calculate a 3x3 rotation matrix
C=rotate (omega, i, w);
R=C*Ro;
where omega, i, and w are rotation angles in radians. On my current system, giving the example input of omega = i = w = 0, C will be a 3x3 identity matrix.
If I copy the code into another directory that I wrote in the past few months, the same code will result in the following error.
Error using rotate
Too many output arguments.
So my question to SO is why the same lines of code on the same computer with the same version of Matlab will work in one directory but not in another directory?

If you had read the help for rotate, you would have seen that this one does not calculate the rotation matrix, but rotates a matlab graph. Some toolboxes eg Phased Array System Toolbox have some functions rotx, roty,... to calculate the rotational matrix.
However, functions for calculating the rotation matrix is not that hard to construct. Looking at http://en.wikipedia.org/wiki/Rotation_matrix the rotational matrices for every axis is given. The general rotation matrix is then a product of the individual rotation matrices. This should be piece of cake.

Related

Camera Calibration - How To Find The Projection Matrix

There are many questions that explain how to find the projection matrix but they don't apply to my situation. The Matlab function cameraMatrix(cameraParams,rotMatrix,tranVector) can easily find the projection matrix.
I obtained my cameraParams variable by using the camera calibrator app in Matlab by providing checkerboard images in the input. The problem is that in my cameraParams variable, there are 10 rotation matrices and 10 translation vectors for some reason. The app always returns 10 rotation and translation vectors no matter what. The function cameraMatrix() can only accept a single rotation matrix and translation vector as inputs.
Why are there multiple rotation matrices and translation vectors and how do I find the projection matrix?

How to find ridges by using Hessian matrix

I want to find ridges for a given image. (Ridges not edges!)
An example is like the image below
I think Hessian matrix will work intuitively. So I hard coded Hessian matrix kernel by starting from a 2D-Gaussian equation as the links below described.
How to build 2D hessian matrix kernel
I use surf to visualize the 3 second order derivative kernels (D_xx,D_yy and D_xy) I created and they look all correct.
I then applied these kernels and did 2D convolution with my image.
I am not sure what to do next, should I need to represent eigen values and vectors by using D_xx,D_yy and D_xy? How can we pull out ridge lines from the image by using the eigen analysis of 2-by-2 matrix for each pixel?
Any idea, formula or even code will be much helpful.
Attached is the code to generate 2D Hessian matrix
[x y]=meshgrid(round(-N/2):round(N/2), round(-N/2):round(N/2));
common = x.^2+y.^2;
Lxx = ((-1+x.^2/(sigma^2)).*exp(-common/(2*sigma^2))) / (2*pi*sigma^4);
Lxx = Lxx./ sum(Lxx(:));
Lyy = ((-1+y.^2/(sigma^2)).*exp(-common/(2*sigma^2))) / (2*pi*sigma^4);
Lyy = Lyy./ sum(Lyy(:));
Lxy = ((x.*y)/(2*pi*sigma^6)).*exp(-common/(2*sigma^2));
Lxy = Lxy./ sum(Lxy(:));

Epipolar lines with known rotation and translation

I want to calculate the epipolar lines for the interest points between two images. I am working on a fountain dataset, so I have the rotation and translation matrix, as well as the camera matrix. I currently use Matlab in order to be fast, but the version I have is quite old(2009).
I am calculating the essential matrix through E=t*R and then the epipolar line with l=E*P, where P is the interest point/set of interest points. Then I get a vector with three lines which I guess are the line parameters of ax+by+c=0. The epipolar line drawn on the right image is totally wrong, far away from the point on the left image. Any idea???
Edit: Used dataset --> fountain benchmark, images 0000 and 0001 http://cvlabwww.epfl.ch/~strecha/multiview/denseMVS.html
Output: Essential matrix e.g. for point P1=[433.36;861.15;1]
E =
0.761857065048902 1.969487475012598 40.418915885686594
-0.927781947178923 0.698934833377211 33.173562943087106
-45.044061511303227 -26.573128396975097 1.000000000000000
It has two complex eigenvalues that are conjugated.
Epipolar line:1.0e+004 *
0.206660143270238
0.023299771007641
-4.240274401559348
Finally I found the solution to my problem. I post it here in case somebody else is interested.
To calculate correctly the relative rotation and translation matrices, the Roto-Translation matrix has to be used. This matrix is a 4x4 matrix for every image. The upper left part is the rotation (wrt the world coordinate system), the 4th sub-column is the translation vector (wrt to the world coordinate system) and the last row is [0 0 0 1]. So, if we have 2 such matrices for 2 images, the final roto-translation matrix is Qright-->left=inv(Qright)*Qleft. From this matrix, we extract the relative translation (t) and rotation(R) (4th sub column and upper left matrix respectively). Then, we create the skew symmetric matrix T for translation. The epipolar matrix is E=R*T. But this isn't enough. In order to calculate correctly the epipolar lines, the Fundamental matrix F has to be found. For a given dataset such the one I used, camera matrices K are given so this is easy: F=inv(Kright')*E*inv(Kleft), where (') is the transposed and inv is the inverted matrix. Then, the epipolar lines of the right image are calculated lines=F*P, where P is the point in homogeneous coordinates.
Thank you!
There are lots of documents that can found online that explain epipolar geometry and how to find epipolar lines in stereo images. Here is one. It walks you through different concepts decently. The trick to this topic, I found, is keeping track of the variables which are ultimately the result of matrix transformations and implied (professor shortcuts) algabraic operations.
My recommendation would be looking at page 12 of the link I've provided and applying it your scenario. Without any data to go off of other than the description you've provided, it's impossible to work out the problem.
Good luck.
Note: sorry to hear your Matlab version is old. I know that 2013 has built in functions for this stuff, but I'm not sure if 2009 does because MathWorks requries an account to read older documentation.

Sampling uniformly from many circles on the sphere efficiently in matlab

I have a 3-by-N matrix X whose columns are vectors on the unit sphere (i.e., the Euclidean length of each vector is 1), and I have a 1-by-N vector Theta whose entries are all angles between 0 and pi. For each i, there is a circle on the sphere centered at X(:,i) defined as the set of all points that have the angle Theta(i) with X(:,i). I would like to get one uniform sample from the circle for each i, avoiding for loops because they can be slow in Matlab. I know that in vectorized Matlab code I can easily get one sample each from all circles with angles in Theta if I assume the center of all circles is [0,0,1], and then I know how to get a rotation matrix (using Rodrigues rotation formula) that rotates [0,0,1] to another desired vector x, so for each i, I can just apply this rotation matrix to the sample point I obtained assuming [0,0,1] was the center.
I would like to this for all i without for loops, i.e. using array/matrix/vector notation.
If you're using Rodrigues' rotation formula, you're trying to convert from axis-angle representation to rotation matrices. You're in luck. I happen to have written fast vectorized code to do exactly what I believe you're asking about. You can can find the code here: axang2rotmat.m. Use is pretty straightforward (read the help):
n = 1e3; % Number of axis-angles and rotation matrices
th = pi*rand(1,n); % Random rotation angles between 0 and pi
v = normc(rand(3,n)); % Random rotation vectors, normalized across columns
R = axang2rotmat(v,th); % Generate n rotation matrices, R is 3-by-3-n
Note, the above code is just to demonstrate the use of axang2rotmat and won't give you uniformly sampled rotation matrices (See Miles, Biometrika 1962 for details on why and workaround). I recommend that you calculate random rotation matrices directly, however. You can us another of my functions for that: randrotmat.m.
I also have code to convert back from rotation matrices to axis-angle and check if a particular matrix is a rotation matrix here.

Plotting quaternion in Matlab using "engine.h" from c++

I have an algorithm in C++ that uses Kalman Filter. Somewhere in the code a predict a Quaternion q' and then I update the Quaternion with Kalman Filter q.
I want to plot two graphics in Matlab with the evolution of the predicted quaternion and the corrected(updated) quaternion so I am using "engine.h" library to send quaternion info to Matlab during processing (actually what I send is a 4x1 matrix).
So my question is: What is the best way to plot a quaternion in Matlab so I can visually extract information? Is it maybe better to plot the angles separately?
I think a good option is sending the quaternion as a vector to MATLAB, using C++ MATLAB engine
[qx qy qz qw]
Then, in MATLAB environment you can use a toolbox for translating to Euler Angles, which is a common visual option.
For adding a path of a toolbox in matlab engine:
addpath(genpath('C:\Program Files (x86)\MATLAB\R2010a\toolbox\SpinCalc'));
With spincalc toolbox, converting would be something like this:
Angles=SpinCalc('QtoEA321',Quaternion,0,0);
Well, assuming that the question is "How to visualize in a nice way a 4D space",
I can think of a few options:
Show multiple slices of the space, that is for (x,y,z,t) -> (x,y), (y,z),etc..
Show (x,y) as scatter plot and encode the information of z in color, t in size of dot. For that you can use the scatter command :
SCATTER(X,Y,S,C) displays colored circles at the locations specified
by the vectors X and Y (which must be the same size).
If your question was "How to visualize in a nice way quarternions,
check this out