1

I'm using OpenCV's camera calibration function, I have 34 images of the checkerboard on a turntable, taken from a static camera. The camera position from the initial tvec is approximately correct, however the rvec is totally wrong. I ploted the orientation in matlab to indicate the problem.

orientation plot

The formula I used should be correct like,this.

My question is why these orientations are wrong ? is this normal for the calibration algorithm? Matlab uses Z as the up axis, could this be my visualization code problem ?

Thanks.

Update:

I put my plot code here, I suspect it has the problem.

function drawCameraPoses(rvec, tvec)

if (size(tvec,2) ~= 3 || size(tvec,1) <= 0)
    disp('tvec must contain 3 columns and greater than 0 row') ;
    exit ;
end

if (size(rvec) ~= size(tvec))
    disp('rvec must be at the same size of the tvec.') ;
end


%plot camera positions
figure ;
%watch out xyz order
plot3(tvec(:,1), tvec(:,3), tvec(:,2),'r+') ;
hold on ;
grid on ;

plotrange = [max(tvec);min(tvec)] ;
disp(plotrange)
xyrange = [max([plotrange(1,1),plotrange(1,3)]) ; min([plotrange(2,1),plotrange(2,3)])];
disp(xyrange)
plotscaleFactor = 1.5 ;

xlim([xyrange(2) - abs(xyrange(2))*(plotscaleFactor - 1.0), xyrange(1) + abs(xyrange(1))*(plotscaleFactor - 1.0)]) ;
ylim([xyrange(2) - abs(xyrange(2))*(plotscaleFactor - 1.0), xyrange(1) + abs(xyrange(1))*(plotscaleFactor - 1.0)]) ;
zlim([plotrange(2,2) - abs(plotrange(2,2)) * (plotscaleFactor - 1.0), plotrange(1,2) + abs(plotrange(1,2)) * (plotscaleFactor - 1.0)]) ;

%plot camera orientation

for i = 1 : size(rvec,1)
    [x,y,z] = getEndPoint(tvec(i,:),rvec(i,:)) ;
    line([tvec(i,1), x],[tvec(i,3),z],[tvec(i,2),y],'Color','r');
end


end

function [x,y,z] = getEndPoint(t,r)
length = 50 ;
unitR = r / norm(r) ;
unitR = unitR * length ;
x = t(1) + unitR(1) ;
y = t(2) + unitR(2) ;
z = t(3) + unitR(3) ;
end
Community
  • 1
  • 1
tomriddle_1234
  • 3,145
  • 6
  • 41
  • 71
  • What are you plotting> what is x,y,z inn that 3D plot? – Ander Biguri Apr 10 '15 at 11:14
  • If the rvec from OpenCV is [r0,r1,r2], position tvec is [t0,t1,t2] , in the 3D plot, position is [t0,t2,t1], then I normalized the rvec into [r0',r1',r2'], so the orientation vector would be [r0', r2', r1'], I just ploted line segments starts from [t0,t2,t1], end with [t0+r0'*length, t2+r2' * length, t1+r1'*length] – tomriddle_1234 Apr 11 '15 at 03:35

2 Answers2

1

Actually, the output rvec and tvec should form the transformation matrix rather than simply coordinate assignment. So inspired from the calibration toolbox.

The code to display the camera poses would be,

function drawCameraPoses(rvec, tvec, flag)

if (size(tvec,2) ~= 3 || size(tvec,1) <= 0)
    disp('tvec must contain 3 columns and greater than 0 row') ;
    exit ;
end

if (size(rvec) ~= size(tvec))
    disp('rvec must be at the same size of the tvec.') ;
end

if ~flag
    %raw rvec tvec
    for i = 1:size(tvec,1)
        rotM = rodrigues(rvec(i,:)) ;
        tvec(i,:) = -rotM'* tvec(i,:)' ;
        rvec(i,:) = rodrigues(rotM') ;
    end
end

%plot camera positions
figure ;
%watch out xyz order
plot3(tvec(:,1), tvec(:,2), tvec(:,3),'r+') ;
hold on ;
grid on ;

xlim([-1 1]) ;
ylim([-1 1]) ;
zlim([-1 1]) ;

%plot camera orientation
baseSymbol = 0.2 * [0 1 0 0 0 0;0 0 0 1 0 0;0 0 0 0 0 1] ;
for i = 1 : size(rvec,1)
   %rotM already transposed
   rotM = rodrigues(rvec(i,:)) ;
   baseK = rotM * baseSymbol + tvec(i,:)' * ones(1,6) ;
   plot3(baseK(1,:),baseK(2,:), baseK(3,:),'-b') ;
end


end

enter image description here

tomriddle_1234
  • 3,145
  • 6
  • 41
  • 71
0

For visualizing the camera orientation try the plotCamera function in the Computer Vision System Toolbox.

While you are at it, try the Camera Calibrator app.

Dima
  • 38,860
  • 14
  • 75
  • 115