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.";
}
while (getline(file,
line)) {
}
}
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);
Mat row_i = dst.row(i);
image_row.convertTo(row_i,
CV_32F);
}
return dst;
}
Mat src = _src.getMat();
if(src.channels() != 1) {
}
Mat dst;
return dst;
}
{
Mat data;
int ch;
int rows;
PCA pca;
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!";
}
Mat data = formatImagesForPCA(images);
PCA pca(data,
cv::Mat(), PCA::DATA_AS_ROW, 0.95);
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: core/include/opencv2/core.hpp:120
String msg
the formatted error message
Definition: core/include/opencv2/core.hpp:139
n-dimensional dense array class
Definition: core/include/opencv2/core/mat.hpp:829
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.
@ NORM_MINMAX
flag
Definition: core/include/opencv2/core/base.hpp:207
const _InputArray & InputArray
Definition: core/include/opencv2/core/mat.hpp:443
#define CV_32F
Definition: core/include/opencv2/core/hal/interface.h:78
#define CV_8UC1
Definition: core/include/opencv2/core/hal/interface.h:88
#define CV_Error(code, msg)
Call the error handler.
Definition: core/include/opencv2/core/base.hpp:335
@ WINDOW_NORMAL
the user can resize the window (no constraint) / also use to switch a fullscreen window to a normal s...
Definition: highgui.hpp:143
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.
@ IMREAD_GRAYSCALE
If set, always convert image to the single channel grayscale image (codec internal conversion).
Definition: imgcodecs.hpp:70
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
Loads an image from a file.
void line(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a line segment connecting two points.
int main(int argc, char *argv[])
Definition: highgui_qt.cpp:3
@ StsError
unknown /unspecified error
Definition: core/include/opencv2/core/base.hpp:71
@ StsBadArg
function arg/param is bad
Definition: core/include/opencv2/core/base.hpp:74
PyParams params(const std::string &tag, const std::string &model, const std::string &weights, const std::string &device)
Definition: core/include/opencv2/core.hpp:107