测试工具类

devA
yuxue 5 years ago
parent 19575f5e9b
commit c5d6723405

@ -193,8 +193,6 @@ public class PlateLocate {
int k = 1; int k = 1;
for (int i = 0; i < rects.size(); i++) { for (int i = 0; i < rects.size(); i++) {
RotatedRect minRect = rects.get(i); RotatedRect minRect = rects.get(i);
if (verifySizes(minRect)) {
if (debug) { if (debug) {
Point2f rect_points = new Point2f(4); Point2f rect_points = new Point2f(4);
minRect.points(rect_points); minRect.points(rect_points);
@ -229,10 +227,6 @@ public class PlateLocate {
resultVec.add(resultMat); resultVec.add(resultMat);
} }
} }
}
if (debug) {
opencv_imgcodecs.imwrite(tempPath + "debug_result.jpg", result);
}
return resultVec; return resultVec;
} }

@ -1,6 +1,5 @@
package com.yuxue.util; package com.yuxue.util;
import java.util.Arrays; import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -19,7 +18,6 @@ import org.bytedeco.javacpp.opencv_imgcodecs;
import org.bytedeco.javacpp.opencv_imgproc; import org.bytedeco.javacpp.opencv_imgproc;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.yuxue.constant.Constant;
/** /**
@ -36,13 +34,13 @@ public class ImageUtil {
private static String DEFAULT_BASE_TEST_PATH = "D:/PlateDetect/temp/"; private static String DEFAULT_BASE_TEST_PATH = "D:/PlateDetect/temp/";
public static void loadSVM(String path) { public static void loadSvmModel(String path) {
svm.clear(); svm.clear();
svm=SVM.load(path); svm=SVM.load(path);
} }
// 加载ann配置文件 图像转文字的训练库文件 // 加载ann配置文件 图像转文字的训练库文件
public static void loadModel(String path) { public static void loadAnnModel(String path) {
ann.clear(); ann.clear();
ann = ANN_MLP.load(path); ann = ANN_MLP.load(path);
} }
@ -69,8 +67,8 @@ public class ImageUtil {
// debugMap.put("char_auxRoi", 13); // debugMap.put("char_auxRoi", 13);
// 加载训练库文件 // 加载训练库文件
//loadModel(Constant.DEFAULT_ANN_PATH); //loadAnnModel(Constant.DEFAULT_ANN_PATH);
//loadSVM(Constant.DEFAULT_SVM_PATH); //loadSvmModel(Constant.DEFAULT_SVM_PATH);
} }
@ -79,11 +77,11 @@ public class ImageUtil {
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";
Mat inMat = opencv_imgcodecs.imread(filename); Mat src = opencv_imgcodecs.imread(filename);
Boolean debug = true; Boolean debug = true;
Mat gsMat = ImageUtil.gaussianBlur(inMat, debug, tempPath); Mat gsMat = ImageUtil.gaussianBlur(src, debug, tempPath);
Mat grey = ImageUtil.grey(gsMat, debug, tempPath); Mat grey = ImageUtil.grey(gsMat, debug, tempPath);
@ -93,10 +91,14 @@ public class ImageUtil {
Mat morphology = ImageUtil.morphology(threshold, debug, tempPath); Mat morphology = ImageUtil.morphology(threshold, debug, tempPath);
MatVector contours = ImageUtil.contours(inMat, morphology, debug, tempPath); MatVector contours = ImageUtil.contours(src, morphology, debug, tempPath);
Vector<Mat> rects = ImageUtil.screenBlock(src, contours, debug, tempPath);
// ImageUtil.rgb2Hsv(inMat, debug, tempPath); // ImageUtil.rgb2Hsv(inMat, debug, tempPath);
System.err.println("done!!!");
} }
@ -222,15 +224,16 @@ public class ImageUtil {
*/ */
public static MatVector contours(Mat src, Mat inMat, Boolean debug, String tempPath) { public static MatVector contours(Mat src, Mat inMat, Boolean debug, String tempPath) {
MatVector contours = new MatVector(); MatVector contours = new MatVector();
opencv_imgproc.findContours(inMat, contours, // a vector of contours // 提取外部轮廓
opencv_imgproc.CV_RETR_EXTERNAL, // 提取外部轮廓 opencv_imgproc.findContours(inMat, contours, opencv_imgproc.CV_RETR_EXTERNAL, opencv_imgproc.CV_CHAIN_APPROX_NONE);
opencv_imgproc.CV_CHAIN_APPROX_NONE); // all pixels of each contours
if (debug) { if (debug) {
Mat result = new Mat();
src.copyTo(result); // 复制一张图,不在原图上进行操作,防止后续需要使用原图
// 将轮廓描绘到原图 // 将轮廓描绘到原图
opencv_imgproc.drawContours(src, contours, -1, new Scalar(0, 0, 255, 255)); opencv_imgproc.drawContours(result, contours, -1, new Scalar(0, 0, 255, 255));
// 输出带轮廓的原图 // 输出带轮廓的原图
opencv_imgcodecs.imwrite(tempPath + (debugMap.get("contours") + 100) + "_contours.jpg", src); opencv_imgcodecs.imwrite(tempPath + (debugMap.get("contours") + 100) + "_contours.jpg", result);
} }
return contours; return contours;
} }
@ -249,37 +252,61 @@ public class ImageUtil {
public static final int DEFAULT_VERIFY_MIN = 3; public static final int DEFAULT_VERIFY_MIN = 3;
public static final int DEFAULT_VERIFY_MAX = 20; public static final int DEFAULT_VERIFY_MAX = 20;
public static final int DEFAULT_ANGLE = 30; // 角度判断所用常量 public static final int DEFAULT_ANGLE = 30; // 角度判断所用常量
public static final int WIDTH = 136;
public static final int HEIGHT = 36;
public static final int TYPE = opencv_core.CV_8UC3;
@SuppressWarnings("resource")
public static Vector<Mat> screenBlock(Mat src, MatVector contours, Boolean debug, String tempPath){ public static Vector<Mat> screenBlock(Mat src, MatVector contours, Boolean debug, String tempPath){
MatVector rects = new MatVector();
// Vector<RotatedRect> rects = new Vector<RotatedRect>(); Vector<Mat> dst = new Vector<Mat>();
MatVector mv = new MatVector();
for (int i = 0; i < contours.size(); ++i) { for (int i = 0; i < contours.size(); ++i) {
// RotatedRect 该类表示平面上的旋转矩形,有三个属性: 矩形中心点(质心); 边长(长和宽); 旋转角度 // RotatedRect 该类表示平面上的旋转矩形,有三个属性: 矩形中心点(质心); 边长(长和宽); 旋转角度
RotatedRect mr = opencv_imgproc.minAreaRect(contours.get(i)); RotatedRect mr = opencv_imgproc.minAreaRect(contours.get(i));
float angle = Math.abs(mr.angle()); float angle = Math.abs(mr.angle());
if (verifySizes(mr)) { if (verifySizes(mr) && angle <= DEFAULT_ANGLE) { // 判断尺寸及旋转角度 ±30°排除不合法的图块
// rects.add(mr);
// 判断旋转角度 ±30° if (debug) { // 描绘出筛选后的轮廓
if (angle <= DEFAULT_ANGLE) { mv.put(contours.get(i));
rects.put(mr); Mat result = new Mat();
// 旋转角度 src.copyTo(result); // 复制一张图,不在原图上进行操作,防止后续需要使用原图
// 将轮廓描绘到原图
opencv_imgproc.drawContours(result, mv, -1, new Scalar(0, 0, 255, 255));
// 输出带轮廓的原图
opencv_imgcodecs.imwrite(tempPath + (debugMap.get("screenblock") + 100) + "_screenblock.jpg", result);
}
// 旋转角度,根据需要是否进行角度旋转
Size rect_size = new Size((int) mr.size().width(), (int) mr.size().height());
if (mr.size().width() / mr.size().height() < 1) { // 宽度小于高度
angle = 90 + angle; // 旋转90°
rect_size = new Size(rect_size.height(), rect_size.width());
}
Mat rotmat = opencv_imgproc.getRotationMatrix2D(mr.center(), angle, 1); Mat rotmat = opencv_imgproc.getRotationMatrix2D(mr.center(), angle, 1);
Mat img_rotated = new Mat(); Mat img_rotated = new Mat();
opencv_imgproc.warpAffine(src, img_rotated, rotmat, src.size()); // CV_INTER_CUBIC opencv_imgproc.warpAffine(src, img_rotated, rotmat, src.size()); // CV_INTER_CUBIC
} // 切图
} Mat img_crop = new Mat();
opencv_imgproc.getRectSubPix(src, rect_size, mr.center(), img_crop);
if (debug) {
opencv_imgcodecs.imwrite(tempPath + (debugMap.get("crop") + 100) + "_crop_" + i + ".jpg", img_crop);
} }
// 处理切图,调整为指定大小
Mat resized = new Mat(HEIGHT, WIDTH, TYPE);
opencv_imgproc.resize(img_crop, resized, resized.size(), 0, 0, opencv_imgproc.INTER_CUBIC);
if (debug) { if (debug) {
// 将轮廓描绘到原图 opencv_imgcodecs.imwrite(tempPath + (debugMap.get("resize") + 100) + "_resize_" + i + ".jpg", resized);
opencv_imgproc.drawContours(src, rects, -1, new Scalar(0, 0, 255, 255)); }
// 输出带轮廓的原图 dst.add(resized);
opencv_imgcodecs.imwrite(tempPath + (debugMap.get("screenblock") + 100) + "_screenblock.jpg", src); }
} }
return null; return dst;
} }
/** /**
@ -314,14 +341,6 @@ public class ImageUtil {
/** /**
* rgbhsv * rgbhsv
* @param inMat * @param inMat

Loading…
Cancel
Save