From b66b98d9ccf6d827fd0c5c2c597d6a8dde25f716 Mon Sep 17 00:00:00 2001 From: yuxue Date: Tue, 12 May 2020 21:35:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=A0=B7=E6=9C=AC=E8=AE=AD?= =?UTF-8?q?=E7=BB=83=E6=B5=8B=E8=AF=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/com/yuxue/test/EasyPrTest.java | 48 +++++--- src/test/java/com/yuxue/test/PridectTest.java | 68 +++++++++++ src/test/java/com/yuxue/test/TrainTest.java | 115 ++++++++++++++++++ 3 files changed, 211 insertions(+), 20 deletions(-) create mode 100644 src/test/java/com/yuxue/test/PridectTest.java create mode 100644 src/test/java/com/yuxue/test/TrainTest.java diff --git a/src/test/java/com/yuxue/test/EasyPrTest.java b/src/test/java/com/yuxue/test/EasyPrTest.java index e59972b9..29383edd 100644 --- a/src/test/java/com/yuxue/test/EasyPrTest.java +++ b/src/test/java/com/yuxue/test/EasyPrTest.java @@ -36,7 +36,6 @@ public class EasyPrTest { */ @Test public void testPlateRecognise() { - //String imgPath = "res/image/test_image/test.jpg"; String imgPath = "res/image/test_image/plate_recognize.jpg"; Mat src = opencv_imgcodecs.imread(imgPath); @@ -55,7 +54,7 @@ public class EasyPrTest { String palte = cr.charsRecognise(img, "tem/"); // 字符识别 PlateColor color = CoreFunc.getPlateType(img, true); - System.err.println("识别到的车牌: " + palte + "_" + color); + System.err.println("识别到的车牌: " + palte + "_" + color.desc); // 识别的车牌,保存图片文件 //需要先创建文件夹 String str = "d:/PlateDetect/" + palte + "_"+ color + "_" + System.currentTimeMillis() +".png"; String str1 = "d:/PlateDetect/" + i + ".png"; @@ -89,7 +88,7 @@ public class EasyPrTest { for (int i = 0; i < matVector.size(); ++i) { Mat img = matVector.get(i); // 弹窗显示 - //opencv_highgui.imshow("Plate Detected", img); + opencv_highgui.imshow("Plate Detected", img); String str = "d:/test/" + i + ".png"; opencv_imgcodecs.imwrite(str, img); @@ -178,11 +177,17 @@ public class EasyPrTest { System.out.println(result); } + + + + /** + * 测试检测绿牌颜色 + */ @Test - public void testGreenPlate() { + public void testGreenColorReco() { String imgPath = "res/image/test_image/debug_resize_2.jpg"; Mat src = opencv_imgcodecs.imread(imgPath); - + // 获取绿牌的H值范围 /*MatVector hsvSplit = new MatVector(); split(src_hsv, hsvSplit); @@ -213,44 +218,47 @@ public class EasyPrTest { } else { map.put(H, 1); } - + } } map.entrySet().forEach(n->{ System.err.println(n.getKey() + "\t" + n.getValue()); });*/ - + // 判断绿色车牌 - /*Mat src_hsv = new Mat(); + Mat src_hsv = new Mat(); opencv_imgproc.cvtColor(src, src_hsv, opencv_imgproc.CV_BGR2HSV); src_hsv = CoreFunc.colorMatch(src, PlateColor.GREEN, true); System.err.println(CoreFunc.plateColorJudge(src, PlateColor.GREEN, true)); String str = "d:/PlateDetect/src_hsv.png"; - opencv_imgcodecs.imwrite(str, src_hsv);*/ - + opencv_imgcodecs.imwrite(str, src_hsv); + } + + @Test + public void testGreenPlate() { + String imgPath = "res/image/test_image/debug_resize_2.jpg"; + Mat src = opencv_imgcodecs.imread(imgPath); + // 车牌检测对象 PlateDetect plateDetect = new PlateDetect(); plateDetect.setPDLifemode(true); plateDetect.setDebug(false, ""); // 将过程的图块保存到盘符 - + Vector matVector = new Vector(); - + System.err.println(plateDetect.plateDetect(src, matVector)); System.err.println(matVector.size()); - + for (int i = 0; i < matVector.size(); ++i) { // 遍历车牌图块Mat,进行识别 Mat img = matVector.get(i); - // 识别的车牌,保存图片文件 - // 此方法生成的文件,中文名称都是乱码,试了各种编解码均无效,OpenCV自身的编解码问题。 - opencv_highgui.imshow("123", img); - + String str = "d:/PlateDetect/temp/result_.png"; opencv_imgcodecs.imwrite(str, img); - + } - + } - + diff --git a/src/test/java/com/yuxue/test/PridectTest.java b/src/test/java/com/yuxue/test/PridectTest.java new file mode 100644 index 00000000..1203e6f4 --- /dev/null +++ b/src/test/java/com/yuxue/test/PridectTest.java @@ -0,0 +1,68 @@ +package com.yuxue.test; + +import org.opencv.core.Core; +import org.opencv.core.CvType; +import org.opencv.core.Mat; +import org.opencv.imgcodecs.Imgcodecs; +import org.opencv.imgproc.Imgproc; +import org.opencv.ml.SVM; + + +/** + * windows下环境配置: + * 1、官网下载对应版本的openvp:https://opencv.org/releases/page/2/ 当前使用4.0.1版本 + * 2、双击exe文件安装,将 安装目录下\build\java\x64\opencv_java401.dll 拷贝到\build\x64\vc14\bin\目录下 + * 3、eclipse添加User Libraries + * 4、项目右键build path,添加步骤三新增的lib + * + * 未完待续 + * @author yuxue + * @date 2020-05-12 21:34 + */ +public class PridectTest { + + static { + System.loadLibrary(Core.NATIVE_LIBRARY_NAME); + } + + public static void main(String[] args) { + Mat src = Imgcodecs.imread("D:\\xunlian\\a\\0.jpg");//图片大小要和样本一致 + Imgproc.cvtColor(src, src, Imgproc.COLOR_BGR2GRAY); + Mat dst = new Mat(); + Imgproc.Canny(src, dst, 40, 200); + test(dst); + } + + public static void test(Mat src) { + SVM svm = SVM.load("./Result/a.xml");//加载训练得到的 xml + + Mat samples = new Mat(1,src.cols()*src.rows(),CvType.CV_32FC1); + + //转换 src 图像的 cvtype + //失败案例:我试图用 src.convertTo(src, CvType.CV_32FC1); 转换,但是失败了,原因未知。猜测: 内部的数据类型没有转换? + float[] dataArr = new float[src.cols()*src.rows()]; + for(int i =0,f = 0 ;i