cvPreparePredictData中不支持的格式或格式组合(输入样本必须具有32fC1类型)
Unsupported format or combination of formats (Input sample must have 32fC1 type) in cvPreparePredictData
我试图训练我自己的SVM来检测人,我将图像序列全部调整为320x240。我在训练过程中将图像转换为灰色,在测试过程中将测试图像转换为灰度,但我出现了错误。
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv/cv.h>
#include <iostream>
#include <vector>
#include <stdio.h>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/nonfree/features2d.hpp>
#include <fstream>
using namespace std;
using namespace cv;
int main(int argc, char* argv[]) {
String folder = "output/*.jpg";
vector<String> filenames;
glob(folder, filenames);
Mat gray,gray1;
int img_area=320*240;
Mat training_mat(filenames.size(),img_area,CV_32FC1);
Mat labels(filenames.size(),1,CV_32FC1);
for(int i = 0; i < 900; i++){
labels.at<float>(i,0)=1;
}
for(int i = 900; i < filenames.size(); i++){
labels.at<float>(i,0)=-1;
}
for (size_t i = 0; i < filenames.size(); ++i){
Mat img = imread(filenames[i]);
cvtColor(img, gray, CV_RGB2GRAY);
int ii = 0;
for (int k = 0; k < gray.rows; k++) {
for (int j = 0; j < gray.cols; j++) {
training_mat.at<float>(i,ii++) = gray.at<uchar>(k,j);
}
}
}
CvSVMParams params;
params.svm_type = CvSVM::C_SVC;
params.kernel_type = CvSVM::POLY;
params.gamma = 3;
params.degree = 3;
CvSVM svm;
svm.train(training_mat, labels, Mat(), Mat(), params);
svm.save("svm_filename"); // saving
svm.load("svm_filename"); // loading
Mat image = imread("output/500.jpg");
cvtColor(image, gray1, CV_RGB2GRAY);
int response = svm.predict(gray1);
if(response == 1){
cout << "person " << endl ;
}
return 0;
}
我编译的程序我得到了这个错误:
OpenCV错误:在cvPreparePredictData,file/home/mourad/occv/opencv2.4.10/modules/ml/src/inner_functions.cpp中,抛出"cv::Exception"实例后调用的1102行终止不支持的格式或格式组合(输入样本必须具有32fC1类型)
您也应该将gray1
转换为32FC1
。
cvtColor(image, gray1, CV_RGB2GRAY);
gray1.convertTo(gray1, CV_32FC1, 1.0/255.0); // <-- add this line before predict
int response = svm.predict(gray1);
此外,请注意,下面的代码不是从uchar
转换为float
的正确方法,因为它们应该使用不同的范围:[0,255]
和[0,1]
。
training_mat.at<float>(i,ii++) = gray.at<uchar>(k,j);
我在我的程序中添加了这个:
cvtColor(image,gray1, CV_RGB2GRAY);
Mat image_mat(1,img_area,CV_32FC1);
int jj=0;
for (int k = 0; k < gray1.rows; k++) {
for (int j = 0; j < gray1.cols; j++) {
image_mat.at<float>(0,jj++) = gray.at<uchar>(k,j);
}
}
int response=svm.predict(image_mat);
if(response == 1){
cout << "person detected" << endl;
}
但程序运行时没有显示任何内容。
相关文章:
- 如何在openssl-ecc中获取十六进制格式的私钥
- 将"打开的CV图像"中的"颜色"转换为整数格式
- TDateTime格式在C++Builder中不会更改
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 检查不带转换的扫描格式
- 当我尝试使用 sstream 和分面将 Boost Time_duration转换为字符串时,我没有得到所需的格式
- 是否可以从格式字符串中检索"width"
- clang格式:宏的缩进
- clang格式:禁用排序包含
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 将RGB图像保存为PPM格式
- 询问在设计我的手臂模拟器功能表示格式1
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 带有Protobuf序列化的C++Hazelcast:字符串不是UTF-8格式的
- 如何将strftime中的格式错误作为异常捕获
- 将CHW格式的浮点向量转换为cv::Mat
- 如何将二进制格式的 C++ 对象的 std::vector 保存到磁盘?
- 如何以叮当格式设置评论的行长?
- cvPreparePredictData中不支持的格式或格式组合(输入样本必须具有32fC1类型)
- Istream:每个样本读取和wav格式位