沐木返戏 2017-02-24 05:47 采纳率: 0%
浏览 1457

opencv3.2.0下pca.cpp编译后命令行该如何输入?

我用的环境是linux ubuntu+opencv3.2.0,其pca.cpp已经用cmake编译成功,但是执行总是报命令行错误,求解命令行应该如何输入,举例最好。pca.cpp源码如下:/*

  • pca.cpp *
  • Author:
  • Kevin Hughes *
  • Special Thanks to:
  • Philipp Wagner *
  • This program demonstrates how to use OpenCV PCA with a
  • 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 *
  • so for example your input text file would look like this: *
  • /orl_faces/s1/1.pgm
  • /orl_faces/s2/1.pgm
  • /orl_faces/s3/1.pgm
  • /orl_faces/s4/1.pgm
  • /orl_faces/s5/1.pgm
  • /orl_faces/s6/1.pgm
  • /orl_faces/s7/1.pgm
  • /orl_faces/s8/1.pgm
  • /orl_faces/s9/1.pgm
  • /orl_faces/s10/1.pgm
  • /orl_faces/s11/1.pgm
  • /orl_faces/s12/1.pgm
  • /orl_faces/s13/1.pgm
  • /orl_faces/s14/1.pgm
  • /orl_faces/s15/1.pgm * */

#include
#include
#include

#include
#include "opencv2/imgcodecs.hpp"
#include

using namespace cv;
using namespace std;

///////////////////////
// Functions
static void read_imgList(const string& filename, vector& 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, 0));
}
}

static Mat formatImagesForPCA(const vector &data)
{
Mat dst(static_cast(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;
}

static Mat toGrayscale(InputArray _src) {
Mat src = _src.getMat();
// only allow one channel
if(src.channels() != 1) {
CV_Error(Error::StsBadArg, "Only Matrices with one channel are supported");
}
// 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;
};

static void onTrackbar(int pos, void* ptr)
{
cout << "Retained Variance = " << pos << "% ";
cout << "re-calculating PCA..." << std::flush;

double var = pos / 100.0;

struct params *p = (struct params *)ptr;

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;

}

///////////////////////
// Main
int main(int argc, char** argv)
{
cv::CommandLineParser parser(argc, argv, "{@input||image list}{help h||show help message}");
if (parser.has("help"))
{
parser.printMessage();
exit(0);
}
// Get the path to your CSV.
string imgList = parser.get("@input");
if (imgList.empty())
{
parser.printMessage();
exit(1);
}

// vector to hold the images
vector<Mat> images;

// 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);
}

// 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!";
    CV_Error(Error::StsError, error_message);
}

// Reshape and stack images into a rowMatrix
Mat data = formatImagesForPCA(images);

// perform PCA
PCA pca(data, cv::Mat(), PCA::DATA_AS_ROW, 0.95); // trackbar is initially set here, also this is a common value for retainedVariance

// Demonstration of the effect of retainedVariance on the first image
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

// init highgui window
string winName = "Reconstruction | press 'q' to quit";
namedWindow(winName, WINDOW_NORMAL);

// 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;

// create the tracbar
int pos = 95;
createTrackbar("Retained Variance (%)", winName, &pos, 100, onTrackbar, (void*)&p);

// display until user presses q
imshow(winName, reconstruction);

char key = 0;
while(key != 'q')
    key = (char)waitKey();

return 0;
}
看了半天,刚开始学习C++,不太懂,望前辈给个傻瓜都能看懂的解答。

  • 写回答

1条回答 默认 最新

  • devmiao 2017-02-24 21:37
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误