我正在尝试用4个图像训练我的svm。我所有的图片都是300*400。我将它们的大小调整为304*400,这样我就可以得到我的图像的HOGDescriptor,因为16*16块。然后我用Core。hconcat(mats, trainData)来收集我所有的图像到一个Mat.之后,当我试图为我的trainData设置标签时,在train部分我得到下面的错误。我是openCV的新手。怎么了?
Mat rose1 = new Mat();
Mat rose2 = new Mat();
Mat rose3 = new Mat();
Mat rose4 = new Mat();
Mat rose5 = new Mat();
try {
rose1 = org.opencv.android.Utils.loadResource(
getApplicationContext(), R.drawable.rose1);
rose2 = org.opencv.android.Utils.loadResource(
getApplicationContext(), R.drawable.rose2);
rose3 = org.opencv.android.Utils.loadResource(
getApplicationContext(), R.drawable.rose3);
rose4 = org.opencv.android.Utils.loadResource(
getApplicationContext(), R.drawable.rose4);
rose5 = org.opencv.android.Utils.loadResource(
getApplicationContext(), R.drawable.rose5);
} catch (IOException e) {
e.printStackTrace();
}
Mat rose1Resized = new Mat();
Mat rose2Resized = new Mat();
Mat rose3Resized = new Mat();
Mat rose4Resized = new Mat();
Size sz = new Size(304, 400);
Imgproc.resize(rose1, rose1Resized, sz);
Imgproc.resize(rose2, rose2Resized, sz);
Imgproc.resize(rose3, rose3Resized, sz);
Imgproc.resize(rose4, rose4Resized, sz);
// HOG
MatOfFloat rose1Float = new MatOfFloat();
MatOfFloat rose2Float = new MatOfFloat();
MatOfFloat rose3Float = new MatOfFloat();
MatOfFloat rose4Float = new MatOfFloat();
HOGDescriptor hog = new HOGDescriptor(new Size(304, 400), new Size(16,
16), new Size(new Point(8, 8)), new Size(new Point(8, 8)), 9);
hog.compute(rose1Resized, rose1Float);
hog.compute(rose2Resized, rose2Float);
hog.compute(rose3Resized, rose3Float);
hog.compute(rose4Resized, rose4Float);
ArrayList<Mat> mats = new ArrayList<>();
mats.add(rose1Float);
mats.add(rose2Float);
mats.add(rose3Float);
mats.add(rose4Float);
// SVM
Mat trainData = new Mat();
Core.hconcat(mats, trainData);
float[] lableFloat = { 1, 1, 1, 1 };
Mat lables = new Mat(1, 4, CvType.CV_32FC1);
lables.put(0, 0, lableFloat);
CvSVM svm = new CvSVM();
CvSVMParams params = new CvSVMParams();
params.set_svm_type(CvSVM.C_SVC);
params.set_kernel_type(CvSVM.LINEAR);
params.set_term_crit(new TermCriteria(TermCriteria.EPS, 100, 1e-6));
svm.train(trainData, lables, new Mat(), new Mat(), params);
错误是:E/AndroidRuntime(27347): CvException [org.opencv.core. cn]CvException: cv::Exception:/home/report/ci_slave_desktop/50-SDK/opencv/modules/ml/src/inner_functions.cpp:671:错误:(-209)响应数组必须包含与函数cvPreprocessCategoricalResponses中样本总数相同的元素
首先在获得HOG后重塑matofffloat。因为rose1Float是65268*1,我需要它在一行Mat.
Mat roseReshaped1 = rose1Float.reshape(1, 1);
Mat roseReshaped2 = rose2Float.reshape(1, 1);
Mat roseReshaped3 = rose3Float.reshape(1, 1);
Mat roseReshaped4 = rose4Float.reshape(1, 1);
然后我用push_back代替"Core "。trainData hconcat(垫)"
Mat trainData = new Mat(0, sizeOfCols, CvType.CV_32FC1);
trainData.push_back(roseReshaped1);
trainData.push_back(roseReshaped2);
trainData.push_back(roseReshaped3);
trainData.push_back(roseReshaped4);
我的trainData将是4*65268,这是我的标签。或者如openv所说,回应!
int[] l = { 1, 2, 3, 4 };
Mat lables = new Mat(4, 1, CvType.CV_32SC1);
lables.put(0, 0, l);
现在一切都很好。感谢@berak