An example using PCA for dimensionality reduction while maintaining an amount of variance
#include <iostream>
#include <fstream>
#include <sstream>
static void read_imgList(const string& filename, vector<Mat>& images) {
std::ifstream file(filename.c_str(), ifstream::in);
if (!file) {
string error_message = "No valid input file was given, please check the given filename.";
CV_Error(Error::StsBadArg, error_message);
}
string line;
while (getline(file, line)) {
images.push_back(imread(line, IMREAD_GRAYSCALE));
}
}
static Mat formatImagesForPCA(
const vector<Mat> &data)
{
Mat dst(
static_cast<int>(data.size()), data[0].rows*data[0].cols,
CV_32F);
for(unsigned int i = 0; i < data.size(); i++)
{
Mat image_row = data[i].clone().reshape(1,1);
}
return dst;
}
CV_Error(Error::StsBadArg,
"Only Matrices with one channel are supported");
}
return dst;
}
{
int ch;
int rows;
string winName;
};
static void onTrackbar(int pos, void* ptr)
{
cout << "Retained Variance = " << pos << "% ";
cout << "re-calculating PCA..." << std::flush;
double var = pos / 100.0;
p->pca =
PCA(p->data,
cv::Mat(), PCA::DATA_AS_ROW, var);
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);
imshow(p->winName, reconstruction);
cout << "done! # of principal components: " << p->pca.eigenvectors.rows << endl;
}
int main(
int argc,
char** argv)
{
if (parser.has("help"))
{
parser.printMessage();
exit(0);
}
string imgList = parser.get<string>("@input");
if (imgList.empty())
{
parser.printMessage();
exit(1);
}
vector<Mat> images;
try {
read_imgList(imgList, images);
cerr <<
"Error opening file \"" << imgList <<
"\". Reason: " << e.
msg << endl;
exit(1);
}
if(images.size() <= 1) {
string error_message = "This demo needs at least 2 images to work. Please add more images to your data set!";
CV_Error(Error::StsError, error_message);
}
Mat data = formatImagesForPCA(images);
Mat point = pca.project(data.
row(0));
Mat reconstruction = pca.backProject(point);
reconstruction = reconstruction.
reshape(images[0].channels(), images[0].rows);
reconstruction = toGrayscale(reconstruction);
string winName = "Reconstruction | press 'q' to quit";
p.data = data;
p.ch = images[0].channels();
p.rows = images[0].rows;
p.pca = pca;
p.winName = winName;
int pos = 95;
createTrackbar(
"Retained Variance (%)", winName, &pos, 100, onTrackbar, (
void*)&p);
imshow(winName, reconstruction);
char key = 0;
while(key != 'q')
return 0;
}
Designed for command line parsing.
Definition utility.hpp:890
Class passed to an error.
Definition exception.hpp:86
std::string msg
the formatted error message
Definition exception.hpp:106
n-dimensional dense array class
Definition mat.hpp:951
Mat row(int y) const
Creates a matrix header for the specified matrix row.
Mat reshape(int cn, int rows=0) const
Changes the shape and/or the number of channels of a 2D matrix without copying the data.
int channels() const
Returns the number of matrix channels.
void convertTo(OutputArray m, int rtype, double alpha=1, double beta=0) const
Converts an array to another data type with optional scaling.
Principal Component Analysis.
Definition core.hpp:2429
void normalize(InputArray src, InputOutputArray dst, double alpha=1, double beta=0, int norm_type=NORM_L2, int dtype=-1, InputArray mask=noArray())
Normalizes the norm or value range of an array.
#define CV_32F
Definition interface.h:81
#define CV_8UC1
Definition interface.h:99
#define CV_Error(code, msg)
Call the error handler.
Definition exception.hpp:174
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
int createTrackbar(const String &trackbarname, const String &winname, int *value, int count, TrackbarCallback onChange=0, void *userdata=0)
Creates a trackbar and attaches it to the specified window.
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3
PyParams params(const std::string &tag, const std::string &model, const std::string &weights, const std::string &device)