在Objective-C中调用OpenCV CvSVMParams



我想在iOS中使用OpenCV实现SVM算法,但无法调用Objective-C中的一些方法。如何在Objective-C中调用OpenCV CvSVMParams。当我尝试这个时,它显示错误'未知类型名称CvSVMParams'

编辑:我明白我的错误,我使用旧版本的OpenCV,现在我修复了。但是编译器说

OpenCV错误:断言失败(示例。color == var_count &&在预测文件/Volumes/Linux/builds/precommit_ios/opencv/modules/ml/src/svm.cpp的1919行中,samples.type() == CV_32Flibc + + abi。dylib:以未捕获的异常类型cv终止::exception:/Volumes/Linux/builds/precommit_ios/opencv/modules/ml/src/svm.cpp:1919: error: (-215) samples. cpp:color == var_count &&sample .type() == CV_32F in function predict

#import "CustomObject.h"
#import <opencv2/opencv.hpp>
#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIKit.h>
using namespace cv;
@implementation CustomObject
- (void) supportVectorMachine {
float labels[10] = { 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0 };
cv::Mat labelsMat(10, 1, CV_32FC1, labels);
float trainingData[10][2] = { { 100, 10 }, { 150, 10 }, { 600, 200 }, { 600, 10 }, { 10, 100 }, { 455, 10 }, { 345, 255 }, { 10, 501 }, { 401, 255 }, { 30, 150 } };
cv::Mat trainDataMat(10, 2, CV_32FC1, trainingData);
//opencv 3.0
Ptr<ml::SVM> svm = ml::SVM::create();
// edit: the params struct got removed,
// we use setter/getter now:
svm->setType(ml::SVM::C_SVC);
svm->setKernel(ml::SVM::LINEAR);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));
Ptr<TrainData> td = TrainData::create(trainDataMat, ROW_SAMPLE, labelsMat);
//Create test features
float testData[2] = { 150, 15 };
cv::Mat testDataMat(2, 1, CV_32FC1, testData);
//Predict the class labele for test data sample
float predictLable = svm->predict(testDataMat);
NSLog(@"%f", predictLable);
}
end

问题解决。我忘记为训练数据调用train方法,并将标签从浮点数更改为整数。

- (void) supportVectorMachine {
int labels[10] = {1, 1, 1};
cv::Mat labelsMat(10, 1, CV_32S, labels);
float trainingData[3][3] = { { 84, 191, 19 }, { 24, 186, 17}, { 22, 157, 21} };
//float trainingData[10][1] = { 100, 150, 600, 600, 100, 455, 345, 501, 401, 150};
cv::Mat trainDataMat(3, 3, CV_32FC1, trainingData);

//opencv 3.0
Ptr<ml::SVM> svm = ml::SVM::create();
// edit: the params struct got removed,
// we use setter/getter now:
svm->setType(ml::SVM::C_SVC);
svm->setKernel(ml::SVM::LINEAR);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));
svm->setGamma(3.0);
Ptr<TrainData> td = TrainData::create(trainDataMat, ROW_SAMPLE, labelsMat);
svm->train(td);
//Create test features
float testData[1] = {500};
cv::Mat testDataMat(1, 1, CV_32FC1, testData);
//Predict the class labele for test data sample
float predictLable = svm->predict(testDataMat);
NSLog(@"%f", predictLable);
}

相关内容

  • 没有找到相关文章

最新更新