分水岭修改

pull/25/head
thenk008 5 years ago
parent 201139e03b
commit 06acc55456

@ -10,6 +10,7 @@ public class Kernel {
private static final String All_Number2 = "[-1,0,-1]#[0,4,0]#[-1,0,-1]#";
public static final int rainNub = 10;//分水岭初始降雨点的数量
public static final int th = 150;//分水岭灰度阈值
public static final double rgbN = 441.6729559300637;//RGB范数归一化最大值
public static Matrix Vertical;
public static Matrix Horizontal;
public static Matrix All;

@ -40,7 +40,7 @@ public class Convolution extends Frequency {
return matrix;
}
private List<ThreeChannelMatrix> regionThreeChannelMatrix(ThreeChannelMatrix threeChannelMatrix, int size) {
private List<ThreeChannelMatrix> regionThreeChannelMatrix(ThreeChannelMatrix threeChannelMatrix, int size) {
List<ThreeChannelMatrix> threeChannelMatrixList = new ArrayList<>();
Matrix matrixRAll = threeChannelMatrix.getMatrixR();
Matrix matrixGAll = threeChannelMatrix.getMatrixG();
@ -63,17 +63,17 @@ public class Convolution extends Frequency {
}
// public static void main(String[] args) throws Exception {
// Picture picture = new Picture();
// for (int i = 1; i < 10; i++) {
// ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix("D:\\share\\cai/a" + i + ".jpg");
// ThreeChannelMatrix threeChannelMatrix2 = picture.getThreeMatrix("D:\\share\\cai/b" + i + ".jpg");
// kc(threeChannelMatrix1, 2, 3, 40);
// kc(threeChannelMatrix2, 2, 3, 40);
// System.out.println("============================================");
// }
// Picture picture = new Picture();//imageTrance
// ThreeChannelMatrix threeChannelMatrix = picture.getThreeMatrix("D:\\share/a.png");
// ThreeChannelMatrix threeChannelMatrix2 = picture.getThreeMatrix("D:\\share/b.png");
// XYBody xyBody = imageTrance(threeChannelMatrix.getMatrixRGB(), 40);
// XYBody xyBody2 = imageTrance(threeChannelMatrix2.getMatrixRGB(), 40);
// regression(xyBody);
// RegressionBody regressionBody = xyBody.getRegressionBody();
// regressionBody.getMaxDis(xyBody2.getY(), xyBody2.getX());//av==0.8493586867
// }
public List<List<Double>> kAvg(ThreeChannelMatrix threeMatrix, int poolSize, int sqNub
public List<List<Double>> kAvg(ThreeChannelMatrix threeMatrix, int poolSize, int sqNub
, int regionSize) throws Exception {
RGBSort rgbSort = new RGBSort();
List<List<Double>> features = new ArrayList<>();
@ -111,13 +111,13 @@ public class Convolution extends Frequency {
for (int t = 0; t < dm.length; t++) {
dm[t] = rgbNorms.get(t).getNorm();
}
System.out.println(Arrays.toString(dm));
//System.out.println(Arrays.toString(dm));
features.add(feature);
}
return features;
}
public List<List<Double>> kc(ThreeChannelMatrix threeChannelMatrix, int poolSize, int sqNub
public List<List<Double>> kc(ThreeChannelMatrix threeChannelMatrix, int poolSize, int sqNub
, int regionSize) throws Exception {
Matrix matrixR = threeChannelMatrix.getMatrixR();
Matrix matrixG = threeChannelMatrix.getMatrixG();
@ -214,36 +214,44 @@ public class Convolution extends Frequency {
return list;
}
public List<List<Double>> imageTrance(Matrix matrix, int size, int featureNub) throws Exception {//矩阵和卷积核大小
private void regression(XYBody xyBody) {
//计算当前图形的线性回归
RegressionBody regressionBody = new RegressionBody();
regressionBody.lineRegression(xyBody.getY(), xyBody.getX(),this);
xyBody.setRegressionBody(regressionBody);
}
public XYBody imageTrance(Matrix matrix, int size) throws Exception {//矩阵和卷积核大小
int xn = matrix.getX();
int yn = matrix.getY();
int xSize = xn / size;//求导后矩阵的行数
int ySize = yn / size;//求导后矩阵的列数
double[] Y = new double[xSize * ySize];
double[] X = new double[xSize * ySize];
double rgbN = Kernel.rgbN;
for (int i = 0; i < xn - size; i += size) {
for (int j = 0; j < yn - size; j += size) {
Matrix matrix1 = matrix.getSonOfMatrix(i, j, size, size);
double[] nubs = new double[size * size];//平均值数组
for (int t = 0; t < size; t++) {
for (int k = 0; k < size; k++) {
double nub = matrix1.getNumber(t, k) / 255;
double nub = matrix1.getNumber(t, k) / rgbN;
nubs[t * size + k] = nub;
}
}
double avg = average(nubs);//平均值
double dc = dcByAvg(nubs, avg);//当前离散系数
//double va = varianceByAve(nubs, avg);//方差
//double dc = frequency.dcByAvg(nubs, avg);//当前离散系数
double va = varianceByAve(nubs, avg);//方差
//离散系数作为XAVG作为Y
int t = i / size * ySize + j / size;
Y[t] = avg;
X[t] = dc;
X[t] = va;
}
}
//计算当前图形的线性回归
RegressionBody regressionBody = new RegressionBody();
regressionBody.lineRegression(Y, X, this);
return regressionBody.mappingMatrix(featureNub);
XYBody xyBody = new XYBody();
xyBody.setX(X);
xyBody.setY(Y);
return xyBody;
}
@ -325,7 +333,7 @@ public class Convolution extends Frequency {
return myMatrix;
}
protected Matrix late(Matrix matrix, int size) throws Exception {//迟化处理
protected Matrix late(Matrix matrix, int size) throws Exception {//迟化处理
int xn = matrix.getX();
int yn = matrix.getY();
int x = xn / size;//求导后矩阵的行数

@ -57,78 +57,6 @@ public class Operation {//进行计算
return sub(matrix1);
}
public void coverStudyOne(Map<Integer, ThreeChannelMatrix> matrixMap, int regionSize) throws Exception {//先学卷积核
Frame frame = new Frame();
frame.setHeight(regionSize);
frame.setWidth(regionSize);
frame.setLengthHeight(regionSize);
frame.setLengthWidth(regionSize);
List<SensoryNerve> sensoryNerve = templeConfig.getConvolutionNerveManager().getSensoryNerves();
for (Map.Entry<Integer, ThreeChannelMatrix> entry : matrixMap.entrySet()) {
Matrix matrix = entry.getValue().getH();
List<FrameBody> frameBodies = convolution.getRegion(matrix, frame);
int key = entry.getKey();
int size = frameBodies.size();
for (int i = 0; i < size; i++) {
FrameBody frameBody = frameBodies.get(i);
intoConvolutionNetwork(1, frameBody.getMatrix(), sensoryNerve, true, key, null);
}
}
}
public void coverStudyTwo(Map<Integer, ThreeChannelMatrix> matrixMap, int poolSize, int sqNub, int regionSize,
int times) throws Exception {
int size = templeConfig.getSensoryNerves().size();
List<SensoryNerve> sensoryNerve = templeConfig.getConvolutionNerveManager().getSensoryNerves();
Frame frame = new Frame();
frame.setHeight(regionSize);
frame.setWidth(regionSize);
frame.setLengthHeight(regionSize);
frame.setLengthWidth(regionSize);
List<CoverBody> coverBodies = new ArrayList<>();
for (Map.Entry<Integer, ThreeChannelMatrix> entry : matrixMap.entrySet()) {
List<FrameBody> frameBodies = convolution.getRegion(entry.getValue().getH(), frame);
int key = entry.getKey();
int sizeA = frameBodies.size();
List<List<Double>> featureList = new ArrayList<>();
for (int i = 0; i < sizeA; i++) {
FrameBody frameBody = frameBodies.get(i);
MatrixBack matrixBack = new MatrixBack();
intoConvolutionNetwork(1, frameBody.getMatrix(), sensoryNerve, false, 0, matrixBack);
Matrix matrix1 = matrixBack.getMatrix();
List<Double> feature = getFeatures(matrix1);
featureList.add(feature);
}
CoverBody coverBody = new CoverBody();
Map<Integer, Double> tag = new HashMap<>();
tag.put(entry.getKey(), 1.0);
//聚类RGB特征
List<List<Double>> lists = convolution.kAvg(entry.getValue(), poolSize, sqNub, regionSize);
coverBody.setcFeature(featureList);
coverBody.setFeature(lists);
coverBody.setTag(tag);
coverBodies.add(coverBody);
}
for (int j = 0; j < times; j++) {
for (int i = 0; i < size; i++) {
List<Double> list;
for (CoverBody coverBody : coverBodies) {
List<Double> list1 = coverBody.getFeature().get(i);
List<Double> list2 = coverBody.getcFeature().get(i);
if (i < list1.size()) {
list = list1;
} else {
list = list2;
}
if (templeConfig.isShowLog()) {
System.out.println("feature:" + list);
}
intoDnnNetwork(1, list, templeConfig.getSensoryNerves(), true, coverBody.getTag(), null);
}
}
}
}
public void coverStudy(Map<Integer, ThreeChannelMatrix> matrixMap, int poolSize, int sqNub, int regionSize,
int times) throws Exception {
if (templeConfig.getStudyPattern() == StudyPattern.Cover_Pattern) {

@ -77,16 +77,23 @@ public class Picture {
ThreeChannelMatrix threeChannelMatrix = new ThreeChannelMatrix();
Matrix matrixR = new Matrix(height, width);//行,列
Matrix matrixG = new Matrix(height, width);//行,列
Matrix matrixB = new Matrix(height, width);//行,列
Matrix matrixB = new Matrix(height, width);//行,
Matrix matrixRGB = new Matrix(height, width);
threeChannelMatrix.setMatrixR(matrixR);
threeChannelMatrix.setMatrixG(matrixG);
threeChannelMatrix.setMatrixB(matrixB);
threeChannelMatrix.setMatrixRGB(matrixRGB);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int pixel = bi.getRGB(j, i);// 下面三行代码将一个数字转换为RGB数字
matrixR.setNub(i, j, (pixel & 0xff0000) >> 16);
matrixG.setNub(i, j, (pixel & 0xff00) >> 8);
matrixB.setNub(i, j, (pixel & 0xff));
int r = (pixel & 0xff0000) >> 16;//R
int g = (pixel & 0xff00) >> 8;//G
int b = (pixel & 0xff);//B
double rgb = Math.sqrt(Math.pow(r, 2) + Math.pow(g, 2) + Math.pow(b, 2));
matrixRGB.setNub(i, j, rgb);
matrixR.setNub(i, j, r);
matrixG.setNub(i, j, g);
matrixB.setNub(i, j, b);
}
}
return threeChannelMatrix;

@ -7,6 +7,15 @@ public class ThreeChannelMatrix {
Matrix matrixG;
Matrix matrixB;
Matrix H;
Matrix matrixRGB;
public Matrix getMatrixRGB() {
return matrixRGB;
}
public void setMatrixRGB(Matrix matrixRGB) {
this.matrixRGB = matrixRGB;
}
public Matrix getH() {
return H;

@ -0,0 +1,33 @@
package org.wlld.imageRecognition;
import org.wlld.imageRecognition.modelEntity.RegressionBody;
public class XYBody {
private double[] X;
private double[] Y;
private RegressionBody regressionBody;
public RegressionBody getRegressionBody() {
return regressionBody;
}
public void setRegressionBody(RegressionBody regressionBody) {
this.regressionBody = regressionBody;
}
public double[] getX() {
return X;
}
public void setX(double[] x) {
X = x;
}
public double[] getY() {
return Y;
}
public void setY(double[] y) {
Y = y;
}
}

@ -9,26 +9,26 @@ import java.util.List;
public class RegressionBody {
private double w;
private double b;
private double[] X;
private double maxDis;//最大距离
public List<List<Double>> mappingMatrix(int size) {
int len = X.length - size;
List<List<Double>> lists = new ArrayList<>();
for (int i = 0; i < len; i += size) {
List<Double> list = new ArrayList<>();
for (int t = i; t < i + size; t++) {
double nub = ArithUtil.add(ArithUtil.mul(w, X[t]), b);
list.add(nub);
public void getMaxDis(double[] Y, double[] X) {//获取当前的最大距离
double allNub = 0;
for (int i = 0; i < X.length; i++) {
double y = ArithUtil.add(ArithUtil.mul(X[i], w), b);
double dis = Math.abs(ArithUtil.sub(Y[i], y));
allNub = ArithUtil.add(allNub, dis);
System.out.println("dis======" + dis);
if (dis > maxDis) {
maxDis = dis;
}
lists.add(list);
}
return lists;
allNub = ArithUtil.div(allNub, X.length);//当前最大值0.1955576793420405,allNub==0.035958733
System.out.println("当前最大值:" + maxDis + ",allNub==" + allNub);
}
public void lineRegression(double[] Y, double[] X, Frequency frequency) {//进行二元线性回归
double avX = frequency.average(X);//平均值
int len = Y.length;
this.X = X;
double wFenZi = 0;
double wFenMu;
double xSigma = 0;//X求和
@ -49,6 +49,5 @@ public class RegressionBody {
bSigma = ArithUtil.add(ArithUtil.sub(y, ArithUtil.mul(w, x)), bSigma);
}
b = ArithUtil.div(bSigma, len);
}
}

@ -2,7 +2,6 @@ package org.wlld.imageRecognition.segmentation;
import org.wlld.MatrixTools.Matrix;
import org.wlld.config.Kernel;
import org.wlld.imageRecognition.border.FrameBody;
import java.util.*;
@ -201,6 +200,9 @@ public class Watershed {
int x = matrix.getX();
int y = matrix.getY();
int size = xSize * ySize;
double minPoint = 1;
int xr = 0;
int yr = 0;
for (int i = 0; i <= x - xSize; i += xSize) {
for (int j = 0; j <= y - ySize; j += ySize) {
Matrix myMatrix = rainfallMap.getSonOfMatrix(i, j, xSize, ySize);
@ -213,9 +215,15 @@ public class Watershed {
}
}
double cover = (double) sigma / (double) size;//降雨率产生剧烈波动时则出现坐标
System.out.println("cover==" + cover + ",x==" + i + ",y==" + j);
if (cover < minPoint) {
xr = i;
yr = j;
//System.out.println("cover==" + cover + ",x==" + i + ",y==" + j);
minPoint = cover;
}
}
}
System.out.println("min==" + minPoint + ",x==" + xr + ",y==" + yr);
}
private int getMinIndex(double[] array, double mySelf) {//获取最小值

@ -34,7 +34,7 @@ public class CoverTest {
// for (int i = 1; i < 100; i++) {
// cover2(operation, "D:\\pic\\6\\a/a" + i + ".jpg");
// }
//test(null, 2, 3, 40, "c", 3);
//test(null, 2, 3, 40, "c", 3);
cover();
}
@ -71,7 +71,7 @@ public class CoverTest {
operation = getModel();
}
for (int i = 1; i < 100; i++) {
String na = "D:\\share\\cai/" + name + i + ".jpg";
String na = "D:\\pic\\test/" + name + i + ".jpg";
//System.out.println("name======================" + na);
ThreeChannelMatrix threeChannelMatrix = picture.getThreeMatrix(na);
Map<Integer, Double> map1 = operation.coverPoint(threeChannelMatrix, poolSize, sqlNub, regionSize);
@ -80,7 +80,7 @@ public class CoverTest {
for (Map.Entry<Integer, Double> entry : map1.entrySet()) {
int key = entry.getKey();
double value = entry.getValue();
// System.out.println("key==" + key + ",value==" + value);
// System.out.println("key==" + key + ",value==" + value);
if (value > point) {
point = value;
id = key;
@ -108,31 +108,26 @@ public class CoverTest {
//初始化模板 注意 width height参数是你训练图片的实际尺寸需要改其他不用动
//创建运算类进行标注
templeConfig.isShowLog(true);
templeConfig.setStudyPoint(0.005);//不动
templeConfig.setStudyPoint(0.01);//不动
templeConfig.setSoftMax(true);
//templeConfig.setDeep(2);
//templeConfig.setHiddenNerveNub(9);
templeConfig.setSensoryNerveNub(4);
templeConfig.setSensoryNerveNub(3);//多出来的
templeConfig.setRzType(RZ.L1);//不动//3 18
templeConfig.setlParam(0.01);//不动
templeConfig.init(StudyPattern.Cover_Pattern, true, 400, 400, 4);
templeConfig.setlParam(0.015);//不动
templeConfig.init(StudyPattern.Cover_Pattern, true, 400, 400, 2);
Operation operation = new Operation(templeConfig);
for (int i = 1; i < 100; i++) {
Map<Integer, ThreeChannelMatrix> matrixMap = new HashMap<>();
ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix("D:\\share\\cai/a" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix2 = picture.getThreeMatrix("D:\\share\\cai/b" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix3 = picture.getThreeMatrix("D:\\share\\cai/c" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix4 = picture.getThreeMatrix("D:\\share\\cai/d" + i + ".jpg");
matrixMap.put(1, threeChannelMatrix1);
matrixMap.put(2, threeChannelMatrix2);
matrixMap.put(3, threeChannelMatrix3);
matrixMap.put(4, threeChannelMatrix4);
operation.coverStudy(matrixMap, 5, 4, 10, 2);
ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix("D:\\pic\\test/b" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix2 = picture.getThreeMatrix("D:\\pic\\test/d" + i + ".jpg");
matrixMap.put(1, threeChannelMatrix1);//桔梗覆盖
matrixMap.put(2, threeChannelMatrix2);//土地
operation.coverStudy(matrixMap, 2, 3, 18, 2);
}
ModelParameter modelParameter = templeConfig.getModel();
String model = JSON.toJSONString(modelParameter);
System.out.println(model);
// test(operation, 2, 3, 40, "d", 4);
test(operation, 2, 3, 18, "d", 2);
}
}

@ -5,6 +5,8 @@ import org.wlld.MatrixTools.Matrix;
import org.wlld.config.Classifier;
import org.wlld.config.RZ;
import org.wlld.config.StudyPattern;
import org.wlld.function.Sigmod;
import org.wlld.function.Tanh;
import org.wlld.imageRecognition.*;
import org.wlld.imageRecognition.segmentation.Watershed;
import org.wlld.nerveEntity.ModelParameter;
@ -19,59 +21,69 @@ public class FoodTest {
public static void rain() throws Exception {//降雨
Picture picture = new Picture();
Matrix matrix = picture.getImageMatrixByLocal("D:\\share\\cai/i1.jpg");
Matrix matrix = picture.getImageMatrixByLocal("D:\\share\\cai/d1.jpg");
Watershed watershed = new Watershed(matrix);
watershed.rainfall();
}
public static void food() throws Exception {
Picture picture = new Picture();
TempleConfig templeConfig = new TempleConfig();
templeConfig.setClassifier(Classifier.DNN);
templeConfig.isShowLog(true);
templeConfig.setSoftMax(true);
templeConfig.setTh(0);
//templeConfig.setMatrixWidth(5);
templeConfig.setStudyPoint(0.02);
templeConfig.setRzType(RZ.L1);
templeConfig.setlParam(0.016);//0.015
Picture picture = new Picture();//创建图片解析类
TempleConfig templeConfig = new TempleConfig();//创建配置模板类
templeConfig.setClassifier(Classifier.DNN);//使用DNN 分类器
//templeConfig.setActiveFunction(new Sigmod());//设置激活函数
templeConfig.setDeep(2);//设置深度 深度神经网络 深度越深速度越慢
//数量越大越准 但是影响量比较小 不绝对 盲试
templeConfig.setHiddenNerveNub(9);//设置隐层神经元数量
templeConfig.isShowLog(true);//输出打印数据
//
//templeConfig.setSoftMax(true);//启用最后一层的SOFTMAX
//templeConfig.setTh(-1);//设置阈值
templeConfig.setStudyPoint(0.012);//设置学习率 0-1
templeConfig.setRzType(RZ.L1);//设置正则函数
templeConfig.setlParam(0.015);//设置正则参数
templeConfig.init(StudyPattern.Accuracy_Pattern, true, 640, 480, 2);
Operation operation = new Operation(templeConfig);
Operation operation = new Operation(templeConfig);//计算类
// 一阶段
for (int j = 0; j < 2; j++) {
for (int i = 1; i < 233; i++) {//一阶段
for (int i = 1; i < 101; i++) {//一阶段
System.out.println("study1===================" + i);
//读取本地URL地址图片,并转化成矩阵
Matrix a = picture.getImageMatrixByLocal("D:\\pic\\6\\a/a" + i + ".jpg");
Matrix b = picture.getImageMatrixByLocal("D:\\pic\\6\\b/b" + i + ".jpg");
Matrix a = picture.getImageMatrixByLocal("D:\\pic\\1/a" + i + ".jpg");
Matrix b = picture.getImageMatrixByLocal("D:\\pic\\2/b" + i + ".jpg");
operation.learning(a, 1, false);
operation.learning(b, 2, false);
}
}
// 二阶段
for (int i = 1; i < 233; i++) {
// 二阶段 归一化
for (int i = 1; i < 101; i++) {
System.out.println("avg==" + i);
Matrix a = picture.getImageMatrixByLocal("D:\\pic\\6\\a/a" + i + ".jpg");
Matrix b = picture.getImageMatrixByLocal("D:\\pic\\6\\b/b" + i + ".jpg");
Matrix a = picture.getImageMatrixByLocal("D:\\pic\\1/a" + i + ".jpg");
Matrix b = picture.getImageMatrixByLocal("D:\\pic\\2/b" + i + ".jpg");
operation.normalization(a, templeConfig.getConvolutionNerveManager());
operation.normalization(b, templeConfig.getConvolutionNerveManager());
}
templeConfig.getNormalization().avg();
for (int j = 0; j < 5; j++) {
for (int i = 1; i < 233; i++) {
for (int j = 0; j < 3; j++) {
for (int i = 1; i < 101; i++) {
System.out.println("j==" + j + ",study2==================" + i);
Matrix a = picture.getImageMatrixByLocal("D:\\pic\\6\\a/a" + i + ".jpg");
Matrix b = picture.getImageMatrixByLocal("D:\\pic\\6\\b/b" + i + ".jpg");
Matrix a = picture.getImageMatrixByLocal("D:\\pic\\1/a" + i + ".jpg");
Matrix b = picture.getImageMatrixByLocal("D:\\pic\\2/b" + i + ".jpg");
operation.learning(a, 1, true);
operation.learning(b, 2, true);
}
}
templeConfig.finishStudy();//结束学习
ModelParameter modelParameter = templeConfig.getModel();
String model = JSON.toJSONString(modelParameter);
System.out.println(model);
int wrong = 0;
int allNub = 0;
for (int i = 1; i <= 232; i++) {
for (int i = 1; i <= 100; i++) {
//读取本地URL地址图片,并转化成矩阵
Matrix a = picture.getImageMatrixByLocal("D:\\pic\\6\\a/a" + i + ".jpg");
Matrix a = picture.getImageMatrixByLocal("D:\\pic\\1/a" + i + ".jpg");
allNub++;
int an = operation.toSee(a);
if (an != 1) {
@ -80,9 +92,7 @@ public class FoodTest {
}
double wrongPoint = ArithUtil.div(wrong, allNub);
System.out.println("错误率:" + (wrongPoint * 100) + "%");
ModelParameter modelParameter = templeConfig.getModel();
String model = JSON.toJSONString(modelParameter);
System.out.println(model);
}
}

Loading…
Cancel
Save