From 7ca3521d0284da9c720ebf40110f293da2b96d82 Mon Sep 17 00:00:00 2001 From: fushoujiang Date: Wed, 27 Nov 2024 17:13:19 +0800 Subject: [PATCH 1/3] =?UTF-8?q?Picture/ImageTools=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=B1=BB=E9=9D=99=E6=80=81=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/dromara/easyai/tools/ImageTools.java | 10 +- .../org/dromara/easyai/tools/Picture.java | 104 ++++++++++-------- 2 files changed, 66 insertions(+), 48 deletions(-) diff --git a/src/main/java/org/dromara/easyai/tools/ImageTools.java b/src/main/java/org/dromara/easyai/tools/ImageTools.java index bd68795..efe510e 100644 --- a/src/main/java/org/dromara/easyai/tools/ImageTools.java +++ b/src/main/java/org/dromara/easyai/tools/ImageTools.java @@ -11,9 +11,13 @@ import java.awt.image.BufferedImage; import java.io.*; import java.util.List; +/** + * 图片输出工具类 + * @author lidapeng + */ public class ImageTools { - public void writeImage(ThreeChannelMatrix img, String url) { + public static void writeImage(ThreeChannelMatrix img, String url) { ByteArrayOutputStream b = null; FileOutputStream fileOutputStream = null; try { @@ -36,7 +40,7 @@ public class ImageTools { } } - public void drawBox(String fileURL, List borderFoods, String outFileName, int fontSize) throws Exception { + public static void drawBox(String fileURL, List borderFoods, String outFileName, int fontSize) throws Exception { File file = new File(fileURL); BufferedImage image2 = ImageIO.read(file); int width = image2.getWidth(); @@ -55,7 +59,7 @@ public class ImageTools { ImageIO.write(bi, "jpg", new FileOutputStream(outFileName)); } - public ByteArrayOutputStream drawImage(ThreeChannelMatrix img) throws Exception { + public static ByteArrayOutputStream drawImage(ThreeChannelMatrix img) throws Exception { Matrix matrixR = img.getMatrixR(); Matrix matrixG = img.getMatrixG(); Matrix matrixB = img.getMatrixB(); diff --git a/src/main/java/org/dromara/easyai/tools/Picture.java b/src/main/java/org/dromara/easyai/tools/Picture.java index 049a43a..271e05e 100644 --- a/src/main/java/org/dromara/easyai/tools/Picture.java +++ b/src/main/java/org/dromara/easyai/tools/Picture.java @@ -8,17 +8,19 @@ import java.awt.image.BufferedImage; import java.io.File; import java.io.InputStream; +/** + * @author lidapeng + * 图片工具类 + */ public class Picture { - private int pictureWidth; - private int pictureHeight; - private boolean vertical = false; - public void vertical() {//强制竖向图 - vertical = true; - } - - //从本地文件拿出图像矩阵 - public Matrix getImageMatrixByLocal(String fileURL) throws Exception { + /** + * 从本地文件拿出图像矩阵 + * @param fileURL 图片本地地址 + * @return Matrix + * @throws Exception + */ + public static Matrix getImageMatrixByLocal(String fileURL) throws Exception { File file = new File(fileURL); BufferedImage bi = null; try { @@ -29,27 +31,44 @@ public class Picture { return getImage(bi); } + /** + * 获取图片的RGB三通道矩阵 + * @param file 文件,原图,不强制转换 + * @return + * @throws Exception + */ public ThreeChannelMatrix getThreeMatrix(File file) throws Exception { + return getThreeMatrix(file,false); + } + + /** + * 获取图片的RGB三通道矩阵 + * @param file 文件 + * @param vertical 是否强制竖直 + * @return threeChannelMatrix + * @throws Exception + */ + public ThreeChannelMatrix getThreeMatrix(File file,boolean vertical) throws Exception { BufferedImage bi = null; try { bi = ImageIO.read(file); } catch (Exception e) { e.printStackTrace(); } - return this.getThreeChannel(bi); + return getThreeChannel(bi,vertical); } - public ThreeChannelMatrix getThreeMatrix(InputStream file) throws Exception { + public static ThreeChannelMatrix getThreeMatrix(InputStream file) throws Exception { BufferedImage bi = null; try { bi = ImageIO.read(file); } catch (Exception e) { e.printStackTrace(); } - return this.getThreeChannel(bi); + return getThreeChannel(bi); } - public ThreeChannelMatrix getThreeMatrix(String fileURL) throws Exception { + public static ThreeChannelMatrix getThreeMatrix(String fileURL) throws Exception { File file = new File(fileURL); BufferedImage bi = null; try { @@ -61,7 +80,7 @@ public class Picture { } // - public Matrix getImageMatrixByIo(InputStream inputStream) throws Exception { + public static Matrix getImageMatrixByIo(InputStream inputStream) throws Exception { BufferedImage bi = null; try { bi = ImageIO.read(inputStream); @@ -71,11 +90,9 @@ public class Picture { return getImage(bi); } - private Matrix getImage(BufferedImage bi) throws Exception { + private static Matrix getImage(BufferedImage bi) throws Exception { int width = bi.getWidth();//最大宽度 int height = bi.getHeight();//最大高度 - pictureWidth = width; - pictureHeight = height; Matrix matrix = new Matrix(height, width);//行,列 for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { @@ -87,21 +104,30 @@ public class Picture { return matrix; } - private ThreeChannelMatrix getThreeChannel(BufferedImage bi) throws Exception { - int width = bi.getWidth();//最大宽度 - int height = bi.getHeight();//最大高度 + private static ThreeChannelMatrix getThreeChannel(BufferedImage bi) throws Exception { + return getThreeChannel(bi,false); + } + + private static ThreeChannelMatrix getThreeChannel(BufferedImage bi,boolean vertical) throws Exception { + //最大宽度 + int width = bi.getWidth(); + //最大高度 + int height = bi.getHeight(); boolean rotate = false; if (vertical && width > height) { rotate = true; - width = bi.getHeight();//最大宽度 - height = bi.getWidth();//最大高度 + //最大宽度 + width = bi.getHeight(); + //最大高度 + height = bi.getWidth(); } ThreeChannelMatrix threeChannelMatrix = new ThreeChannelMatrix(); threeChannelMatrix.setX(height); threeChannelMatrix.setY(width); - Matrix matrixR = new Matrix(height, width);//行,列 - Matrix matrixG = new Matrix(height, width);//行,列 - Matrix matrixB = new Matrix(height, width);//行,列 + //行,列 + Matrix matrixR = new Matrix(height, width); + Matrix matrixG = new Matrix(height, width); + Matrix matrixB = new Matrix(height, width); Matrix matrixH = new Matrix(height, width); threeChannelMatrix.setMatrixR(matrixR); threeChannelMatrix.setMatrixG(matrixG); @@ -109,15 +135,17 @@ public class Picture { threeChannelMatrix.setH(matrixH); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { - int pixel;// 下面三行代码将一个数字转换为RGB数字 + // 下面三行代码将一个数字转换为RGB数字 + int pixel; if (rotate) { - pixel = bi.getRGB(i, j);// 下面三行代码将一个数字转换为RGB数字 + // 下面三行代码将一个数字转换为RGB数字 + pixel = bi.getRGB(i, j); } else { pixel = bi.getRGB(j, i); } - int r = (pixel & 0xff0000) >> 16;//R - int g = (pixel & 0xff00) >> 8;//G - int b = (pixel & 0xff);//B + int r = (pixel & 0xff0000) >> 16; + int g = (pixel & 0xff00) >> 8; + int b = (pixel & 0xff); matrixR.setNub(i, j, r / 255D); matrixG.setNub(i, j, g / 255D); matrixB.setNub(i, j, b / 255D); @@ -127,7 +155,7 @@ public class Picture { return threeChannelMatrix; } - private double dimensionReduction(int pixel) {//提取灰度进行降维 + private static double dimensionReduction(int pixel) {//提取灰度进行降维 int r = (pixel & 0xff0000) >> 16;//R int g = (pixel & 0xff00) >> 8;//G int b = (pixel & 0xff);//B @@ -135,19 +163,5 @@ public class Picture { return gray; } - public int getPictureWidth() { - return pictureWidth; - } - public void setPictureWidth(int pictureWidth) { - this.pictureWidth = pictureWidth; - } - - public int getPictureHeight() { - return pictureHeight; - } - - public void setPictureHeight(int pictureHeight) { - this.pictureHeight = pictureHeight; - } } From 9252e803fa537a9dc96981a000de088201c702c1 Mon Sep 17 00:00:00 2001 From: fushoujiang Date: Wed, 27 Nov 2024 19:04:41 +0800 Subject: [PATCH 2/3] =?UTF-8?q?Picture/ImageTools=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=B1=BB=E9=9D=99=E6=80=81=E5=8C=96+=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E8=BE=93=E5=87=BAfile=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/dromara/easyai/tools/ImageTools.java | 20 ++++++++++++++++--- .../org/dromara/easyai/tools/Picture.java | 10 ++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/dromara/easyai/tools/ImageTools.java b/src/main/java/org/dromara/easyai/tools/ImageTools.java index efe510e..845afc0 100644 --- a/src/main/java/org/dromara/easyai/tools/ImageTools.java +++ b/src/main/java/org/dromara/easyai/tools/ImageTools.java @@ -60,6 +60,19 @@ public class ImageTools { } public static ByteArrayOutputStream drawImage(ThreeChannelMatrix img) throws Exception { + final BufferedImage bufferedImage = getBufferedImage(img); + ByteArrayOutputStream ar = new ByteArrayOutputStream(); + ImageIO.write(bufferedImage, "PNG", ar); + return ar; + } + public static File drawImage(ThreeChannelMatrix img,String imageName) throws Exception { + final BufferedImage bufferedImage = getBufferedImage(img); + File ar = new File(imageName); + ImageIO.write(bufferedImage, "PNG", ar); + return ar; + } + + private static BufferedImage getBufferedImage(ThreeChannelMatrix img) throws Exception { Matrix matrixR = img.getMatrixR(); Matrix matrixG = img.getMatrixG(); Matrix matrixB = img.getMatrixB(); @@ -85,8 +98,9 @@ public class ImageTools { g2.drawRect(j, i, 1, 1); } } - ByteArrayOutputStream ar = new ByteArrayOutputStream(); - ImageIO.write(bi, "PNG", ar); - return ar; + return bi; } + + + } diff --git a/src/main/java/org/dromara/easyai/tools/Picture.java b/src/main/java/org/dromara/easyai/tools/Picture.java index 271e05e..19a6854 100644 --- a/src/main/java/org/dromara/easyai/tools/Picture.java +++ b/src/main/java/org/dromara/easyai/tools/Picture.java @@ -31,6 +31,16 @@ public class Picture { return getImage(bi); } + public static Matrix getImageMatrixByFile(File file) throws Exception { + BufferedImage bi = null; + try { + bi = ImageIO.read(file); + } catch (Exception e) { + e.printStackTrace(); + } + return getImage(bi); + } + /** * 获取图片的RGB三通道矩阵 * @param file 文件,原图,不强制转换 From 2feedfb6303142d80981094d2b464a1f8d0f3837 Mon Sep 17 00:00:00 2001 From: lidapeng <794757862@qq.com> Date: Wed, 27 Nov 2024 21:12:11 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=B7=A5=E5=85=B7=E7=B1=BB=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E9=9D=99=E6=80=81=E4=BF=AE=E6=94=B9=E5=90=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../easyai/entity/ThreeChannelMatrix.java | 30 ++++------------- .../easyai/matrixTools/MatrixOperation.java | 30 +++++++++++++++++ .../org/dromara/easyai/tools/ImageTools.java | 11 +++++-- .../org/dromara/easyai/tools/Picture.java | 32 ++++++------------- .../org/dromara/easyai/yolo/FastYolo.java | 3 +- 5 files changed, 56 insertions(+), 50 deletions(-) diff --git a/src/main/java/org/dromara/easyai/entity/ThreeChannelMatrix.java b/src/main/java/org/dromara/easyai/entity/ThreeChannelMatrix.java index f27010d..7ceb0ed 100644 --- a/src/main/java/org/dromara/easyai/entity/ThreeChannelMatrix.java +++ b/src/main/java/org/dromara/easyai/entity/ThreeChannelMatrix.java @@ -41,33 +41,15 @@ public class ThreeChannelMatrix { } } - private int getLBPValue(Matrix matrix) throws Exception { - int value = 0; - double avg = matrix.getAVG(); - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 3; j++) { - if (i != 1 || j != 1) { - value = value << 1; - if (matrix.getNumber(i, j) > avg) { - value = value | 1; - } - } - } - } - return value; - } - public Matrix getLBPMatrix() throws Exception { - Matrix addMatrix = matrixOperation.add(matrixOperation.add(matrixR, matrixG), matrixB); - Matrix matrix = new Matrix(x / 3, y / 3); - for (int i = 0; i <= x - 3; i += 3) { - for (int j = 0; j <= y - 3; j += 3) { - Matrix aMatrix = addMatrix.getSonOfMatrix(i, j, 3, 3); - int lbp = getLBPValue(aMatrix); - matrix.setNub(i / 3, j / 3, lbp); + Matrix addMatrix = new Matrix(x, y); + for (int i = 0; i < x; i++) { + for (int j = 0; j < y; j++) { + double value = matrixR.getNumber(i, j) + matrixG.getNumber(i, j) + matrixB.getNumber(i, j); + addMatrix.setNub(i, j, value); } } - return matrix; + return matrixOperation.lbpMatrix(addMatrix); } public void standardization() throws Exception {//标准化 diff --git a/src/main/java/org/dromara/easyai/matrixTools/MatrixOperation.java b/src/main/java/org/dromara/easyai/matrixTools/MatrixOperation.java index 328943e..18b12f4 100644 --- a/src/main/java/org/dromara/easyai/matrixTools/MatrixOperation.java +++ b/src/main/java/org/dromara/easyai/matrixTools/MatrixOperation.java @@ -106,6 +106,36 @@ public class MatrixOperation { } } + private int getLBPValue(Matrix matrix) throws Exception { + int value = 0; + double avg = matrix.getAVG(); + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + if (i != 1 || j != 1) { + value = value << 1; + if (matrix.getNumber(i, j) > avg) { + value = value | 1; + } + } + } + } + return value; + } + + public Matrix lbpMatrix(Matrix addMatrix) throws Exception { + int x = addMatrix.getX(); + int y = addMatrix.getY(); + Matrix matrix = new Matrix(x / 3, y / 3); + for (int i = 0; i <= x - 3; i += 3) { + for (int j = 0; j <= y - 3; j += 3) { + Matrix aMatrix = addMatrix.getSonOfMatrix(i, j, 3, 3); + int lbp = getLBPValue(aMatrix); + matrix.setNub(i / 3, j / 3, lbp); + } + } + return matrix; + } + //多元线性回归 public Matrix getLinearRegression(Matrix parameter, Matrix out) throws Exception { if (parameter.getX() == out.getX() && out.isVector()) { diff --git a/src/main/java/org/dromara/easyai/tools/ImageTools.java b/src/main/java/org/dromara/easyai/tools/ImageTools.java index 845afc0..367c1e8 100644 --- a/src/main/java/org/dromara/easyai/tools/ImageTools.java +++ b/src/main/java/org/dromara/easyai/tools/ImageTools.java @@ -13,6 +13,7 @@ import java.util.List; /** * 图片输出工具类 + * * @author lidapeng */ public class ImageTools { @@ -65,7 +66,8 @@ public class ImageTools { ImageIO.write(bufferedImage, "PNG", ar); return ar; } - public static File drawImage(ThreeChannelMatrix img,String imageName) throws Exception { + + public static File drawImage(ThreeChannelMatrix img, String imageName) throws Exception { final BufferedImage bufferedImage = getBufferedImage(img); File ar = new File(imageName); ImageIO.write(bufferedImage, "PNG", ar); @@ -87,12 +89,18 @@ public class ImageTools { int b = (int) (matrixB.getNumber(i, j) * 255D); if (r > 255) { r = 255; + } else if (r < 0) { + r = 0; } if (g > 255) { g = 255; + } else if (g < 0) { + g = 0; } if (b > 255) { b = 255; + } else if (b < 0) { + b = 0; } g2.setColor(new Color(r, g, b)); g2.drawRect(j, i, 1, 1); @@ -102,5 +110,4 @@ public class ImageTools { } - } diff --git a/src/main/java/org/dromara/easyai/tools/Picture.java b/src/main/java/org/dromara/easyai/tools/Picture.java index 19a6854..8327d1f 100644 --- a/src/main/java/org/dromara/easyai/tools/Picture.java +++ b/src/main/java/org/dromara/easyai/tools/Picture.java @@ -16,6 +16,7 @@ public class Picture { /** * 从本地文件拿出图像矩阵 + * * @param fileURL 图片本地地址 * @return Matrix * @throws Exception @@ -43,42 +44,33 @@ public class Picture { /** * 获取图片的RGB三通道矩阵 - * @param file 文件,原图,不强制转换 - * @return - * @throws Exception - */ - public ThreeChannelMatrix getThreeMatrix(File file) throws Exception { - return getThreeMatrix(file,false); - } - - /** - * 获取图片的RGB三通道矩阵 - * @param file 文件 + * + * @param file 文件 * @param vertical 是否强制竖直 * @return threeChannelMatrix * @throws Exception */ - public ThreeChannelMatrix getThreeMatrix(File file,boolean vertical) throws Exception { + public static ThreeChannelMatrix getThreeMatrix(File file, boolean vertical) throws Exception { BufferedImage bi = null; try { bi = ImageIO.read(file); } catch (Exception e) { e.printStackTrace(); } - return getThreeChannel(bi,vertical); + return getThreeChannel(bi, vertical); } - public static ThreeChannelMatrix getThreeMatrix(InputStream file) throws Exception { + public static ThreeChannelMatrix getThreeMatrix(InputStream file, boolean vertical) throws Exception { BufferedImage bi = null; try { bi = ImageIO.read(file); } catch (Exception e) { e.printStackTrace(); } - return getThreeChannel(bi); + return getThreeChannel(bi, vertical); } - public static ThreeChannelMatrix getThreeMatrix(String fileURL) throws Exception { + public static ThreeChannelMatrix getThreeMatrix(String fileURL, boolean vertical) throws Exception { File file = new File(fileURL); BufferedImage bi = null; try { @@ -86,7 +78,7 @@ public class Picture { } catch (Exception e) { e.printStackTrace(); } - return getThreeChannel(bi); + return getThreeChannel(bi, vertical); } // @@ -114,11 +106,7 @@ public class Picture { return matrix; } - private static ThreeChannelMatrix getThreeChannel(BufferedImage bi) throws Exception { - return getThreeChannel(bi,false); - } - - private static ThreeChannelMatrix getThreeChannel(BufferedImage bi,boolean vertical) throws Exception { + private static ThreeChannelMatrix getThreeChannel(BufferedImage bi, boolean vertical) throws Exception { //最大宽度 int width = bi.getWidth(); //最大高度 diff --git a/src/main/java/org/dromara/easyai/yolo/FastYolo.java b/src/main/java/org/dromara/easyai/yolo/FastYolo.java index eb2079e..45f8b92 100644 --- a/src/main/java/org/dromara/easyai/yolo/FastYolo.java +++ b/src/main/java/org/dromara/easyai/yolo/FastYolo.java @@ -258,9 +258,8 @@ public class FastYolo {//yolo List yoloBodies = yoloSample.getYoloBodies();//集合 List boxes = getBoxes(yoloBodies); String url = yoloSample.getLocationURL();//地址 - Picture picture = new Picture(); NMS nms = new NMS(yoloConfig.getIouTh()); - ThreeChannelMatrix pic = picture.getThreeMatrix(url); + ThreeChannelMatrix pic = Picture.getThreeMatrix(url, false); List yoloMessageList = new ArrayList<>(); double stepReduce = yoloConfig.getStepReduce(); int stepX = (int) (winHeight * stepReduce);