!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 {
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 {//标准化

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 {
if (parameter.getX() == out.getX() && out.isVector()) {

View File

@ -11,9 +11,14 @@ 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 +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);
BufferedImage image2 = ImageIO.read(file);
int width = image2.getWidth();
@ -55,7 +60,21 @@ 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 {
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();
@ -70,19 +89,25 @@ 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);
}
}
ByteArrayOutputStream ar = new ByteArrayOutputStream();
ImageIO.write(bi, "PNG", ar);
return ar;
return bi;
}
}

View File

@ -8,17 +8,20 @@ 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 +32,45 @@ public class Picture {
return getImage(bi);
}
public ThreeChannelMatrix getThreeMatrix(File file) throws Exception {
public static Matrix getImageMatrixByFile(File file) throws Exception {
BufferedImage bi = null;
try {
bi = ImageIO.read(file);
} catch (Exception e) {
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;
try {
bi = ImageIO.read(file);
} catch (Exception e) {
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);
BufferedImage bi = null;
try {
@ -57,11 +78,11 @@ public class Picture {
} catch (Exception e) {
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;
try {
bi = ImageIO.read(inputStream);
@ -71,11 +92,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 +106,26 @@ 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, 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 +133,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 +153,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 +161,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;
}
}

View File

@ -258,9 +258,8 @@ public class FastYolo {//yolo
List<YoloBody> yoloBodies = yoloSample.getYoloBodies();//集合
List<Box> 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<YoloMessage> yoloMessageList = new ArrayList<>();
double stepReduce = yoloConfig.getStepReduce();
int stepX = (int) (winHeight * stepReduce);