添加矩阵转列向量

pull/1/head
lidapeng 5 years ago
parent 12d2c40764
commit 0b8e030809

@ -24,6 +24,7 @@ public class MatrixOperation {
}
public static Matrix pushVector(Matrix myMatrix, Matrix matrix, boolean addRow) throws Exception {
//向一个矩阵里合并一个行向量或者列向量到矩阵行或者列的末尾
if (matrix.getX() == 1 || matrix.getY() == 1) {
Matrix addMatrix;
if (addRow) {//增加一行
@ -94,14 +95,23 @@ public class MatrixOperation {
}
}
public static Matrix matrixToRow(Matrix matrix) throws Exception {//将一个矩阵转成行向量
public static Matrix matrixToVector(Matrix matrix, boolean isRow) throws Exception {//将一个矩阵转成行向量
int x = matrix.getX();
int y = matrix.getY();
Matrix myMatrix = new Matrix(1, x * y);
Matrix myMatrix;
if (isRow) {
myMatrix = new Matrix(1, x * y);
} else {
myMatrix = new Matrix(x * y, 1);
}
int t = 0;
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
myMatrix.setNub(0, t, matrix.getNumber(i, j));
if (isRow) {
myMatrix.setNub(0, t, matrix.getNumber(i, j));
} else {
myMatrix.setNub(t, 0, matrix.getNumber(i, j));
}
t++;
}
}

@ -58,8 +58,11 @@ public class Operation {//进行计算
//图像视觉 speed 模式
public void look(Matrix matrix, long eventId) throws Exception {
if (templeConfig.getStudyPattern() == StudyPattern.Speed_Pattern) {
long a = System.currentTimeMillis();
List<Double> list = convolution(matrix, templeConfig.isHavePosition());
intoNerve(eventId, list, templeConfig.getSensoryNerves(), false, null);
long b = System.currentTimeMillis();
System.out.println("耗时:" + (b - a));
} else if (templeConfig.getStudyPattern() == StudyPattern.Accuracy_Pattern) {
see(matrix, eventId);
}

@ -5,6 +5,7 @@ import org.wlld.config.StudyPattern;
import org.wlld.function.ReLu;
import org.wlld.function.Sigmod;
import org.wlld.i.OutBack;
import org.wlld.imageRecognition.border.BorderBody;
import org.wlld.nerveCenter.NerveManager;
import org.wlld.nerveEntity.ModelParameter;
import org.wlld.nerveEntity.SensoryNerve;
@ -26,24 +27,14 @@ public class TempleConfig {
private int studyPattern;//学习模式
private OutBack outBack;
private boolean isHavePosition = false;//是否需要锁定物体位置
private int region = 10;//将一个图片均匀分成几个区域 默认是10
private int regionRow = 8;//目标检测物体大概会在图像中占的行份
private int regionColumn = 8;//目标检测物体大概会在图像中占的列份
private Map<Integer, BorderBody> borderBodyMap = new HashMap<>();
public int getRegionRow() {
return regionRow;
public Map<Integer, BorderBody> getBorderBodyMap() {
return borderBodyMap;
}
public void setRegionRow(int regionRow) {
this.regionRow = regionRow;
}
public int getRegionColumn() {
return regionColumn;
}
public void setRegionColumn(int regionColumn) {
this.regionColumn = regionColumn;
public void setBorderBodyMap(Map<Integer, BorderBody> borderBodyMap) {
this.borderBodyMap = borderBodyMap;
}
public NerveManager getNerveManager() {
@ -54,14 +45,6 @@ public class TempleConfig {
this.nerveManager = nerveManager;
}
public int getRegion() {
return region;
}
public void setRegion(int region) {
this.region = region;
}
public boolean isHavePosition() {
return isHavePosition;
}

@ -1,6 +1,7 @@
package org.wlld.imageRecognition.border;
import org.wlld.MatrixTools.Matrix;
import org.wlld.MatrixTools.MatrixOperation;
import org.wlld.imageRecognition.TempleConfig;
import org.wlld.test.Ma;
import org.wlld.tools.ArithUtil;
@ -17,23 +18,14 @@ public class Border {
private int maxY;
private int width;//宽度
private int height;//高度
private double modelX;//标准左上角X坐标
private double modelY;//标准左上角Y坐标
private double modelHeight;//标准高度
private double modelWidth;//标准宽度
private TempleConfig templeConfig;
Border(TempleConfig templeConfig, int imageWidth, int imageHeight) {
int region = templeConfig.getRegion();//将一个区域分成几份
double row = ArithUtil.div(imageHeight, region);//每一行的像素
double column = ArithUtil.div(imageWidth, region);//每一列的像素
int divRow = templeConfig.getRegionRow();//检测物体行占据的份数
int divColumn = templeConfig.getRegionColumn();//检测物体列占据的份数
modelHeight = ArithUtil.mul(divRow, row);
modelWidth = ArithUtil.mul(divColumn, column);
double regionRow = (region - divRow) / 2;//左上角X份数
double regionColumn = (region - divColumn) / 2;//左上角Y份数
modelX = ArithUtil.mul(regionRow, row);
modelY = ArithUtil.mul(regionColumn, column);
modelWidth = imageWidth;
modelHeight = imageHeight;
this.templeConfig = templeConfig;
}
//输入像素值查找边框四角坐标
@ -52,16 +44,20 @@ public class Border {
}
}
public void end(Matrix matrix) {//长宽
public void end(Matrix matrix, int id) throws Exception {//长宽
height = maxX - minX;
width = maxY - minY;
BorderBody borderBody = templeConfig.getBorderBodyMap().get(id);
//多元线性回归的四个输出值
double tx = ArithUtil.div(ArithUtil.sub(minX, modelX), modelHeight);
double ty = ArithUtil.div(ArithUtil.sub(minY, modelY), modelWidth);
double tx = ArithUtil.div(minX, modelHeight);
double ty = ArithUtil.div(minY, modelWidth);
double tw = Math.log(ArithUtil.div(width, modelWidth));
double th = Math.log(ArithUtil.div(height, modelHeight));
//进行参数汇集
//进行参数汇集 矩阵转化为行向量
matrix = MatrixOperation.matrixToVector(matrix, true);
//将矩阵的末尾填1
matrix = MatrixOperation.push(matrix, 1);
}
}

@ -0,0 +1,93 @@
package org.wlld.imageRecognition.border;
import org.wlld.MatrixTools.Matrix;
/**
* @author lidapeng
* @description
*
* @date 8:49 2020/1/24
*/
public class BorderBody {
private Matrix xW;//平移X权重
private Matrix yW;//平移Y权重
private Matrix wW;//缩放宽度权重
private Matrix hW;//缩放高度权重
private Matrix x;//参数矩阵
private Matrix tx;//X轴偏移量
private Matrix ty;//Y轴偏移量
private Matrix tw;//W缩放量
private Matrix th;//H缩放量
public Matrix getxW() {
return xW;
}
public void setxW(Matrix xW) {
this.xW = xW;
}
public Matrix getyW() {
return yW;
}
public void setyW(Matrix yW) {
this.yW = yW;
}
public Matrix getwW() {
return wW;
}
public void setwW(Matrix wW) {
this.wW = wW;
}
public Matrix gethW() {
return hW;
}
public void sethW(Matrix hW) {
this.hW = hW;
}
public Matrix getX() {
return x;
}
public void setX(Matrix x) {
this.x = x;
}
public Matrix getTx() {
return tx;
}
public void setTx(Matrix tx) {
this.tx = tx;
}
public Matrix getTy() {
return ty;
}
public void setTy(Matrix ty) {
this.ty = ty;
}
public Matrix getTw() {
return tw;
}
public void setTw(Matrix tw) {
this.tw = tw;
}
public Matrix getTh() {
return th;
}
public void setTh(Matrix th) {
this.th = th;
}
}

@ -20,8 +20,8 @@ import java.util.Map;
*/
public class HelloWorld {
public static void main(String[] args) throws Exception {
//testPic2();
testModel();
testPic();
//testModel();
}
public static void testPic() throws Exception {
@ -143,7 +143,7 @@ public class HelloWorld {
Map<Integer, Double> wrongTagging = new HashMap<>();//分类标注
rightTagging.put(1, 1.0);
wrongTagging.put(1, 0.0);
for (int i = 1; i < 5; i++) {
for (int i = 1; i < 2; i++) {
System.out.println("开始学习1==" + i);
//读取本地URL地址图片,并转化成矩阵
Matrix right = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/c/c" + i + ".png");
@ -154,7 +154,7 @@ public class HelloWorld {
operation.learning(right, rightTagging, false);
operation.learning(wrong, wrongTagging, false);
}
for (int i = 1; i < 5; i++) {//神经网络学习
for (int i = 1; i < 2; i++) {//神经网络学习
System.out.println("开始学习2==" + i);
//读取本地URL地址图片,并转化成矩阵
Matrix right = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/c/c" + i + ".png");

@ -0,0 +1,44 @@
package org.wlld;
import org.wlld.MatrixTools.Matrix;
import org.wlld.MatrixTools.MatrixOperation;
/**
* @author lidapeng
* @description
* @date 3:35 2020/1/23
*/
public class MatrixTest {
public static void main(String[] args) throws Exception {
test2();
}
public static void test1() throws Exception {
Matrix matrix = new Matrix(2, 2);
Matrix matrix2 = new Matrix(1, 5);
String b = "[6,7,8,9,10]#";
String a = "[1,2]#" +
"[3,4]#";
matrix.setAll(a);
matrix2.setAll(b);
Matrix matrix1 = MatrixOperation.matrixToVector(matrix, true);
matrix1 = MatrixOperation.push(matrix1, 5);
matrix1 = MatrixOperation.pushVector(matrix1, matrix2, true);
System.out.println(matrix1.getString());
}
public static void test2() throws Exception {
Matrix matrix = new Matrix(2, 2);
Matrix matrix1 = new Matrix(1, 5);
String a = "[1,2]#" +
"[3,4]#";
String b = "[6,7,8,9,10]#";
matrix.setAll(a);
matrix1.setAll(b);
matrix1 = MatrixOperation.matrixToVector(matrix1, false);
matrix = MatrixOperation.matrixToVector(matrix, false);
matrix = MatrixOperation.push(matrix, 5);
matrix = MatrixOperation.pushVector(matrix, matrix1, false);
System.out.println(matrix.getString());
}
}
Loading…
Cancel
Save