From b0bd057f474859f938d76d6d5ec42a844ade523b Mon Sep 17 00:00:00 2001 From: diven Date: Sun, 29 Jan 2023 13:28:09 +0800 Subject: [PATCH] init --- .../visual/open/anpr/model/LocationPoint.java | 34 ++++++ .../visual/open/anpr/model/PlateLocation.java | 110 +++++++++++------- .../anpr/exps/PlateRecognitionExample.java | 32 +++-- .../com/visual/open/anpr/utils/DrawImage.java | 9 +- 4 files changed, 137 insertions(+), 48 deletions(-) create mode 100644 open-anpr-client/src/main/java/com/visual/open/anpr/model/LocationPoint.java diff --git a/open-anpr-client/src/main/java/com/visual/open/anpr/model/LocationPoint.java b/open-anpr-client/src/main/java/com/visual/open/anpr/model/LocationPoint.java new file mode 100644 index 0000000..e4bd37c --- /dev/null +++ b/open-anpr-client/src/main/java/com/visual/open/anpr/model/LocationPoint.java @@ -0,0 +1,34 @@ +package com.visual.open.anpr.model; + +import java.io.Serializable; + +public class LocationPoint implements Serializable { + + /**坐标X的值**/ + private int x; + /**坐标Y的值**/ + private int y; + + public LocationPoint(){} + + public LocationPoint(float x, float y) { + this.x = (int)x; + this.y = (int)y; + } + + public int getX() { + return x; + } + + public void setX(int x) { + this.x = x; + } + + public int getY() { + return y; + } + + public void setY(int y) { + this.y = y; + } +} diff --git a/open-anpr-client/src/main/java/com/visual/open/anpr/model/PlateLocation.java b/open-anpr-client/src/main/java/com/visual/open/anpr/model/PlateLocation.java index 3078ba1..5eb00ca 100755 --- a/open-anpr-client/src/main/java/com/visual/open/anpr/model/PlateLocation.java +++ b/open-anpr-client/src/main/java/com/visual/open/anpr/model/PlateLocation.java @@ -3,64 +3,94 @@ package com.visual.open.anpr.model; import java.io.Serializable; public class PlateLocation implements Serializable { - /**左上角x坐标**/ - private int x; - /**左上角y坐标**/ - private int y; - /**宽度**/ - private int w; - /**高度**/ - private int h; - /** - * 构建坐标 - * @param x - * @param y - * @param w - * @param h - * @return - */ - public static PlateLocation build(int x, int y, int w, int h){ - return new PlateLocation().setX(x).setY(y).setW(w).setH(h); + /**左上角坐标值**/ + private LocationPoint leftTop; + /**右上角坐标**/ + private LocationPoint rightTop; + /**右下角坐标**/ + private LocationPoint rightBottom; + /**左下角坐标**/ + private LocationPoint leftBottom; + + public LocationPoint getLeftTop() { + return leftTop; } - public static PlateLocation build(float x, float y, float w, float h){ - return new PlateLocation().setX((int) x).setY((int) y).setW((int) w).setH((int) h); + public void setLeftTop(LocationPoint leftTop) { + this.leftTop = leftTop; } - public int getX() { - return x; + public LocationPoint getRightTop() { + return rightTop; } - public PlateLocation setX(int x) { - this.x = x; - return this; + public void setRightTop(LocationPoint rightTop) { + this.rightTop = rightTop; } - public int getY() { - return y; + public LocationPoint getRightBottom() { + return rightBottom; } - public PlateLocation setY(int y) { - this.y = y; - return this; + public void setRightBottom(LocationPoint rightBottom) { + this.rightBottom = rightBottom; } - public int getW() { - return w; + public LocationPoint getLeftBottom() { + return leftBottom; } - public PlateLocation setW(int w) { - this.w = w; - return this; + public void setLeftBottom(LocationPoint leftBottom) { + this.leftBottom = leftBottom; + } + + + /** + * x的最小坐标 + * @return + */ + public int minX(){ + return Math.min(Math.min(Math.min(leftTop.getX(), rightTop.getX()), rightBottom.getX()), leftBottom.getX()); } - public int getH() { - return h; + /** + * y的最小坐标 + * @return + */ + public int minY(){ + return Math.min(Math.min(Math.min(leftTop.getY(), rightTop.getY()), rightBottom.getY()), leftBottom.getY()); } - public PlateLocation setH(int h) { - this.h = h; - return this; + /** + * x的最大坐标 + * @return + */ + public int maxX(){ + return Math.max(Math.max(Math.max(leftTop.getX(), rightTop.getX()), rightBottom.getX()), leftBottom.getX()); + } + + /** + * y的最大坐标 + * @return + */ + public int maxY(){ + return Math.max(Math.max(Math.max(leftTop.getY(), rightTop.getY()), rightBottom.getY()), leftBottom.getY()); + } + + /** + * 获取宽度 + * @return + */ + public float width(){ + return (float) Math.sqrt(Math.pow((rightTop.getX()-leftTop.getX()), 2)+Math.pow((rightTop.getY()-leftTop.getY()), 2)); + } + + /** + * 获取高度 + * @return + */ + public float height(){ + return (float) Math.sqrt(Math.pow((rightTop.getX()-rightBottom.getX()), 2)+Math.pow((rightTop.getY()-rightBottom.getY()), 2)); } } diff --git a/open-anpr-test/src/main/java/com/visual/open/anpr/exps/PlateRecognitionExample.java b/open-anpr-test/src/main/java/com/visual/open/anpr/exps/PlateRecognitionExample.java index 8ba8b6e..5fe8f91 100755 --- a/open-anpr-test/src/main/java/com/visual/open/anpr/exps/PlateRecognitionExample.java +++ b/open-anpr-test/src/main/java/com/visual/open/anpr/exps/PlateRecognitionExample.java @@ -37,17 +37,35 @@ public class PlateRecognitionExample { DrawImage drawImage = DrawImage.build(imageA.getAbsolutePath()); if(response.ok()){ for(RecognitionRep recognitionRep : response.getData()){ + //画位置信息 PlateLocation location = recognitionRep.getLocation(); - drawImage.drawRect( - new DrawImage.Rect(location.getX(), location.getY(), location.getW(), location.getH()), 2, Color.RED); - - + drawImage.drawLine( + new DrawImage.Point(location.getLeftTop().getX(), location.getLeftTop().getY()), + new DrawImage.Point(location.getRightTop().getX(), location.getRightTop().getY()), + 2, Color.RED + ); + drawImage.drawLine( + new DrawImage.Point(location.getRightTop().getX(), location.getRightTop().getY()), + new DrawImage.Point(location.getRightBottom().getX(), location.getRightBottom().getY()), + 2, Color.RED + ); + drawImage.drawLine( + new DrawImage.Point(location.getRightBottom().getX(), location.getRightBottom().getY()), + new DrawImage.Point(location.getLeftBottom().getX(), location.getLeftBottom().getY()), + 2, Color.RED + ); + drawImage.drawLine( + new DrawImage.Point(location.getLeftBottom().getX(), location.getLeftBottom().getY()), + new DrawImage.Point(location.getLeftTop().getX(), location.getLeftTop().getY()), + 2, Color.RED + ); + //画识别信息 RecognitionInfo recognition = recognitionRep.getRecognition(); - int fonSize = Float.valueOf(1.4f * location.getW() / recognition.getPlateNo().length()).intValue(); + int fonSize = Float.valueOf(1.4f * location.width() / recognition.getPlateNo().length()).intValue(); drawImage.drawText(recognition.getPlateNo(), - new DrawImage.Point(location.getX(), location.getY()-(int)(fonSize*2.2)), fonSize, Color.RED); + new DrawImage.Point(location.minX(), location.minY()-(int)(fonSize*2.2)), fonSize, Color.RED); drawImage.drawText((recognition.getLayout() == PlateLayout.SINGLE ? "单排" : "双排") + ":" + recognition.getPlateColor().getName(), - new DrawImage.Point(location.getX(), location.getY()-(int)(fonSize*1.2)), fonSize, Color.RED); + new DrawImage.Point(location.minX(), location.minY()-(int)(fonSize*1.2)), fonSize, Color.RED); } } HighGui.imshow("image", drawImage.toMat()); diff --git a/open-anpr-test/src/main/java/com/visual/open/anpr/utils/DrawImage.java b/open-anpr-test/src/main/java/com/visual/open/anpr/utils/DrawImage.java index c6b1c00..d9d9c3d 100644 --- a/open-anpr-test/src/main/java/com/visual/open/anpr/utils/DrawImage.java +++ b/open-anpr-test/src/main/java/com/visual/open/anpr/utils/DrawImage.java @@ -1,6 +1,5 @@ package com.visual.open.anpr.utils; - import org.opencv.core.CvType; import org.opencv.core.Mat; @@ -51,6 +50,14 @@ public class DrawImage { return this; } + public DrawImage drawLine(Point point1, Point point2, int lineWidth, Color color){ + Graphics2D g = (Graphics2D)image.getGraphics(); + g.setColor(color); + g.setStroke(new BasicStroke(lineWidth)); + g.drawLine(point1.x, point1.y, point2.x, point2.y); + return this; + } + public DrawImage drawText(String text, Point point, int fontSize, Color color){ Graphics2D g = image.createGraphics(); g.setColor(color);