/
rot_matrix2quaternion.sci
57 lines (52 loc) · 1.88 KB
/
rot_matrix2quaternion.sci
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
function q = rot_matrix2quaternion(R)
// Generates quaternion from rotation matrix
//
// REFERENCE
// Oxford's Visual geometry group multiview matlab toolbox
//
// $Revision: 1.1 $ $Date: 2010-10-09 23:39:24 $
q = [ (1 + R(1,1) + R(2,2) + R(3,3))
(1 + R(1,1) - R(2,2) - R(3,3))
(1 - R(1,1) + R(2,2) - R(3,3))
(1 - R(1,1) - R(2,2) + R(3,3)) ];
// Pivot to avoid division by small numbers
[b I] = max(abs(q));
q(I) = sqrt(q(I)) / 2 ;
if I == 1
q(2) = (R(3,2) - R(2,3)) / (4*q(I));
q(3) = (R(1,3) - R(3,1)) / (4*q(I));
q(4) = (R(2,1) - R(1,2)) / (4*q(I));
elseif I==2
q(1) = (R(3,2) - R(2,3)) / (4*q(I));
q(3) = (R(2,1) + R(1,2)) / (4*q(I));
q(4) = (R(1,3) + R(3,1)) / (4*q(I));
elseif I==3
q(1) = (R(1,3) - R(3,1)) / (4*q(I));
q(2) = (R(2,1) + R(1,2)) / (4*q(I));
q(4) = (R(3,2) + R(2,3)) / (4*q(I));
elseif I==4
q(1) = (R(2,1) - R(1,2)) / (4*q(I));
q(2) = (R(1,3) + R(3,1)) / (4*q(I));
q(3) = (R(3,2) + R(2,3)) / (4*q(I));
end
endfunction
//
// -------------------------------------------------------------------------
// SIP - Scilab Image Processing toolbox
// Copyright (C) 2002-2010 Ricardo Fabbri
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// -------------------------------------------------------------------------
//