11#include " PositionSolver.h"
22
33
4- PositionSolver::PositionSolver (int width, int height,
5- float prior_pitch, float prior_yaw, float prior_distance):
6- contour_indices{ 0 ,1 ,8 ,15 ,16 ,27 ,28 ,29 ,30 ,31 ,32 ,33 ,34 ,35 ,36 ,39 ,42 ,45 }
4+ PositionSolver::PositionSolver (int width, int height,
5+ float prior_pitch, float prior_yaw, float prior_distance) :
6+ contour_indices{ 0 ,1 ,8 ,15 ,16 ,27 ,28 ,29 ,30 ,31 ,32 ,33 ,34 ,35 ,36 ,39 ,42 ,45 },
7+ landmark_points_buffer (18 , 1 , CV_32FC2),
8+ rv({ 0 , 0 , 0 }),
9+ tv({ 0 , 0 , 0 })
710{
811 this ->prior_pitch = (1 .1f * (prior_pitch + 90 .f ) / 180 .f ) - (double )2 .5f ;
9- this ->prior_distance = prior_distance * -2 . ;
10- this ->prior_yaw = (1 .84f *(prior_yaw + 90 .f )/180 .f ) - (double )3 .14f ;
12+ this ->prior_distance = prior_distance * -2 .;
13+ this ->prior_yaw = (1 .84f * (prior_yaw + 90 .f ) / 180 .f ) - (double )3 .14f ;
14+
15+ this ->rv [0 ] = this ->prior_pitch ;
16+ this ->rv [1 ] = this ->prior_yaw ;
17+ this ->tv [2 ] = this ->prior_distance ;
1118
1219 // std::cout << "PRIORS CALCULATED: \nPITCH: " <<this->prior_pitch << " YAW: " << this->prior_yaw << " DISTANCE: " << this->prior_distance;
1320
1421 mat3dcontour = (cv::Mat_<double >(18 , 3 ) <<
1522 0.45517698 , -0.30089578 , 0.76442945 ,
1623 0.44899884 , -0.16699584 , 0.765143 ,
17- 0 ., 0.621079 , 0.28729478 ,
24+ 0 ., 0.621079 , 0.28729478 ,
1825 -0.44899884 , -0.16699584 , 0.765143 ,
1926 -0.45517698 , -0.30089578 , 0.76442945 ,
2027 0 ., -0.2933326 , 0.1375821 ,
@@ -32,124 +39,39 @@ PositionSolver::PositionSolver(int width, int height,
3239 -0.31590518 , -0.2983375 , 0.2851074
3340 );
3441
35- /* mat3dface = (cv::Mat_<double>(70, 3) <<
36- 0.4551769692672, 0.300895790030204, -0.764429433974752,
37- 0.448998827123556, 0.166995837790733, -0.765143004071253,
38- 0.437431554952677, 0.022655479179981, -0.739267175112735,
39- 0.415033422928434, -0.088941454648772, -0.747947437846473,
40- 0.389123587370091, -0.232380029794684, -0.704788385327458,
41- 0.334630113904382, -0.361265387599081, -0.615587579236862,
42- 0.263725112132858, -0.460009725616771, -0.491479221041573,
43- 0.16241621322721, -0.558037146073869, -0.339445180872282,
44- 0., -0.621079019321682, -0.287294770748887,
45- -0.16241621322721, -0.558037146073869, -0.339445180872282,
46- -0.263725112132858, -0.460009725616771, -0.491479221041573,
47- -0.334630113904382, -0.361265387599081, -0.615587579236862,
48- -0.389123587370091, -0.232380029794684, -0.704788385327458,
49- -0.415033422928434, -0.088941454648772, -0.747947437846473,
50- -0.437431554952677, 0.022655479179981, -0.739267175112735,
51- -0.448998827123556, 0.166995837790733, -0.765143004071253,
52- -0.4551769692672, 0.300895790030204, -0.764429433974752,
53- 0.385529968662985, 0.402800553948697, -0.310031082540741,
54- 0.322196658344302, 0.464439136821772, -0.250558059367669,
55- 0.25409760441282, 0.46420381416882, -0.208177722146526,
56- 0.186875436782135, 0.44706071961879, -0.145299823706503,
57- 0.120880983543622, 0.423566314072968, -0.110757158774771,
58- -0.120880983543622, 0.423566314072968, -0.110757158774771,
59- -0.186875436782135, 0.44706071961879, -0.145299823706503,
60- -0.25409760441282, 0.46420381416882, -0.208177722146526,
61- -0.322196658344302, 0.464439136821772, -0.250558059367669,
62- -0.385529968662985, 0.402800553948697, -0.310031082540741,
63- 0., 0.293332603215811, -0.137582088779393,
64- 0., 0.194828701837823, -0.069158109325951,
65- 0., 0.103844017393155, -0.009151819844964,
66- 0., 0., 0.,
67- 0.080626352317973, -0.041276068128093, -0.134161035564826,
68- 0.046439347377934, -0.057675223874769, -0.102990627164664,
69- 0., -0.068753126205604, -0.090545348482397,
70- -0.046439347377934, -0.057675223874769, -0.102990627164664,
71- -0.080626352317973, -0.041276068128093, -0.134161035564826,
72- 0.315905195966084, 0.298337502555443, -0.285107407636464,
73- 0.275252345439353, 0.312721904921771, -0.244558251170671,
74- 0.176394511553111, 0.311907184376107, -0.219205360345231,
75- 0.131229723798772, 0.284447361805627, -0.234239149487417,
76- 0.184124948330084, 0.260179585304867, -0.226590776513707,
77- 0.279433549294448, 0.267363071770222, -0.248441437111633,
78- -0.131229723798772, 0.284447361805627, -0.234239149487417,
79- -0.176394511553111, 0.311907184376107, -0.219205360345231,
80- -0.275252345439353, 0.312721904921771, -0.244558251170671,
81- -0.315905195966084, 0.298337502555443, -0.285107407636464,
82- -0.279433549294448, 0.267363071770222, -0.248441437111633,
83- -0.184124948330084, 0.260179585304867, -0.226590776513707,
84- 0.121155252430729, -0.208988660580347, -0.160606287940521,
85- 0.041356305910044, -0.194484199722098, -0.096159882202821,
86- 0., -0.205180167345702, -0.083299217789729,
87- -0.041356305910044, -0.194484199722098, -0.096159882202821,
88- -0.121155252430729, -0.208988660580347, -0.160606287940521,
89- -0.132325402795928, -0.290857984604968, -0.187067868218105,
90- -0.064137791831655, -0.325377847425684, -0.158924039726607,
91- 0., -0.343742581679188, -0.113925986025684,
92- 0.064137791831655, -0.325377847425684, -0.158924039726607,
93- 0.132325402795928, -0.290857984604968, -0.187067868218105,
94- 0.181481567104525, -0.243239316141725, -0.231284988892766,
95- 0.083999507750469, -0.239717753728704, -0.155256465640701,
96- 0., -0.256058040176369, -0.0950619498899,
97- -0.083999507750469, -0.239717753728704, -0.155256465640701,
98- -0.181481567104525, -0.243239316141725, -0.231284988892766,
99- -0.074036069749345, -0.250689938345682, -0.177346470406188,
100- 0., -0.264945854681568, -0.112349967428413,
101- 0.074036069749345, -0.250689938345682, -0.177346470406188,
102- 0.257990002632141, 0.276080012321472, -0.219998998939991,
103- -0.257990002632141, 0.276080012321472, -0.219998998939991,
104- 0.257990002632141, 0.276080012321472, -0.324570998549461,
105- -0.257990002632141, 0.276080012321472, -0.324570998549461
106- );*/
107-
10842 camera_matrix = (cv::Mat_<double >(3 , 3 ) <<
10943 height, 0 , height / 2 ,
11044 0 , height, width / 2 ,
11145 0 , 0 , 1
112- );
46+ );
11347
11448 camera_distortion = (cv::Mat_<double >(4 , 1 ) << 0 , 0 , 0 , 0 );
11549}
11650
11751void PositionSolver::solve_rotation (FaceData* face_data)
11852{
119- std::vector<cv::Point2f> landmarkPoints;
120-
121- for (int i : this ->contour_indices )
53+ int contour_idx = 0 ;
54+ for (int j = 0 ; j < 2 ; j++)
12255 {
123- landmarkPoints. push_back (
124- cv::Point (face_data-> landmark_coords [ 2 * i], face_data-> landmark_coords [ 2 * i + 1 ])
125- ) ;
126- }
56+ for ( int i = 0 ; i < NB_CONTOUR_POINTS; i++)
57+ {
58+ contour_idx = contour_indices[i] ;
59+ landmark_points_buffer. at < float >(i, j) = ( int )face_data-> landmark_coords [ 2 * contour_idx + j];
12760
128- std::vector< double > rv ({prior_pitch, prior_yaw, 0 }), tv ({ 0 , 0 , prior_distance });
129- cv::Mat rvec (rv), tvec (tv);
61+ }
62+ }
13063
131- cv::Mat ip (landmarkPoints );
64+ cv::Mat rvec (rv, true ), tvec (tv, true );
13265
133-
13466 solvePnP (mat3dcontour,
135- ip,
136- this ->camera_matrix ,
137- this ->camera_distortion ,
138- rvec,
139- tvec,
140- true , // extrinsic guess
141- cv::SOLVEPNP_ITERATIVE
142- );
143-
144- /* cv::solvePnPRefineLM(
145- mat3dcontour,
146- ip,
67+ landmark_points_buffer,
14768 this ->camera_matrix ,
14869 this ->camera_distortion ,
14970 rvec,
150- tvec
151- );*/
152-
71+ tvec,
72+ true , // extrinsic guess
73+ cv::SOLVEPNP_ITERATIVE
74+ );
15375
15476 get_euler (rvec, tvec);
15577
@@ -160,28 +82,43 @@ void PositionSolver::solve_rotation(FaceData* face_data)
16082 face_data->translation [i] = tvec.at <double >(i, 0 );
16183 }
16284
163- }
85+ }
86+
87+ void PositionSolver::set_prior_pitch (float new_pitch)
88+ {
89+ this ->prior_pitch = new_pitch;
90+ this ->rv [0 ] = this ->prior_pitch ;
91+ }
92+
93+ void PositionSolver::set_prior_yaw (float new_yaw)
94+ {
95+ this ->prior_yaw = new_yaw;
96+ this ->rv [1 ] = this ->prior_yaw ;
97+ }
98+
99+ void PositionSolver::set_prior_distance (float new_distance)
100+ {
101+ this ->prior_distance = new_distance;
102+ this ->tv [2 ] = this ->prior_distance ;
103+ }
164104
165105
166106void PositionSolver::get_euler (cv::Mat& rvec, cv::Mat& tvec)
167107{
168108 cv::Mat rotM (3 , 3 , CV_64FC1);
169109 cv::Rodrigues (rvec, rotM);
170110
171-
172-
173111 cv::Mat concated (3 , 4 , CV_64FC1);
174112 cv::hconcat (rotM, tvec, concated);
175113
176-
177114 cv::decomposeProjectionMatrix (
178115 concated,
179116 cv::Mat (3 , 3 , CV_64FC1),
180117 cv::Mat (3 , 3 , CV_64FC1),
181118 cv::Mat (4 , 1 , CV_64FC1),
182- cv::Mat ( 3 , 3 , CV_64FC1 ),
183- cv::Mat ( 3 , 3 , CV_64FC1 ),
184- cv::Mat ( 3 , 3 , CV_64FC1 ),
119+ cv::noArray ( ),
120+ cv::noArray ( ),
121+ cv::noArray ( ),
185122 rvec
186123 );
187124
0 commit comments