[go: up one dir, main page]

Menu

[r1]: / lv_aamshape2d.cpp  Maximize  Restore  History

Download this file

824 lines (584 with data), 19.3 kB

  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
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
/****************************************************************************
*
* Copyright (c) 2006 by JIA Pei, all rights reserved.
* Copyright (c) 2006 by Vision Open: http://www.visionopen.com/
*
* Author: JIA Pei
* Contact: jp4work@gmail.com
* URL: http://www.visionopen.com/members/jiapei.php
* The author administrates Vision Open -- http://www.visionopen.com
*
* 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.
*
* 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
*
* This software is partly based on the following open source:
*
* - Boost
* - OpenCV
*
* This software is using IMM Face Database, which can be downloaded from
* http://www2.imm.dtu.dk/~aam/.
*
* M. B. Stegmann, B. K. Ersb{\o}ll, and R. Larsen. FAME - a flexible appearance
* modelling environment. IEEE Trans. on Medical Imaging, 22(10):1319-1331, 2003
*
****************************************************************************/
// $Id: lv_aamshape2d.cpp,v 1.1.1.1 2006-09-03 17:49:04 JIA Pei Exp $
#include <string>
#include <iostream>
#include <fstream>
#include "lv_aamshape2d.h"
using namespace std;
/**
@author JIA Pei
@version 2006-04-13
@brief Copy constructor.
@param s The input shape.
@return Nothing.
*/
lv_aamshape2d::lv_aamshape2d( const lv_aamshape2d &s )
{
this->CopyData (s);
}
lv_aamshape2d& lv_aamshape2d::operator=(const lv_aamshape2d &s)
{
this->CopyData (s);
return *this;
}
lv_aamshape2d& lv_aamshape2d::operator=(double value)
{
if (this->m_vPoint.size())
{
for (unsigned int i = 0; i < this->m_vPoint.size(); i++)
{
this->m_vPoint[i].m_CVxy.x = value;
this->m_vPoint[i].m_CVxy.y = value;
}
}
return *this;
}
lv_aamshape2d lv_aamshape2d::operator+(const lv_aamshape2d &s)
{
lv_aamshape2d res(*this);
for (unsigned int i = 0; i < res.m_vPoint.size(); i++)
{
res.m_vPoint[i].m_CVxy.x += s.m_vPoint[i].m_CVxy.x;
res.m_vPoint[i].m_CVxy.y += s.m_vPoint[i].m_CVxy.y;
}
return res;
}
lv_aamshape2d& lv_aamshape2d::operator+=(const lv_aamshape2d &s)
{
for (unsigned int i = 0; i < this->m_vPoint.size(); i++)
{
this->m_vPoint[i].m_CVxy.x += s.m_vPoint[i].m_CVxy.x;
this->m_vPoint[i].m_CVxy.y += s.m_vPoint[i].m_CVxy.y;
}
return *this;
}
lv_aamshape2d lv_aamshape2d::operator-(const lv_aamshape2d &s)
{
lv_aamshape2d res(*this);
for (unsigned int i = 0; i < res.m_vPoint.size(); i++)
{
res.m_vPoint[i].m_CVxy.x -= s.m_vPoint[i].m_CVxy.x;
res.m_vPoint[i].m_CVxy.y -= s.m_vPoint[i].m_CVxy.y;
}
return res;
}
lv_aamshape2d& lv_aamshape2d::operator-=(const lv_aamshape2d &s)
{
for (unsigned int i = 0; i < this->m_vPoint.size(); i++)
{
this->m_vPoint[i].m_CVxy.x -= s.m_vPoint[i].m_CVxy.x;
this->m_vPoint[i].m_CVxy.y -= s.m_vPoint[i].m_CVxy.y;
}
return *this;
}
lv_aamshape2d lv_aamshape2d::operator*(double value)
{
lv_aamshape2d res(*this);
for (unsigned int i = 0; i < this->m_vPoint.size(); i++)
{
res.m_vPoint[i].m_CVxy.x *= value;
res.m_vPoint[i].m_CVxy.y *= value;
}
return res;
}
lv_aamshape2d& lv_aamshape2d::operator*=(double value)
{
for (unsigned int i = 0; i < this->m_vPoint.size(); i++)
{
this->m_vPoint[i].m_CVxy.x *= value;
this->m_vPoint[i].m_CVxy.y *= value;
}
return *this;
}
double lv_aamshape2d::operator*(const lv_aamshape2d &s)
{
double result = 0.;
for (unsigned int i = 0; i < this->m_vPoint.size(); i++)
{
result += this->m_vPoint[i].m_CVxy.x * s.m_vPoint[i].m_CVxy.x;
result += this->m_vPoint[i].m_CVxy.y * s.m_vPoint[i].m_CVxy.y;
}
return result;
}
lv_aamshape2d lv_aamshape2d::operator/(double value)
{
assert (value != 0);
lv_aamshape2d res(*this);
for (unsigned int i = 0; i < this->m_vPoint.size(); i++)
{
res.m_vPoint[i].m_CVxy.x /= value;
res.m_vPoint[i].m_CVxy.y /= value;
}
return res;
}
lv_aamshape2d& lv_aamshape2d::operator/=(double value)
{
assert (value != 0);
for (unsigned int i = 0; i < this->m_vPoint.size(); i++)
{
this->m_vPoint[i].m_CVxy.x /= value;
this->m_vPoint[i].m_CVxy.y /= value;
}
return *this;
}
void lv_aamshape2d::CopyData( const lv_aamshape2d &s )
{
// copy vector data (i.e. the point coordinates)
this->m_vPoint.resize( s.m_vPoint.size() );
// copy class data members
this->m_vPoint = s.m_vPoint;
this->m_sHostImage = s.m_sHostImage;
}
void lv_aamshape2d::resize(int length)
{
this->m_vPoint.resize(length);
}
/**
@author JIA Pei
@version 2006-09-01
@brief Reads an .asf into relative coordinates.
@see WriteASF
@param filename Input filename.
@return true on success, false on errors
*/
void lv_aamshape2d::ReadASF( const string &filename )
{
fstream fp;
fp.open(filename.c_str (), ios::in);
string temp;
getline( fp, temp );
getline( fp, temp );
getline( fp, temp );
getline( fp, temp );
getline( fp, temp );
getline( fp, temp );
getline( fp, temp );
getline( fp, temp );
getline( fp, temp );
getline( fp, temp );
int NbOfPoints = atoi(temp.c_str ());
getline( fp, temp );
getline( fp, temp );
getline( fp, temp );
getline( fp, temp );
getline( fp, temp );
getline( fp, temp );
lv_aampoint2d temppoint;
for (unsigned int i = 0; i < NbOfPoints; i++)
{
getline(fp, temp, ' ');
temppoint.m_ipath = atoi(temp.c_str ());
getline(fp, temp, ' ');
temppoint.m_itype = atoi(temp.c_str ());
getline(fp, temp, ' ');
// In DTU IMM , x means rows from left to right
temppoint.m_CVxy.x = atof(temp.c_str ());
getline(fp, temp, ' ');
// In DTU IMM , y means cols from top to bottom
temppoint.m_CVxy.y = atof(temp.c_str ());
getline(fp, temp, ' ');
temppoint.m_iindex = atoi(temp.c_str ());
getline(fp, temp, ' ');
temppoint.m_ifrom = atoi(temp.c_str ());
getline(fp, temp);
temppoint.m_ito = atoi(temp.c_str ());
// In sum, topleft is (0,0), right bottom is (640,480)
this->m_vPoint.push_back (temppoint);
}
getline(fp, temp);
getline(fp, temp);
getline(fp, temp);
getline(fp, temp);
getline(fp, temp);
this->m_sHostImage = temp;
fp.close ();
}
/**
@author JIA Pei
@version 2006-09-02
@brief Calculates the Center Of Gravity of the shape
(actually it's the center of the centroid).
@param x X Center Of Gravity output.
@param y X Center Of Gravity output.
@return Nothing.
*/
void lv_aamshape2d::CenterOfGravity( double &x, double &y )
{
double xSum, ySum;
xSum = ySum = 0.0;
for(int i=0; i<this->m_vPoint.size(); i++)
{
xSum += this->m_vPoint[i].m_CVxy.x;
ySum += this->m_vPoint[i].m_CVxy.y;
}
x = xSum/this->m_vPoint.size();
y = ySum/this->m_vPoint.size();
}
/**
@author JIA Pei
@version 2006-09-02
@brief Translates the shape.
@param x X-translation.
@param y Y-translation.
@return Nothing.
*/
void lv_aamshape2d::Translate( double x, double y )
{
for(unsigned int i = 0; i < this->m_vPoint.size(); i++)
{
this->m_vPoint[i].m_CVxy.x += x;
this->m_vPoint[i].m_CVxy.y += y;
}
}
/**
@author JIA Pei
@version 2006-09-02
@brief Scales the shape.
@param s Scale factor.
@return Nothing.
*/
void lv_aamshape2d::Scale( double s)
{
for(unsigned int i = 0; i < this->m_vPoint.size(); i++)
{
this->m_vPoint[i].m_CVxy.x *= s;
this->m_vPoint[i].m_CVxy.y *= s;
}
}
void lv_aamshape2d::ScaleXY( double sx, double sy)
{
for(unsigned int i = 0; i < this->m_vPoint.size(); i++)
{
this->m_vPoint[i].m_CVxy.x *= sx;
this->m_vPoint[i].m_CVxy.y *= sy;
}
}
/**
@author JIA Pei
@version 2006-09-02
@brief Rotates the shape 'theta' radians.
@param theta Rotation angle in radians.
@return Nothing.
*/
void lv_aamshape2d::Rotate( double theta)
{
double x, y, cx, cy;
// set up rotation matrix
double c00 = cos( theta );
double c01 = -sin( theta );
double c10 = sin( theta );
double c11 = cos( theta );
for(unsigned int i=0;i<this->m_vPoint.size();i++)
{
x = this->m_vPoint[i].m_CVxy.x;
y = this->m_vPoint[i].m_CVxy.y;
this->m_vPoint[i].m_CVxy.x = c00*x+c01*y;
this->m_vPoint[i].m_CVxy.y = c10*x+c11*y;
}
}
/**
@author JIA Pei
@version 2006-09-02
@brief Normalize the shape by translating to its Center Of Gravity
scale by the reciprocal of the 2-norm.
@return The 2-norm of the shape seen as a nbPoint vector after the
translation to origin.
*/
double lv_aamshape2d::Normalize()
{
double x,y;
this->CenterOfGravity( x, y );
this->Translate( -x, -y );
double norm = this->GetShapeNorm();
this->Scale( 1./norm );
return norm;
}
/**
@author JIA Pei
@version 2006-04-13
@brief Returns the 2-norm of this shape centralized.
@return The 2-norm.
*/
double lv_aamshape2d::ShapeSize()
{
lv_aamshape2d tmp(*this);
return tmp.Normalize();
}
/**
@author JIA Pei
@version 2006-09-02
@brief Get the norm of the shape.
@return double.
*/
double lv_aamshape2d::GetShapeNorm()
{
double norm = 0.0;
// Normalize the vector to unit length, using the 2-norm.
for (int i = 0; i < this->m_vPoint.size(); i++)
{
norm += pow(this->m_vPoint[i].m_CVxy.x, 2);
norm += pow(this->m_vPoint[i].m_CVxy.y, 2);
}
norm = sqrt(norm);
return norm;
}
/**
@author JIA Pei
@version 2006-09-02
@brief Returns the rotation between ref and this (in radians).
Get the rotation between two shapes by minimizing the sum
of squared point distances, as described by Goodall (and
Bookstein) using Singular Value Decomposition (SVD).
Note that both shapes must be normalized with respect to
scale and position beforehand. This could be done by using
lv_aamshape2d::Normalize().
@return The estimated angle, theta, between the two shapes.
*/
double lv_aamshape2d::GetRotation( lv_aamshape2d &ref )
{
assert( ref.m_vPoint.size() == this->m_vPoint.size() );
// Note by JIA Pei, make sure the precision must be CV_64FC1 instead of CV_32FC1
CvMat* mRef = cvCreateMat (this->m_vPoint.size(), 2, CV_64FC1);
CvMat* mRefTranspose = cvCreateMat (2, this->m_vPoint.size(), CV_64FC1);
CvMat* mS = cvCreateMat (this->m_vPoint.size(), 2, CV_64FC1);
CvMat* res = cvCreateMat (2, 2, CV_64FC1);
const int X = 0;
const int Y = 1;
// get data as matrices ( nbPoints x 2 columns )
for(int i=0; i < this->m_vPoint.size(); i++)
{
((double*)(mRef->data.ptr + mRef->step * i))[X] = ref.m_vPoint[i].m_CVxy.x;
((double*)(mRef->data.ptr + mRef->step * i))[Y] = ref.m_vPoint[i].m_CVxy.y;
((double*)(mS->data.ptr + mS->step * i))[X] = this->m_vPoint[i].m_CVxy.x;
((double*)(mS->data.ptr + mS->step * i))[Y] = this->m_vPoint[i].m_CVxy.y;
}
// calculate the rotation by minimizing the sum of squared point distances
cvTranspose( mRef, mRefTranspose );
cvMatMul( mRefTranspose, mS, res );
CvMat* S = cvCreateMat (2, 1, CV_64FC1);
CvMat* UT = cvCreateMat (2, 2, CV_64FC1);
CvMat* V = cvCreateMat (2, 2, CV_64FC1);
// SVD Definition in OpenCV
// A = U*W*V^T = U^T*W*V
// cvSVD( CvArr* A, CvArr* W, CvArr* U=NULL, CvArr* V=NULL, int flags=0 );
cvSVD( res, S, UT, V, CV_SVD_U_T );
cvMatMul( V, UT, res );
// res holds now a normal 2x2 rotation matrix
double angle;
double cos_theta = ((double*)(res->data.ptr + res->step * 0))[0];
double sin_theta = ((double*)(res->data.ptr + res->step * 1))[0];
const double epsilon = 1e-12;
// cos_theta should be less than 1.0; but here, cos_theta is calculated by
// matrix computation, in stead of by acos() function, so cos_theta might
// have some special values like cos_theta >= 1.0.
if ( ( fabs(1.0-cos_theta) < epsilon ) || cos_theta >= 1.0 )
{
// cos_theta = 1 => shapes are already aligned
angle = 0;
return angle;
}
if ( fabs(cos_theta) < epsilon )
{
// cos_theta = 0 => 90 degrees rotation
return M_PI/2;
}
// cos_theta should be bigger than -1.0; but here, cos_theta is calculated by
// matrix computation, in stead of by acos() function, so cos_theta might
// have some special values like cos_theta <= -1.0.
if ( ( fabs(1.0+cos_theta) < epsilon ) || cos_theta <= -1.0 )
{
// cos_theta=-1 => 180 degrees rotation
angle = M_PI;
}
else
{
// get the rotation in radians
double a_cos = acos( cos_theta );
double a_sin = asin( sin_theta );
// The following is correct.
// Explained by JIA Pei, please refer to
// http://en.wikipedia.org/wiki/Trigonometric_function
if (a_sin<0)
{
// lower half of the unit circle
angle = -a_cos;
}
else
{
// upper half of the unit circle
angle = a_cos;
}
}
cvReleaseMat(&mRef);
cvReleaseMat(&mRefTranspose);
cvReleaseMat(&mS);
cvReleaseMat(&res);
cvReleaseMat(&S);
cvReleaseMat(&UT);
cvReleaseMat(&V);
return angle;
}
/**
@author JIA Pei
@version 2006-09-02
@brief Aligns this to 'ref' with respect to pose.
@param ref The reference shape.
@param pTheta Optional pointer to return the rotation carried out on this.
@return none.
*/
void lv_aamshape2d::AlignTo( const lv_aamshape2d &ref, double *pTheta )
{
// make a copy of 'ref'
lv_aamshape2d refCpy( ref );
double x,y;
// move this and refCpy to origin
this->CenterOfGravity( x, y );
this->Translate( -x, -y );
refCpy.CenterOfGravity( x, y );
refCpy.Translate( -x, -y ); // remember now, (x, y) are for ref (refCpy) !!
// normalize scale, using the 2-norm
double this_size = this->GetShapeNorm();
double ref_size = refCpy.GetShapeNorm();
this->Scale( ref_size/this_size );
// align rotation between this and refCpy
double theta;
theta = this->GetRotation( refCpy );
this->Rotate( -theta );
if (pTheta)
{
*pTheta = -theta;
}
// translate this to ref origin
this->Translate( x, y );
}
/**
@author JIA Pei
@version 10-06-2006
@brief Returns the transformation that aligns this to 'ref' with respect to pose.
@param ref The reference shape.
@return Nothing.
*/
void lv_aamshape2d::AlignTransformation( const lv_aamshape2d &ref, double &scale, double &theta, CvPoint2D32f &t )
{
lv_aamshape2d refCpy( ref );
lv_aamshape2d thisCpy( *this );
double x,y;
// move thisCpy and refCpy to origin
thisCpy.CenterOfGravity( x, y );
t.x = x;
t.y = y;
thisCpy.Translate( -t.x, -t.y );
refCpy.CenterOfGravity( x, y );
refCpy.Translate( -x, -y );
t.x = x - t.x;
t.y = y - t.y;
// normalize scale, using the 2-norm
double this_size = thisCpy.Normalize();
double ref_size = refCpy.Normalize();
scale = refCpy.Normalize()/thisCpy.Normalize();
thisCpy.Scale( scale );
// align rotation between thisCpy and refCpy
theta = -thisCpy.GetRotation( refCpy );
}
/**
@author JIA Pei
@version 10-15-2006
@brief Displaces the shape around it's center of gravity using a displacement vector.
@see PoseToParam
@param poseVec The input pose vector.
@return Nothing.
*/
void lv_aamshape2d::Displace(double scale, double theta, double tx, double ty)
{
double cx, cy;
// translate to origin
this->CenterOfGravity( cx, cy );
this->Translate( -cx, -cy );
// scale, rotate and translate
this->Scale( scale );
this->Rotate( theta );
// translate the specified amount plus back to c.o.g.
this->Translate( cx+tx, cy+ty );
}
double lv_aamshape2d::MinX()
{
double val, min = 1.7E+308;
for(int i=0;i<this->m_vPoint.size();i++)
{
val = this->m_vPoint[i].m_CVxy.x;
min = val<min ? val : min;
}
return min;
}
double lv_aamshape2d::MinY()
{
double val, min = 1.7E+308;
for(int i=0;i<this->m_vPoint.size();i++)
{
val = this->m_vPoint[i].m_CVxy.y;
min = val<min ? val : min;
}
return min;
}
double lv_aamshape2d::MaxX()
{
double val, max = -1.7E+308;
for(int i=0;i<this->m_vPoint.size();i++)
{
val = this->m_vPoint[i].m_CVxy.x;
max = val>max ? val : max;
}
return max;
}
double lv_aamshape2d::MaxY()
{
double val, max = -1.7E+308;
for(int i=0;i<this->m_vPoint.size();i++)
{
val = this->m_vPoint[i].m_CVxy.y;
max = val>max ? val : max;
}
return max;
}
void lv_aamshape2d::Point2CvMat(CvMat* res) const
{
for (unsigned int i = 0; i < this->m_vPoint.size() ; i++)
{
CV_MAT_ELEM( *res, double, 0 , 2 * i) = this->m_vPoint[i].m_CVxy.x;
CV_MAT_ELEM( *res, double, 0 , 2 * i + 1) = this->m_vPoint[i].m_CVxy.y;
}
}