获取OpenCV错误“;“图像步进错误”;在Fisherfaces.train()方法中
我正在使用OpenCV v2.4.1 这就是我正在使用的代码获取OpenCV错误“;“图像步进错误”;在Fisherfaces.train()方法中,opencv,Opencv,我正在使用OpenCV v2.4.1 这就是我正在使用的代码 #include "opencv2/opencv.hpp" #include <iostream> #include <fstream> #include <sstream> using namespace cv; using namespace std; Mat toGrayscale(InputArray _src) { Mat src = _src.getMat(); //
#include "opencv2/opencv.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
using namespace cv;
using namespace std;
Mat toGrayscale(InputArray _src) {
Mat src = _src.getMat();
// only allow one channel
if(src.channels() != 1)
CV_Error(CV_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;
}
void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, char separator = ';') {
std::ifstream file(filename.c_str(), ifstream::in);
if (!file)
throw std::exception();
string line, path, classlabel;
while (getline(file, line)) {
stringstream liness(line);
getline(liness, path, separator);
getline(liness, classlabel);
images.push_back(imread(path, 0));
labels.push_back(atoi(classlabel.c_str()));
}
}
int main(int argc, const char *argv[]) {
// check for command line arguments
/*if (argc != 2) {
cout << "usage: " << argv[0] << " <csv.ext>" << endl;
exit(1);
}*/
// path to your CSV
string fn_csv = string("at.txt");
// images and corresponding labels
vector<Mat> images;
vector<int> labels;
// read in the data
try {
read_csv(fn_csv, images, labels);
} catch (exception&) {
cerr << "Error opening file \"" << fn_csv << "\"." << endl;
exit(1);
}
// get width and height
//int width = images[0].cols;
int height = images[0].rows;
// get test instances
Mat testSample = images[images.size() - 1];
int testLabel = labels[labels.size() - 1];
// ... and delete last element
images.pop_back();
labels.pop_back();
// build the Fisherfaces model
Ptr<FaceRecognizer> model = createFisherFaceRecognizer();
model->train(images, labels);
// test model
int predicted = model->predict(testSample);
cout << "predicted class = " << predicted << endl;
cout << "actual class = " << testLabel << endl;
// get the eigenvectors
Mat W = model->eigenvectors();
// show first 10 fisherfaces
for (int i = 0; i < min(10, W.cols); i++) {
// get eigenvector #i
Mat ev = W.col(i).clone();
// reshape to original size AND normalize between [0...255]
Mat grayscale = toGrayscale(ev.reshape(1, height));
// show image (with Jet colormap)
Mat cgrayscale;
applyColorMap(grayscale, cgrayscale, COLORMAP_JET);
imshow(format("%d", i), cgrayscale);
}
waitKey(0);
return 0;
}
#包括“opencv2/opencv.hpp”
#包括
#包括
#包括
使用名称空间cv;
使用名称空间std;
Mat toGrayscale(输入阵列_src){
Mat src=_src.getMat();
//只允许一个通道
如果(src.channels()!=1)
CV_错误(CV_StsBadArg,“仅支持具有一个通道的矩阵”);
//创建并返回标准化图像
Mat-dst;
cv::normalize(_src,dst,0,255,NORM_MINMAX,cv_8UC1);
返回dst;
}
void read_csv(常量字符串和文件名、向量和图像、向量和标签、字符分隔符=“;”){
std::ifstream文件(filename.c_str(),ifstream::in);
如果(!文件)
抛出std::exception();
字符串行、路径、类标签;
while(getline(文件,行)){
细度(线);
getline(路径、分隔符);
getline(名称、类别标签);
图像。推回(imread(路径,0));
labels.push_back(atoi(classlabel.c_str());
}
}
int main(int argc,const char*argv[]{
//检查命令行参数
/*如果(argc!=2){
不能为我工作
早些时候,我在项目属性中添加了release和Debug lib。现在,我删除了所有已发布的lib,它可以完美地工作。当您构建fisherface模型时
Ptr<FaceRecognizer> model = createFisherFaceRecognizer();
这个解决方案对我来说很有效。之前我在项目属性中添加了release和Debug lib。现在我已经删除了所有已发布的lib,它工作得非常完美:-)既然找到了解决方案,您可以将其作为答案发布并接受:)
createFisherFaceRecognizer(int num_components=0, double threshold=DBL_MAX);