改变通道数,代码如何修改public static String[] multiPlateRecognise(opencv_core.Mat mat) { PlateDetect plateDetect = new PlateDetect(); plateDetect.setPDLifemode(true); Vector<opencv_core.Mat> matVector = new Vector<opencv_core.Mat>(10); if (0 == plateDetect.plateDetect(mat, matVector)) { if (matVector.size() > 0) { //字符分割与识别 return new String[]{cr.charsRecognise(matVector.get(0))}; } } return null;public static void main(String[] args) { // 多张车牌图片路径 String[] imgPaths = {"res/image/test_image/plate_locate.jpg", "res/image/test_image/test.jpg", "res/image/test_image/plate_detect.jpg", "res/general_test/京A88731.jpg"}; int sum = imgPaths.length; // 总共处理的图片数量 int errNum = 0; // 识别错误的数量 int sumTime = 0; // 总耗时 long longTime = 0; // 最长处理时长 for (int i = 0; i < sum; i++) { opencv_core.Mat src = opencv_imgcodecs.imread(imgPaths[i]); String[] ret = multiPlateRecognise(src); long now = System.currentTimeMillis(); System.err.println(Arrays.toString(ret)); long s = System.currentTimeMillis() - now; if (s > longTime) { longTime = s; } sumTime += s; boolean flag =false;//是否有一个车牌号识别错误 for (String plate:ret) { if (plate == null) { continue; } String targetPlate = getTargetPlate(imgPaths[i]); if (!plate.equals(targetPlate)){ flag = true; break; } } if (flag) { errNum++; } } BigDecimal errSum = new BigDecimal(errNum); BigDecimal sumNum = new BigDecimal(sum); BigDecimal c = sumNum.subtract(errSum).divide(sumNum,2, RoundingMode.HALF_UP).multiply(new BigDecimal(100)); System.err.println("总耗时:" + sumTime + "ms,平均处理时长:" + sumTime/sum + "ms,错误数量:" + errNum + ",正确识别率:" + c + "%"); } }
时间: 2024-02-14 17:16:34 浏览: 69
如果要改变通道数,需要在代码中对读入的图像进行通道数的转换。假设现在要将读入的图像转换为单通道灰度图像,可以在`multiPlateRecognise`方法中添加如下代码:
```
opencv_core.Mat grayMat = new opencv_core.Mat();
opencv_imgproc.cvtColor(mat, grayMat, opencv_imgproc.COLOR_BGR2GRAY);
```
这里使用了OpenCV提供的`cvtColor`方法将BGR图像转换为灰度图像。接下来,将`grayMat`作为参数传入`PlateDetect`的`plateDetect`方法进行车牌检测和分割。最后,将字符分割得到的多张车牌图像分别传入`charsRecognise`方法进行字符识别即可。完整代码如下:
```
public static String[] multiPlateRecognise(opencv_core.Mat mat) {
PlateDetect plateDetect = new PlateDetect();
plateDetect.setPDLifemode(true);
Vector<opencv_core.Mat> matVector = new Vector<opencv_core.Mat>(10);
opencv_core.Mat grayMat = new opencv_core.Mat();
opencv_imgproc.cvtColor(mat, grayMat, opencv_imgproc.COLOR_BGR2GRAY);
if (0 == plateDetect.plateDetect(grayMat, matVector)) {
if (matVector.size() > 0) {
String[] plates = new String[matVector.size()];
for (int i = 0; i < matVector.size(); i++) {
plates[i] = cr.charsRecognise(matVector.get(i));
}
return plates;
}
}
return null;
}
```
阅读全文