What's the difference between cvtype values in OPENCV?
/**
*
*/
package com.aistrong.cocke.extraction;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Size;
import org.opencv.core.TermCriteria;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.ml.Ml;
import org.opencv.ml.SVM;
import org.opencv.ml.TrainData;
import com.aistrong.cocke.extraction.common.util.ImageUtil;
import com.aistrong.cocke.extraction.common.util.OSUtil;
public class Train {
//存储训练集
public ArrayList<Mat> trainingImages = new ArrayList<Mat>();
//存储标签
public ArrayList<Integer> trainingLabels = new ArrayList<Integer>();
public static void main(String[] args) {
String relativelyPath = System.getProperty("user.dir");
String arch = System.getProperty("os.arch");
String os = System.getProperty("os.name");
if (OSUtil.isLinux(os)) {
System.load(relativelyPath + "/dll/opencv/linux/x64/libopencv_java341.so");
} else if (OSUtil.isMacos(os)) {
/// System.load(relativelyPath +
/// "/dll/opencv/os/libopencv_java341.dylib");
} else if (OSUtil.isMacosX(os)) {
// System.load(relativelyPath +
// "/dll/opencv/osx/libopencv_java341.dylib");
} else if (OSUtil.isWindows(os)) {
if (OSUtil.isArchX64(arch)) {
System.load(relativelyPath + "/dll/opencv/win/x64/opencv_java341.dll");
} else {
System.load(relativelyPath + "/dll/opencv/win/x86/opencv_java341.dll");
}
}
Train train = new Train();
openFile(1, "C:/Users/12575/Desktop/tableImage/line",train);
openFile(0, "C:/Users/12575/Desktop/tableImage/noline",train);
System.out.println("2");
Mat trainingData = new Mat();
Core.vconcat(train.trainingImages, trainingData);
Mat flags = new Mat(train.trainingLabels.size(), 1, CvType.CV_32SC1);
for (int i = 0; i < train.trainingLabels.size(); i++) {
int[] val = { train.trainingLabels.get(i) };
flags.put(i, 0, val);
}
System.out.println("3");
SVM svm = SVM.create();
/***
* 核类型
* CvSVM::LINEAR - 没有任何向映像至高维空间,线性区分(或回归)在原始特征空间中被完成,这是最快的选择。 d(x,y) = x•y == (x,y)
* CvSVM::POLY - 多项式核: d(x,y)= (gamma*(x•y)+coef0)degree
* CvSVM::RBF - 径向基,对于大多数情况都是一个较好的选择:d(x,y)= exp(-gamma*|x-y|2)
* CvSVM::SIGMOID - sigmoid函数被用作核函数:d(x,y) = tanh(gamma*(x•y)+coef0)
* ***/
svm.setKernel(SVM.LINEAR);
/***指定SVM的类型 ,
* 1、C_SVC : C类支撑向量分类机。 n类分组 (n≥2),容许用异常值处罚因子C进行不完全分类
* 2、NU_SVC : n类似然不完全分类的分类器。参数nu取代了c,其值在区间【0,1】中,nu越大,决策边界越平滑。
* 3、ONE_CLASS : 单分类器,所有的练习数据提取自同一个类里,然后SVM建树了一个分界线以分别该类在特点空间中所占区域和其它类在特点空间中所占区域。
* 4、EPS_SVR : 回归。 训练集中的特征向量和拟合出来的超平面的距离需要小于p。异常值惩罚因子C被采用。
* 5、NU_SVR : 回归;nu 代替了p
***/
svm.setType(SVM.C_SVC);
//degree, gamma, coef0:都是核函数的参数
svm.setGamma(1);
svm.setCoef0(0);
//C, nu, p:在一般的SVM优化求解时的参数。
svm.setC(1);
svm.setNu(0);
svm.setP(0);
//训练参数迭代终止条件,训练类型,最大迭代次数 ,结果精确性精度
svm.setTermCriteria(new TermCriteria(TermCriteria.MAX_ITER, 20000, 0.0001));
TrainData trainData = TrainData.create(trainingData, Ml.ROW_SAMPLE, flags);
boolean success = svm.train(trainData);
System.out.println(success);
svm.save("C:/Users/12575/Desktop/tmp/bp.xml");
}
private static byte[] getBytes(File file){
byte[] buffer = null;
try {
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
}catch (Exception e) {
e.printStackTrace();
}
return buffer;
}
//读取本地文件
public static void openFile(int flag, String path,Train train) {
File file = new File(path);
File[] files = file.listFiles();
System.out.println(path);
for (File file2 : files) {
if(file2 == null||!file2.exists()){
continue;
}
byte[] filebyte = getBytes(file2);
if(filebyte == null){
continue;
}
Mat input = Imgcodecs.imdecode(new MatOfByte(filebyte), Imgcodecs.IMREAD_UNCHANGED);
input = ImageUtil.gray(input);
Mat dst = new Mat();
Imgproc.resize(input,dst,new Size(640,800));
dst.convertTo(dst, CvType.CV_32FC1);
Mat reshape = dst.reshape(0, 1);
train.trainingImages.add(reshape);
train.trainingLabels.add(flag);
}
}
public void test1() {
// 测试训练的效果
SVM svm = SVM.load("C:/Users/12575/Desktop/tmp/bp.xml");
Mat responseMat = new Mat();
Mat imread = Imgcodecs.imread("C:\\Users\\98432\\Desktop\\platetest\\NoPlate\\S22_KG2187_3.jpg", 0);
Mat dst = new Mat();
Imgproc.resize(input,dst,new Size(640,800));
imread.convertTo(dst, CvType.CV_32FC1);
Mat reshape = imread.reshape(0, 1);
svm.predict(reshape, responseMat, 0);
System.out.println(responseMat.dump());
System.out.println(svm.predict(reshape));
}
}