MATLAB: Create a projective homography matrix with pitch/roll/yaw

Computer Vision Toolboxgeometric matrix transposegeometrictransformestimatorImage Processing Toolboximwarp

Hi,
I am working with the imwarp() function where I can put in a picture and a 3×3 projective homography matrix. I got this matrix from the GeometricTransformEstimator but now I want to create such a matrix myself by using a pitch/roll/yaw angle.
I tried the matlab function angle2dcm(yaw, pitch, roll) which gives me a rotation matrix but it looks like it is not the same as a homography matrix. Is it even possible to do what I want?
Best Regards

Best Answer

[ Edited: example changed and transpose of matrix in call to projective2d() inserted. See comments.]
It might be worth experimenting with the code below, which uses various of my functions. These are attached.
Possible stumbling blocks are connected with the direction of view of the camera (along the z-axis in the code below), and the default behaviour of imwarp(), which crops the output to the new corner positions of the image. This code compares the result of doing that with the result of keeping the transformed image in the same coordinates as the original image (using imwarp_same()), which allows the effects of the rotation to be seen more clearly.
By fiddling with the values of yaw, pitch, roll and the camera position vector you should be able to get a fix on what is going on and get behaviour that you expect.
example image
img = imread('peppers.png');
% Set angles here. Start with two of them set to zero and third set quite
% small to see the effect.
yaw = 0;
pitch = 0;
roll = -0.2;
% Set camera position here. To understand the effect of angles, move it
% along the z-axis and y-axis so it is above the centre of the top edge of
% the picture. Note that as cammoves is true in call to homography_matrix,
% the z-axis movement needs to be negative.
% The camera is looking along the z-axis.
campos = [size(img,2)/2; 0; -200];
% Construct a rotation matrix. My sign convention is opposite to
% angle2dcm, but I don't have the aerospace toolbox. Computed rmat agrees
% with example in angle2dcm documentation.
yawmat = rotation_matrix([0 0 1], -yaw); % about z
pitchmat = rotation_matrix([0 1 0], -pitch); % about y
rollmat = rotation_matrix([1 0 0], -roll); % about x
rmat = rollmat * pitchmat * yawmat; % zyx order - default for angle2dcm
% construct homography matrix
F = 100; % focal length
hmat = homography_matrix([], rmat, campos, F, true);
% construct projective2d object
tform = projective2d(hmat.');
% display original image and results
subplot(1,3,1);
imshow(img);
% show image warped and cropped
imw_crop = imwarp(img, tform);
subplot(1,3,2);
imshow(imw_crop);
% show image warped in same coordinate system as original
imw_same = imwarp_same(img, tform);
subplot(1,3,3);
imshow(imw_same); shg;
Related Question