添加矩阵PHSH元素,矩阵笛卡尔积

pull/1/head
lidapeng 5 years ago
parent cc7bc710d6
commit bdb39c22d5

@ -1,5 +1,6 @@
package org.wlld.MatrixTools;
import com.sun.org.apache.bcel.internal.generic.FADD;
import org.wlld.tools.ArithUtil;
public class MatrixOperation {
@ -23,6 +24,91 @@ 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) {//增加一行
if (matrix.getY() != myMatrix.getY()) {
throw new Exception("this matrix column is not equals");
}
addMatrix = new Matrix(myMatrix.getX() + 1, myMatrix.getY());
} else {//增加一列
if (matrix.getX() != myMatrix.getX()) {
throw new Exception("this matrix row is not equals");
}
addMatrix = new Matrix(myMatrix.getX(), myMatrix.getY() + 1);
}
for (int i = 0; i < addMatrix.getX(); i++) {
for (int j = 0; j < addMatrix.getY(); j++) {
if (addRow) {
if (i == addMatrix.getX() - 1) {//最后一行
addMatrix.setNub(i, j, matrix.getNumber(0, j));
} else {
addMatrix.setNub(i, j, myMatrix.getNumber(i, j));
}
} else {
if (j == addMatrix.getY() - 1) {//最后一列
addMatrix.setNub(i, j, matrix.getNumber(i, 0));
} else {
addMatrix.setNub(i, j, myMatrix.getNumber(i, j));
}
}
}
}
return addMatrix;
} else {
throw new Exception("this matrix is not a vector");
}
}
public static Matrix push(Matrix matrix, double nub) throws Exception {//向一个向量里PUSH一个值
if (matrix.getX() == 1 || matrix.getY() == 1) {
Matrix myMatrix;
int nubs;
boolean isRow = true;
if (matrix.getX() == 1) {//行向量
nubs = matrix.getY() + 1;
myMatrix = new Matrix(1, nubs);
} else {//列向量
isRow = false;
nubs = matrix.getX() + 1;
myMatrix = new Matrix(nubs, 1);
}
for (int i = 0; i < nubs; i++) {
if (i == nubs - 1) {
if (isRow) {
myMatrix.setNub(0, i, nub);
} else {
myMatrix.setNub(i, 0, nub);
}
} else {
if (isRow) {//行向量
myMatrix.setNub(0, i, matrix.getNumber(0, i));
} else {//列向量
myMatrix.setNub(i, 0, matrix.getNumber(i, 0));
}
}
}
return myMatrix;
} else {
throw new Exception("this matrix is not a vector");
}
}
public static Matrix matrixToRow(Matrix matrix) throws Exception {//将一个矩阵转成行向量
int x = matrix.getX();
int y = matrix.getY();
Matrix myMatrix = new Matrix(1, x * y);
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));
t++;
}
}
return myMatrix;
}
public static double innerProduct(Matrix matrix1, Matrix matrix2) throws Exception {//两个向量内积
Matrix matrix = transPosition(matrix1);
Matrix matrix3 = mulMatrix(matrix, matrix2);

@ -10,16 +10,16 @@ import org.wlld.config.Kernel;
* @date 9:23 2020/1/2
*/
public class Convolution {
public Matrix getFeatures(Matrix matrix, int maxNub) throws Exception {
public Matrix getFeatures(Matrix matrix, int maxNub, boolean isBorder) throws Exception {
do {
matrix = convolution(matrix, Kernel.ALL_Two);
matrix = convolution(matrix, Kernel.ALL_Two, isBorder);
}
while (matrix.getX() > maxNub && matrix.getY() > maxNub);
while (matrix.getX() > maxNub && matrix.getY() > maxNub && !isBorder);
//已经不可以再缩小了,最后做一层卷积,然后提取最大值
return matrix;
}
private Matrix convolution(Matrix matrix, Matrix kernel) throws Exception {
private Matrix convolution(Matrix matrix, Matrix kernel, boolean isBorder) throws Exception {
int x = matrix.getX() - 2;//求导后矩阵的行数
int y = matrix.getY() - 2;//求导后矩阵的列数
Matrix myMatrix = new Matrix(x, y);//最终合成矩阵
@ -31,8 +31,13 @@ public class Convolution {
}
}
}
return late(myMatrix);
if (isBorder) {
return myMatrix;
} else {
return late(myMatrix);
}
}
public Matrix late(Matrix matrix) throws Exception {//迟化处理
int xn = matrix.getX();
int yn = matrix.getY();

@ -19,7 +19,7 @@ public class Operation {//进行计算
this.templeConfig = templeConfig;
}
public List<Double> convolution(Matrix matrix) throws Exception {
public List<Double> convolution(Matrix matrix, boolean isBorder) throws Exception {
//进行卷积
int maxNub = 0;
if (templeConfig.getRow() >= templeConfig.getColumn()) {
@ -27,14 +27,14 @@ public class Operation {//进行计算
} else {
maxNub = templeConfig.getColumn();
}
Matrix matrix1 = convolution.getFeatures(matrix, maxNub);
Matrix matrix1 = convolution.getFeatures(matrix, maxNub, isBorder);
return sub(matrix1);
}
//模板学习
public void study(Matrix matrix, Map<Integer, Double> tagging) throws Exception {
if (templeConfig.getStudyPattern() == StudyPattern.Speed_Pattern) {
List<Double> list = convolution(matrix);
List<Double> list = convolution(matrix, templeConfig.isHavePosition());
intoNerve(1, list, templeConfig.getSensoryNerves(), true, tagging);
} else {
throw new Exception("pattern is wrong");
@ -58,7 +58,7 @@ public class Operation {//进行计算
//图像视觉 speed 模式
public void look(Matrix matrix, long eventId) throws Exception {
if (templeConfig.getStudyPattern() == StudyPattern.Speed_Pattern) {
List<Double> list = convolution(matrix);
List<Double> list = convolution(matrix, templeConfig.isHavePosition());
intoNerve(eventId, list, templeConfig.getSensoryNerves(), false, null);
} else if (templeConfig.getStudyPattern() == StudyPattern.Accuracy_Pattern) {
see(matrix, eventId);

@ -25,6 +25,50 @@ public class TempleConfig {
private int classificationNub = 1;//分类的数量
private int studyPattern;//学习模式
private OutBack outBack;
private boolean isHavePosition = false;//是否需要锁定物体位置
private int region = 10;//将一个图片均匀分成几个区域 默认是10
private int regionRow = 8;//目标检测物体大概会在图像中占的行份
private int regionColumn = 8;//目标检测物体大概会在图像中占的列份
public int getRegionRow() {
return regionRow;
}
public void setRegionRow(int regionRow) {
this.regionRow = regionRow;
}
public int getRegionColumn() {
return regionColumn;
}
public void setRegionColumn(int regionColumn) {
this.regionColumn = regionColumn;
}
public NerveManager getNerveManager() {
return nerveManager;
}
public void setNerveManager(NerveManager nerveManager) {
this.nerveManager = nerveManager;
}
public int getRegion() {
return region;
}
public void setRegion(int region) {
this.region = region;
}
public boolean isHavePosition() {
return isHavePosition;
}
public void setHavePosition(boolean havePosition) {
isHavePosition = havePosition;
}
public NerveManager getConvolutionNerveManager() {
return convolutionNerveManager;

@ -0,0 +1,67 @@
package org.wlld.imageRecognition.border;
import org.wlld.MatrixTools.Matrix;
import org.wlld.imageRecognition.TempleConfig;
import org.wlld.test.Ma;
import org.wlld.tools.ArithUtil;
/**
* @author lidapeng
* @description
* @date 10:37 2020/1/22
*/
public class Border {
private int minX;
private int minY;
private int maxX;
private int maxY;
private int width;//宽度
private int height;//高度
private double modelX;//标准左上角X坐标
private double modelY;//标准左上角Y坐标
private double modelHeight;//标准高度
private double modelWidth;//标准宽度
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);
}
//输入像素值查找边框四角坐标
public void setPosition(int x, int y) {
if (minX > x || minX == 0) {
minX = x;
}
if (minY > y || minY == 0) {
minY = y;
}
if (x > maxX) {
maxX = x;
}
if (y > maxY) {
maxY = y;
}
}
public void end(Matrix matrix) {//长宽
height = maxX - minX;
width = maxY - minY;
//多元线性回归的四个输出值
double tx = ArithUtil.div(ArithUtil.sub(minX, modelX), modelHeight);
double ty = ArithUtil.div(ArithUtil.sub(minY, modelY), modelWidth);
double tw = Math.log(ArithUtil.div(width, modelWidth));
double th = Math.log(ArithUtil.div(height, modelHeight));
//进行参数汇集
}
}
Loading…
Cancel
Save