PCATutorial.cpp
Go to the documentation of this file.
1 //===========================================================================
2 /*!
3  *
4  *
5  * \brief Principal Component Analysis Tutorial Sample Code
6  *
7  * This file is part of the "Principal Component Analysis" tutorial.
8  * The tutorial requires that you download the Cambridge Face Database
9  * from http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html
10  * and adjust the facedirectory path to the directory containing the faces
11  * in PGM format.
12  *
13  * You need the libraries boost_serialization, boost_system, and
14  * boost_filesystem for this example.
15  *
16  *
17  *
18  * \author C. Igel
19  * \date 2011
20  *
21  *
22  * \par Copyright 1995-2015 Shark Development Team
23  *
24  * <BR><HR>
25  * This file is part of Shark.
26  * <http://image.diku.dk/shark/>
27  *
28  * Shark is free software: you can redistribute it and/or modify
29  * it under the terms of the GNU Lesser General Public License as published
30  * by the Free Software Foundation, either version 3 of the License, or
31  * (at your option) any later version.
32  *
33  * Shark is distributed in the hope that it will be useful,
34  * but WITHOUT ANY WARRANTY; without even the implied warranty of
35  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36  * GNU Lesser General Public License for more details.
37  *
38  * You should have received a copy of the GNU Lesser General Public License
39  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
40  *
41  */
42 //===========================================================================
43 
45 #include <shark/Data/Pgm.h>
46 
47 #include <fstream>
48 #include <boost/format.hpp>
49 
50 using namespace std;
51 using namespace shark;
52 
53 
54 int main(){
55  // read image data
56  const char *facedirectory = "Cambridge_FaceDB"; //< set this to the directory containing the face database
58  Data<ImageInformation> imagesInfo;
59  cout << "Read images ... " << flush;
60  try {
61  importPGMSet(facedirectory, images, imagesInfo);
62  } catch(...) {
63  cerr << "[PCATutorial] could not open face database directory\n\nThis file is part of the \"Principal Component Analysis\" tutorial.\nThe tutorial requires that you download the Cambridge Face Database\nfrom http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html\nand adjust the facedirectory path in the source code to the directory\ncontaining the faces in PGM format." << endl;
64  return 1;
65  }
66  cout << "done." << endl;
67 
68  unsigned l = images.numberOfElements(); // number of samples
69  unsigned x = imagesInfo.element(0).x; // width of images
70  unsigned y = imagesInfo.element(0).y; // height of images
71 
72  cout << "Eigenvalue decomposition ... " << flush;
73  PCA pca(images);
74  cout << "done." << endl;
75 
76  cout << "Writing mean face and eigenvalues... " << flush;
77  ofstream ofs("facesEigenvalues.csv");
78  for(unsigned i=0; i<l; i++)
79  ofs << pca.eigenvalue(i) << endl;
80  exportPGM("facesMean.pgm", pca.mean(), x, y);
81  cout << "done. " << endl;
82 
83  cout << "Encoding ... " << flush;
84  unsigned m = 299;
85  LinearModel<> enc;
86  pca.encoder(enc, m);
87  Data<RealVector> encodedImages = enc(images);
88  cout << "done. " << endl;
89 
90  unsigned sampleImage = 0;
91  cout << "Reconstructing face " << sampleImage << " ... " << flush;
92  boost::format fmterTrue("face%d.pgm");
93  exportPGM((fmterTrue % sampleImage).str().c_str(), images.element(sampleImage), x, y);
94  LinearModel<> dec;
95  pca.decoder(dec, m);
96  boost::format fmterRec("facesReconstruction%d-%d.pgm");
97  exportPGM((fmterRec % sampleImage % m).str().c_str(), dec(encodedImages.element(sampleImage)), x, y);
98  cout << "done." << endl;
99 }