2012-08-23 11:21:49 +08:00
|
|
|
/*
|
|
|
|
* pca.cpp
|
|
|
|
*
|
2012-09-04 21:44:23 +08:00
|
|
|
* Author:
|
2012-08-23 11:21:49 +08:00
|
|
|
* Kevin Hughes <kevinhughes27[at]gmail[dot]com>
|
|
|
|
*
|
|
|
|
* Special Thanks to:
|
|
|
|
* Philipp Wagner <bytefish[at]gmx[dot]de>
|
|
|
|
*
|
2012-09-04 21:44:23 +08:00
|
|
|
* This program demonstrates how to use OpenCV PCA with a
|
2012-08-23 11:21:49 +08:00
|
|
|
* specified amount of variance to retain. The effect
|
|
|
|
* is illustrated further by using a trackbar to
|
|
|
|
* change the value for retained varaince.
|
|
|
|
*
|
|
|
|
* The program takes as input a text file with each line
|
|
|
|
* begin the full path to an image. PCA will be performed
|
|
|
|
* on this list of images. The author recommends using
|
|
|
|
* the first 15 faces of the AT&T face data set:
|
|
|
|
* http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html
|
2012-09-04 21:44:23 +08:00
|
|
|
*
|
2012-08-23 11:21:49 +08:00
|
|
|
* so for example your input text file would look like this:
|
2012-09-04 21:44:23 +08:00
|
|
|
*
|
2012-08-23 11:21:49 +08:00
|
|
|
* <path_to_at&t_faces>/orl_faces/s1/1.pgm
|
|
|
|
* <path_to_at&t_faces>/orl_faces/s2/1.pgm
|
|
|
|
* <path_to_at&t_faces>/orl_faces/s3/1.pgm
|
|
|
|
* <path_to_at&t_faces>/orl_faces/s4/1.pgm
|
|
|
|
* <path_to_at&t_faces>/orl_faces/s5/1.pgm
|
|
|
|
* <path_to_at&t_faces>/orl_faces/s6/1.pgm
|
|
|
|
* <path_to_at&t_faces>/orl_faces/s7/1.pgm
|
|
|
|
* <path_to_at&t_faces>/orl_faces/s8/1.pgm
|
|
|
|
* <path_to_at&t_faces>/orl_faces/s9/1.pgm
|
|
|
|
* <path_to_at&t_faces>/orl_faces/s10/1.pgm
|
|
|
|
* <path_to_at&t_faces>/orl_faces/s11/1.pgm
|
|
|
|
* <path_to_at&t_faces>/orl_faces/s12/1.pgm
|
|
|
|
* <path_to_at&t_faces>/orl_faces/s13/1.pgm
|
|
|
|
* <path_to_at&t_faces>/orl_faces/s14/1.pgm
|
|
|
|
* <path_to_at&t_faces>/orl_faces/s15/1.pgm
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include <opencv2/core/core.hpp>
|
2014-07-04 22:48:15 +08:00
|
|
|
#include "opencv2/imgcodecs.hpp"
|
2012-08-23 11:21:49 +08:00
|
|
|
#include <opencv2/highgui/highgui.hpp>
|
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
///////////////////////
|
|
|
|
// Functions
|
2012-09-04 21:44:23 +08:00
|
|
|
static void read_imgList(const string& filename, vector<Mat>& images) {
|
2012-08-23 11:21:49 +08:00
|
|
|
std::ifstream file(filename.c_str(), ifstream::in);
|
|
|
|
if (!file) {
|
|
|
|
string error_message = "No valid input file was given, please check the given filename.";
|
2013-04-08 02:45:38 +08:00
|
|
|
CV_Error(Error::StsBadArg, error_message);
|
2012-08-23 11:21:49 +08:00
|
|
|
}
|
|
|
|
string line;
|
|
|
|
while (getline(file, line)) {
|
|
|
|
images.push_back(imread(line, 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-04 21:44:23 +08:00
|
|
|
static Mat formatImagesForPCA(const vector<Mat> &data)
|
2012-08-23 11:21:49 +08:00
|
|
|
{
|
2012-10-25 00:27:20 +08:00
|
|
|
Mat dst(static_cast<int>(data.size()), data[0].rows*data[0].cols, CV_32F);
|
2012-08-23 11:21:49 +08:00
|
|
|
for(unsigned int i = 0; i < data.size(); i++)
|
|
|
|
{
|
|
|
|
Mat image_row = data[i].clone().reshape(1,1);
|
|
|
|
Mat row_i = dst.row(i);
|
2012-09-04 21:44:23 +08:00
|
|
|
image_row.convertTo(row_i,CV_32F);
|
2012-08-23 11:21:49 +08:00
|
|
|
}
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2012-09-04 21:44:23 +08:00
|
|
|
static Mat toGrayscale(InputArray _src) {
|
2012-08-23 11:21:49 +08:00
|
|
|
Mat src = _src.getMat();
|
|
|
|
// only allow one channel
|
|
|
|
if(src.channels() != 1) {
|
2013-04-08 02:45:38 +08:00
|
|
|
CV_Error(Error::StsBadArg, "Only Matrices with one channel are supported");
|
2012-08-23 11:21:49 +08:00
|
|
|
}
|
|
|
|
// create and return normalized image
|
|
|
|
Mat dst;
|
|
|
|
cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC1);
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct params
|
|
|
|
{
|
|
|
|
Mat data;
|
|
|
|
int ch;
|
|
|
|
int rows;
|
|
|
|
PCA pca;
|
|
|
|
string winName;
|
|
|
|
};
|
|
|
|
|
2012-09-04 21:44:23 +08:00
|
|
|
static void onTrackbar(int pos, void* ptr)
|
|
|
|
{
|
2012-08-23 11:21:49 +08:00
|
|
|
cout << "Retained Variance = " << pos << "% ";
|
|
|
|
cout << "re-calculating PCA..." << std::flush;
|
2012-09-04 21:44:23 +08:00
|
|
|
|
2012-08-23 11:21:49 +08:00
|
|
|
double var = pos / 100.0;
|
2012-09-04 21:44:23 +08:00
|
|
|
|
2012-08-23 11:21:49 +08:00
|
|
|
struct params *p = (struct params *)ptr;
|
2012-09-04 21:44:23 +08:00
|
|
|
|
2013-04-08 02:45:38 +08:00
|
|
|
p->pca = PCA(p->data, cv::Mat(), PCA::DATA_AS_ROW, var);
|
2012-09-04 21:44:23 +08:00
|
|
|
|
2012-08-23 11:21:49 +08:00
|
|
|
Mat point = p->pca.project(p->data.row(0));
|
|
|
|
Mat reconstruction = p->pca.backProject(point);
|
|
|
|
reconstruction = reconstruction.reshape(p->ch, p->rows);
|
|
|
|
reconstruction = toGrayscale(reconstruction);
|
2012-09-04 21:44:23 +08:00
|
|
|
|
2012-08-23 11:21:49 +08:00
|
|
|
imshow(p->winName, reconstruction);
|
|
|
|
cout << "done! # of principal components: " << p->pca.eigenvectors.rows << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////
|
|
|
|
// Main
|
2012-09-04 21:44:23 +08:00
|
|
|
int main(int argc, char** argv)
|
2012-08-23 11:21:49 +08:00
|
|
|
{
|
2015-08-01 23:24:23 +08:00
|
|
|
cv::CommandLineParser parser(argc, argv, "{@input||image list}{help h||show help message}");
|
|
|
|
if (parser.has("help"))
|
|
|
|
{
|
|
|
|
parser.printMessage();
|
|
|
|
exit(0);
|
2012-08-23 11:21:49 +08:00
|
|
|
}
|
|
|
|
// Get the path to your CSV.
|
2015-08-01 23:24:23 +08:00
|
|
|
string imgList = parser.get<string>("@input");
|
|
|
|
if (imgList.empty())
|
|
|
|
{
|
|
|
|
parser.printMessage();
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-09-04 21:44:23 +08:00
|
|
|
|
2012-08-23 11:21:49 +08:00
|
|
|
// vector to hold the images
|
|
|
|
vector<Mat> images;
|
2012-09-04 21:44:23 +08:00
|
|
|
|
2012-08-23 11:21:49 +08:00
|
|
|
// Read in the data. This can fail if not valid
|
|
|
|
try {
|
|
|
|
read_imgList(imgList, images);
|
|
|
|
} catch (cv::Exception& e) {
|
|
|
|
cerr << "Error opening file \"" << imgList << "\". Reason: " << e.msg << endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-09-04 21:44:23 +08:00
|
|
|
|
2012-08-23 11:21:49 +08:00
|
|
|
// Quit if there are not enough images for this demo.
|
|
|
|
if(images.size() <= 1) {
|
|
|
|
string error_message = "This demo needs at least 2 images to work. Please add more images to your data set!";
|
2013-04-08 02:45:38 +08:00
|
|
|
CV_Error(Error::StsError, error_message);
|
2012-08-23 11:21:49 +08:00
|
|
|
}
|
2012-09-04 21:44:23 +08:00
|
|
|
|
2012-08-23 11:21:49 +08:00
|
|
|
// Reshape and stack images into a rowMatrix
|
|
|
|
Mat data = formatImagesForPCA(images);
|
2012-09-04 21:44:23 +08:00
|
|
|
|
2012-08-23 11:21:49 +08:00
|
|
|
// perform PCA
|
2013-04-08 02:45:38 +08:00
|
|
|
PCA pca(data, cv::Mat(), PCA::DATA_AS_ROW, 0.95); // trackbar is initially set here, also this is a common value for retainedVariance
|
2012-09-04 21:44:23 +08:00
|
|
|
|
|
|
|
// Demonstration of the effect of retainedVariance on the first image
|
2012-08-23 11:21:49 +08:00
|
|
|
Mat point = pca.project(data.row(0)); // project into the eigenspace, thus the image becomes a "point"
|
|
|
|
Mat reconstruction = pca.backProject(point); // re-create the image from the "point"
|
|
|
|
reconstruction = reconstruction.reshape(images[0].channels(), images[0].rows); // reshape from a row vector into image shape
|
|
|
|
reconstruction = toGrayscale(reconstruction); // re-scale for displaying purposes
|
2012-09-04 21:44:23 +08:00
|
|
|
|
2012-08-23 11:21:49 +08:00
|
|
|
// init highgui window
|
|
|
|
string winName = "Reconstruction | press 'q' to quit";
|
2013-04-08 02:45:38 +08:00
|
|
|
namedWindow(winName, WINDOW_NORMAL);
|
2012-09-04 21:44:23 +08:00
|
|
|
|
2012-08-23 11:21:49 +08:00
|
|
|
// params struct to pass to the trackbar handler
|
|
|
|
params p;
|
|
|
|
p.data = data;
|
|
|
|
p.ch = images[0].channels();
|
|
|
|
p.rows = images[0].rows;
|
|
|
|
p.pca = pca;
|
|
|
|
p.winName = winName;
|
2012-09-04 21:44:23 +08:00
|
|
|
|
2012-08-23 11:21:49 +08:00
|
|
|
// create the tracbar
|
|
|
|
int pos = 95;
|
2012-09-04 21:44:23 +08:00
|
|
|
createTrackbar("Retained Variance (%)", winName, &pos, 100, onTrackbar, (void*)&p);
|
|
|
|
|
2012-08-23 11:21:49 +08:00
|
|
|
// display until user presses q
|
|
|
|
imshow(winName, reconstruction);
|
2012-09-04 21:44:23 +08:00
|
|
|
|
2012-09-17 04:42:34 +08:00
|
|
|
int key = 0;
|
2012-08-23 11:21:49 +08:00
|
|
|
while(key != 'q')
|
|
|
|
key = waitKey();
|
2012-09-04 21:44:23 +08:00
|
|
|
|
|
|
|
return 0;
|
2012-08-23 11:21:49 +08:00
|
|
|
}
|