!134 工具类静态化

Merge pull request !134 from fushoujiang/test
This commit is contained in:
唯一解 2024-11-27 13:14:59 +00:00 committed by Gitee
commit bfbd325eb9
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 127 additions and 79 deletions

View File

@ -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 { public Matrix getLBPMatrix() throws Exception {
Matrix addMatrix = matrixOperation.add(matrixOperation.add(matrixR, matrixG), matrixB); Matrix addMatrix = new Matrix(x, y);
Matrix matrix = new Matrix(x / 3, y / 3); for (int i = 0; i < x; i++) {
for (int i = 0; i <= x - 3; i += 3) { for (int j = 0; j < y; j++) {
for (int j = 0; j <= y - 3; j += 3) { double value = matrixR.getNumber(i, j) + matrixG.getNumber(i, j) + matrixB.getNumber(i, j);
Matrix aMatrix = addMatrix.getSonOfMatrix(i, j, 3, 3); addMatrix.setNub(i, j, value);
int lbp = getLBPValue(aMatrix);
matrix.setNub(i / 3, j / 3, lbp);
} }
} }
return matrix; return matrixOperation.lbpMatrix(addMatrix);
} }
public void standardization() throws Exception {//标准化 public void standardization() throws Exception {//标准化

View File

@ -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 { public Matrix getLinearRegression(Matrix parameter, Matrix out) throws Exception {
if (parameter.getX() == out.getX() && out.isVector()) { if (parameter.getX() == out.getX() && out.isVector()) {

View File

@ -11,9 +11,14 @@ import java.awt.image.BufferedImage;
import java.io.*; import java.io.*;
import java.util.List; import java.util.List;
/**
* 图片输出工具类
*
* @author lidapeng
*/
public class ImageTools { public class ImageTools {
public void writeImage(ThreeChannelMatrix img, String url) { public static void writeImage(ThreeChannelMatrix img, String url) {
ByteArrayOutputStream b = null; ByteArrayOutputStream b = null;
FileOutputStream fileOutputStream = null; FileOutputStream fileOutputStream = null;
try { try {
@ -36,7 +41,7 @@ public class ImageTools {
} }
} }
public void drawBox(String fileURL, List<OutBox> borderFoods, String outFileName, int fontSize) throws Exception { public static void drawBox(String fileURL, List<OutBox> borderFoods, String outFileName, int fontSize) throws Exception {
File file = new File(fileURL); File file = new File(fileURL);
BufferedImage image2 = ImageIO.read(file); BufferedImage image2 = ImageIO.read(file);
int width = image2.getWidth(); int width = image2.getWidth();
@ -55,7 +60,21 @@ public class ImageTools {
ImageIO.write(bi, "jpg", new FileOutputStream(outFileName)); ImageIO.write(bi, "jpg", new FileOutputStream(outFileName));
} }
public ByteArrayOutputStream drawImage(ThreeChannelMatrix img) throws Exception { 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 matrixR = img.getMatrixR();
Matrix matrixG = img.getMatrixG(); Matrix matrixG = img.getMatrixG();
Matrix matrixB = img.getMatrixB(); Matrix matrixB = img.getMatrixB();
@ -70,19 +89,25 @@ public class ImageTools {
int b = (int) (matrixB.getNumber(i, j) * 255D); int b = (int) (matrixB.getNumber(i, j) * 255D);
if (r > 255) { if (r > 255) {
r = 255; r = 255;
} else if (r < 0) {
r = 0;
} }
if (g > 255) { if (g > 255) {
g = 255; g = 255;
} else if (g < 0) {
g = 0;
} }
if (b > 255) { if (b > 255) {
b = 255; b = 255;
} else if (b < 0) {
b = 0;
} }
g2.setColor(new Color(r, g, b)); g2.setColor(new Color(r, g, b));
g2.drawRect(j, i, 1, 1); g2.drawRect(j, i, 1, 1);
} }
} }
ByteArrayOutputStream ar = new ByteArrayOutputStream(); return bi;
ImageIO.write(bi, "PNG", ar);
return ar;
} }
} }

View File

@ -8,17 +8,20 @@ import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.InputStream; import java.io.InputStream;
/**
* @author lidapeng
* 图片工具类
*/
public class Picture { public class Picture {
private int pictureWidth;
private int pictureHeight;
private boolean vertical = false;
public void vertical() {//强制竖向图 /**
vertical = true; * 从本地文件拿出图像矩阵
} *
* @param fileURL 图片本地地址
//从本地文件拿出图像矩阵 * @return Matrix
public Matrix getImageMatrixByLocal(String fileURL) throws Exception { * @throws Exception
*/
public static Matrix getImageMatrixByLocal(String fileURL) throws Exception {
File file = new File(fileURL); File file = new File(fileURL);
BufferedImage bi = null; BufferedImage bi = null;
try { try {
@ -29,27 +32,45 @@ public class Picture {
return getImage(bi); return getImage(bi);
} }
public ThreeChannelMatrix getThreeMatrix(File file) throws Exception { public static Matrix getImageMatrixByFile(File file) throws Exception {
BufferedImage bi = null; BufferedImage bi = null;
try { try {
bi = ImageIO.read(file); bi = ImageIO.read(file);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return this.getThreeChannel(bi); return getImage(bi);
} }
public ThreeChannelMatrix getThreeMatrix(InputStream file) throws Exception { /**
* 获取图片的RGB三通道矩阵
*
* @param file 文件
* @param vertical 是否强制竖直
* @return threeChannelMatrix
* @throws Exception
*/
public static ThreeChannelMatrix getThreeMatrix(File file, boolean vertical) throws Exception {
BufferedImage bi = null; BufferedImage bi = null;
try { try {
bi = ImageIO.read(file); bi = ImageIO.read(file);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return this.getThreeChannel(bi); return getThreeChannel(bi, vertical);
} }
public ThreeChannelMatrix getThreeMatrix(String fileURL) 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, vertical);
}
public static ThreeChannelMatrix getThreeMatrix(String fileURL, boolean vertical) throws Exception {
File file = new File(fileURL); File file = new File(fileURL);
BufferedImage bi = null; BufferedImage bi = null;
try { try {
@ -57,11 +78,11 @@ public class Picture {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return getThreeChannel(bi); return getThreeChannel(bi, vertical);
} }
// //
public Matrix getImageMatrixByIo(InputStream inputStream) throws Exception { public static Matrix getImageMatrixByIo(InputStream inputStream) throws Exception {
BufferedImage bi = null; BufferedImage bi = null;
try { try {
bi = ImageIO.read(inputStream); bi = ImageIO.read(inputStream);
@ -71,11 +92,9 @@ public class Picture {
return getImage(bi); return getImage(bi);
} }
private Matrix getImage(BufferedImage bi) throws Exception { private static Matrix getImage(BufferedImage bi) throws Exception {
int width = bi.getWidth();//最大宽度 int width = bi.getWidth();//最大宽度
int height = bi.getHeight();//最大高度 int height = bi.getHeight();//最大高度
pictureWidth = width;
pictureHeight = height;
Matrix matrix = new Matrix(height, width);// Matrix matrix = new Matrix(height, width);//
for (int i = 0; i < height; i++) { for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) { for (int j = 0; j < width; j++) {
@ -87,21 +106,26 @@ public class Picture {
return matrix; return matrix;
} }
private ThreeChannelMatrix getThreeChannel(BufferedImage bi) throws Exception { private static ThreeChannelMatrix getThreeChannel(BufferedImage bi, boolean vertical) throws Exception {
int width = bi.getWidth();//最大宽度 //最大宽度
int height = bi.getHeight();//最大高度 int width = bi.getWidth();
//最大高度
int height = bi.getHeight();
boolean rotate = false; boolean rotate = false;
if (vertical && width > height) { if (vertical && width > height) {
rotate = true; rotate = true;
width = bi.getHeight();//最大宽度 //最大宽度
height = bi.getWidth();//最大高度 width = bi.getHeight();
//最大高度
height = bi.getWidth();
} }
ThreeChannelMatrix threeChannelMatrix = new ThreeChannelMatrix(); ThreeChannelMatrix threeChannelMatrix = new ThreeChannelMatrix();
threeChannelMatrix.setX(height); threeChannelMatrix.setX(height);
threeChannelMatrix.setY(width); threeChannelMatrix.setY(width);
Matrix matrixR = new Matrix(height, width);// //
Matrix matrixG = new Matrix(height, width);// Matrix matrixR = new Matrix(height, width);
Matrix matrixB = new Matrix(height, width);// Matrix matrixG = new Matrix(height, width);
Matrix matrixB = new Matrix(height, width);
Matrix matrixH = new Matrix(height, width); Matrix matrixH = new Matrix(height, width);
threeChannelMatrix.setMatrixR(matrixR); threeChannelMatrix.setMatrixR(matrixR);
threeChannelMatrix.setMatrixG(matrixG); threeChannelMatrix.setMatrixG(matrixG);
@ -109,15 +133,17 @@ public class Picture {
threeChannelMatrix.setH(matrixH); threeChannelMatrix.setH(matrixH);
for (int i = 0; i < height; i++) { for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) { for (int j = 0; j < width; j++) {
int pixel;// 下面三行代码将一个数字转换为RGB数字 // 下面三行代码将一个数字转换为RGB数字
int pixel;
if (rotate) { if (rotate) {
pixel = bi.getRGB(i, j);// 下面三行代码将一个数字转换为RGB数字 // 下面三行代码将一个数字转换为RGB数字
pixel = bi.getRGB(i, j);
} else { } else {
pixel = bi.getRGB(j, i); pixel = bi.getRGB(j, i);
} }
int r = (pixel & 0xff0000) >> 16;//R int r = (pixel & 0xff0000) >> 16;
int g = (pixel & 0xff00) >> 8;//G int g = (pixel & 0xff00) >> 8;
int b = (pixel & 0xff);//B int b = (pixel & 0xff);
matrixR.setNub(i, j, r / 255D); matrixR.setNub(i, j, r / 255D);
matrixG.setNub(i, j, g / 255D); matrixG.setNub(i, j, g / 255D);
matrixB.setNub(i, j, b / 255D); matrixB.setNub(i, j, b / 255D);
@ -127,7 +153,7 @@ public class Picture {
return threeChannelMatrix; return threeChannelMatrix;
} }
private double dimensionReduction(int pixel) {//提取灰度进行降维 private static double dimensionReduction(int pixel) {//提取灰度进行降维
int r = (pixel & 0xff0000) >> 16;//R int r = (pixel & 0xff0000) >> 16;//R
int g = (pixel & 0xff00) >> 8;//G int g = (pixel & 0xff00) >> 8;//G
int b = (pixel & 0xff);//B int b = (pixel & 0xff);//B
@ -135,19 +161,5 @@ public class Picture {
return gray; 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;
}
} }

View File

@ -258,9 +258,8 @@ public class FastYolo {//yolo
List<YoloBody> yoloBodies = yoloSample.getYoloBodies();//集合 List<YoloBody> yoloBodies = yoloSample.getYoloBodies();//集合
List<Box> boxes = getBoxes(yoloBodies); List<Box> boxes = getBoxes(yoloBodies);
String url = yoloSample.getLocationURL();//地址 String url = yoloSample.getLocationURL();//地址
Picture picture = new Picture();
NMS nms = new NMS(yoloConfig.getIouTh()); NMS nms = new NMS(yoloConfig.getIouTh());
ThreeChannelMatrix pic = picture.getThreeMatrix(url); ThreeChannelMatrix pic = Picture.getThreeMatrix(url, false);
List<YoloMessage> yoloMessageList = new ArrayList<>(); List<YoloMessage> yoloMessageList = new ArrayList<>();
double stepReduce = yoloConfig.getStepReduce(); double stepReduce = yoloConfig.getStepReduce();
int stepX = (int) (winHeight * stepReduce); int stepX = (int) (winHeight * stepReduce);