修改svm训练算法,优化车牌识别过程

devA
yuxue 5 years ago
parent 9179985e15
commit 6d6c75ca1f

@ -22,7 +22,6 @@ import com.yuxue.util.FileUtil;
/** /**
* org.opencv * org.opencv
* *
*
* windows * windows
* 1openvphttps://opencv.org/releases/page/2/ 当前使用4.0.1版本 * 1openvphttps://opencv.org/releases/page/2/ 当前使用4.0.1版本
* 2exe \build\java\x64\opencv_java401.dll \build\x64\vc14\bin\ * 2exe \build\java\x64\opencv_java401.dll \build\x64\vc14\bin\
@ -59,7 +58,6 @@ public class SVMTrain {
pridect(); pridect();
} }
public static void train() { public static void train() {
// 正样本 // 136 × 36 像素 训练的源图像文件要相同大小 // 正样本 // 136 × 36 像素 训练的源图像文件要相同大小
@ -88,23 +86,9 @@ public class SVMTrain {
} }
Mat inMat = Imgcodecs.imread(path); // 读取样本文件 Mat inMat = Imgcodecs.imread(path); // 读取样本文件
Mat dst = getFeature(inMat); // 获取样本文件的特征
// 创建一个行数为sample_num, 列数为 rows*cols 的矩阵; 用于存放样本 // 创建一个行数为sample_num, 列数为 rows*cols 的矩阵; 用于存放样本
/*if (trainingDataMat == null) {
trainingDataMat = new Mat(sample_num, inMat.rows() * inMat.cols(), CvType.CV_32F);
}*/
// 样本文件处理,这里是为了过滤不需要的特征,减少训练时间 // 根据实际情况需要进行处理
/*Mat greyMat = new Mat();
Imgproc.cvtColor(inMat, greyMat, Imgproc.COLOR_BGR2GRAY); // 转成灰度图
Mat dst = new Mat();
Imgproc.threshold(greyMat, dst, 100, 255, Imgproc.THRESH_OTSU + Imgproc.THRESH_BINARY);
Mat dst = new Mat(inMat.rows(), inMat.cols(), inMat.type());
Imgproc.Canny(greyMat, dst, 130, 250);*/
Mat dst = getFeature(inMat);
if (trainingDataMat == null) { if (trainingDataMat == null) {
trainingDataMat = new Mat(sample_num, dst.rows() * dst.cols(), CvType.CV_32F); trainingDataMat = new Mat(sample_num, dst.rows() * dst.cols(), CvType.CV_32F);
} }
@ -120,8 +104,7 @@ public class SVMTrain {
} }
} }
trainingDataMat.put(i, 0, arr); // 多张图合并到一张 trainingDataMat.put(i, 0, arr); // 多张图的特征合并到一个矩阵
} }
// Imgcodecs.imwrite(DEFAULT_PATH + "trainingDataMat.jpg", trainingDataMat); // Imgcodecs.imwrite(DEFAULT_PATH + "trainingDataMat.jpg", trainingDataMat);
@ -146,7 +129,6 @@ public class SVMTrain {
} }
public static void pridect() { public static void pridect() {
// 加载训练得到的 xml 模型文件 // 加载训练得到的 xml 模型文件
SVM svm = SVM.load(MODEL_PATH); SVM svm = SVM.load(MODEL_PATH);
@ -158,45 +140,14 @@ public class SVMTrain {
doPridect(svm, DEFAULT_PATH + "test/debug_resize_3.jpg"); doPridect(svm, DEFAULT_PATH + "test/debug_resize_3.jpg");
doPridect(svm, DEFAULT_PATH + "test/S22_KG2187_3.jpg"); doPridect(svm, DEFAULT_PATH + "test/S22_KG2187_3.jpg");
doPridect(svm, DEFAULT_PATH + "test/S22_KG2187_5.jpg"); doPridect(svm, DEFAULT_PATH + "test/S22_KG2187_5.jpg");
} }
public static void doPridect(SVM svm, String imgPath) { public static void doPridect(SVM svm, String imgPath) {
Mat src = Imgcodecs.imread(imgPath);
Mat src = Imgcodecs.imread(imgPath);// 图片大小要和样本一致
/*Imgproc.cvtColor(src, src, Imgproc.COLOR_BGR2GRAY);
Mat dst = new Mat();
Imgproc.threshold(src, dst, 100, 255, Imgproc.THRESH_OTSU + Imgproc.THRESH_BINARY);
Mat dst = new Mat();
Imgproc.Canny(src, dst, 130, 250);
Mat samples = dst.reshape(1, 1);
samples.convertTo(samples, CvType.CV_32F);*/
Mat dst = getFeature(src); Mat dst = getFeature(src);
// 等价于上面两行代码
/*Mat samples = new Mat(1, dst.cols() * dst.rows(), CvType.CV_32F);
float[] arr = new float[dst.cols() * dst.rows()];
int l = 0;
for (int j = 0; j < dst.rows(); j++) { // 遍历行
for (int k = 0; k < dst.cols(); k++) { // 遍历列
double[] a = dst.get(j, k);
arr[l] = (float) a[0];
l++;
}
}
samples.put(0, 0, arr);*/
// Imgcodecs.imwrite(DEFAULT_PATH + "test_1.jpg", samples);
// 如果训练时使用这个标识那么符合的图像会返回9.0 // 如果训练时使用这个标识那么符合的图像会返回9.0
float flag = svm.predict(dst); float flag = svm.predict(dst);
// System.err.println(flag);
if (flag == 0) { if (flag == 0) {
System.err.println(imgPath + " 目标符合"); System.err.println(imgPath + " 目标符合");
} }

@ -21,7 +21,6 @@ import org.opencv.imgproc.Imgproc;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.yuxue.constant.Constant;
/** /**
@ -58,19 +57,14 @@ public class ImageUtil {
// debugMap.put("specMat", 11); // debugMap.put("specMat", 11);
// debugMap.put("chineseMat", 12); // debugMap.put("chineseMat", 12);
// debugMap.put("char_auxRoi", 13); // debugMap.put("char_auxRoi", 13);
// 加载训练库文件
//loadAnnModel(Constant.DEFAULT_ANN_PATH);
//loadSvmModel(Constant.DEFAULT_SVM_PATH);
} }
public static void main(String[] args) { public static void main(String[] args) {
String tempPath = DEFAULT_BASE_TEST_PATH + "test/"; String tempPath = DEFAULT_BASE_TEST_PATH + "test/";
String filename = tempPath + "/100_yuantu.jpg"; String filename = tempPath + "/100_yuantu.jpg";
filename = tempPath + "/100_yuantu1.jpg"; // filename = tempPath + "/100_yuantu4.jpg";
//filename = tempPath + "/109_crop_0.png"; filename = tempPath + "/109_crop_0.png";
Mat src = Imgcodecs.imread(filename); Mat src = Imgcodecs.imread(filename);
@ -226,12 +220,6 @@ public class ImageUtil {
public static Mat threshold(Mat inMat, Boolean debug, String tempPath) { public static Mat threshold(Mat inMat, Boolean debug, String tempPath) {
Mat dst = new Mat(); Mat dst = new Mat();
Imgproc.threshold(inMat, dst, 100, 255, Imgproc.THRESH_OTSU + Imgproc.THRESH_BINARY); Imgproc.threshold(inMat, dst, 100, 255, Imgproc.THRESH_OTSU + Imgproc.THRESH_BINARY);
/*for (int i = 0; i < dst.rows(); i++) {
for (int j = 0; j < dst.cols(); j++) {
System.err.println((int)dst.get(i, j)[0]);
}
}*/
if (debug) { if (debug) {
Imgcodecs.imwrite(tempPath + (debugMap.get("threshold") + 100) + "_threshold.jpg", dst); Imgcodecs.imwrite(tempPath + (debugMap.get("threshold") + 100) + "_threshold.jpg", dst);
} }
@ -260,7 +248,7 @@ public class ImageUtil {
Imgcodecs.imwrite(tempPath + (debugMap.get("morphology") + 100) + "_morphology0.jpg", dst); Imgcodecs.imwrite(tempPath + (debugMap.get("morphology") + 100) + "_morphology0.jpg", dst);
} }
/*// 去除小连通区域 // 去除小连通区域
Mat a = clearSmallConnArea(dst, 3, 10, false, tempPath); Mat a = clearSmallConnArea(dst, 3, 10, false, tempPath);
Mat b = clearSmallConnArea(a, 10, 3, false, tempPath); Mat b = clearSmallConnArea(a, 10, 3, false, tempPath);
// 去除孔洞 // 去除孔洞
@ -269,8 +257,8 @@ public class ImageUtil {
if (debug) { if (debug) {
Imgcodecs.imwrite(tempPath + (debugMap.get("morphology") + 100) + "_morphology1.jpg", d); Imgcodecs.imwrite(tempPath + (debugMap.get("morphology") + 100) + "_morphology1.jpg", d);
}*/ }
return dst; return d;
} }

Loading…
Cancel
Save