imtransform different from manually rotated-transformed image
1 view (last 30 days)
Show older comments
Hi,
In the forum I couldn't find any help to my problem.
With the imtransform function I'm trying to transform a given image taken with a foto-camera in position (0,0,0,0,0,0) = (rotation about_x, rotation about y, rotation about z, x,y,z,).
When I move the foto-camera, e.g. to the position (0,10,0,0,0,0) [so the camera is rotated 10 degrees about the y axis] and then take an image, this image should be the same, as if I apply imtransform to the image taken at position 0. I have access to a device which can very precisely run this rotation with a real foto-camera attached to it.
The two images (transformed image with Matlab and image taken with rotated camera) also look similar, but not exactly the same. It seems that Matlab somehow squeezes the image in its height.
I assume the mistake lies in my use of imtransform, but I have no clue where the mistake exactly is hyding, as I used 'XYScale', [1 1].
Any help is higly appreciated.
Thanks Freddi
clear all
close all
%
% Specify camera intrinsics:
f = 50; %focal length in mm =50
cx=36; %sensor width
cy=24; % sensor height
img = imread('/ground_truth_image.png');
[rows, cols, dim] = size(img);
% Defining the number of pixels on the sensor. Has to be equal to the size
% of the image
cxp = 1*cols;
cyp = 1*rows;
%
% %Precomputing parameters
pixval=cx/cxp*1e-3; % size of a pixel on the camera sensor in meter
fpix = round(f*1e-3 /pixval); % focal length in pixals
a = cols/rows; %aspect ratio
%
%Definining the rotation center of the camera (cxp_coor, cyp_coord,
%czp_coord)
cxp_coord = 445;
cyp_coord = 326;
czp_coord = 632; %the distance camera - flat scene is 632mm
%
% intrinsics camera Matrix
K = [fpix 0 cxp_coord; 0 a*fpix cyp_coord; 0 0 1];
%
%normal of the camera plane
L = [ 0 0 -1 czp_coord*1e-3/pixval];
%
% intrinsics camera matrix in homogenous coordinates
Kext = [K, [0;0;0]];
Kext = [Kext;0, 0, 0, 1];
%
% ( r_x, r_y, r_z, x,y,z )=(rotation, rot, rot, left,forward, up )
m = (-1)*[ 0 0 0 0 0 0; 0 10 0 0 0 0]; % (-1) probably because of inverse Transformation in Matlab
cam_mtr = zeros(size(m,1)*3, 4);
%
%compute the two extrinsic camera matrix in the for loop
for i = 1 : size(m,1)
a2 = m(i,2);
R = [cosd(a2) -sind(a2) 0; sind(a2) cosd(a2) 0; 0 0 1];
%
t =[0; 0; 0]; %Define translation of the moved camera
%
% extrinsics Matrix of the new position of the moved
camera, in homogenous coordinates
Rext = [R,t];
Rext = [Rext; 0 0 0 1];
P = Kext*Rext; %camera matrix in homogenous coordinates
cam_mtr(i*4-3:4*i,1:4) = P;
end
%
% Compute the Homography matrix [I'm pretty sure this step is right]
P_0 = cam_mtr(1:4,1:4);
P_0(4,:) = L;
P_1 = cam_mtr(5:8,1:4);
H = P_1 /(P_0);
H(4,:) = [];
H(:,4)=[];
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
t = maketform('projective',H');
img_rotated = imtransform(img, t, 'XYScale', [1 1], 'XData', [1 cols], 'YData', [1 rows]);
figure('NumberTitle','off','Name','MatlabTransform_5'), title('matlabtransform'), imagesc(img2)
0 Comments
Answers (0)
See Also
Categories
Find more on Camera Calibration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!