增加局部纹理聚类

This commit is contained in:
lidapeng 2020-08-16 20:45:08 +08:00
parent 203c05d83b
commit b817746eb6
11 changed files with 112 additions and 112 deletions

View File

@ -6,7 +6,7 @@
<groupId>com.wlld</groupId>
<artifactId>easyAi</artifactId>
<version>1.1.2</version>
<version>1.0.9</version>
<name>easyAi</name>
<!-- FIXME change it to the project's website -->

View File

@ -330,6 +330,7 @@ public class MatrixOperation {
mathMul(myMatrix, def);
return myMatrix;
} else {
System.out.println(matrixs.getString());
throw new Exception("this matrixs do not have InverseMatrixs");
}
}

View File

@ -10,7 +10,7 @@ public class Kernel {
private static final String All_Number2 = "[-1,0,-1]#[0,4,0]#[-1,0,-1]#";
public static final int Region_Nub = 60;//一张图有多少份
public static final double th = 0.88;//分水岭灰度阈值
public static final double rgbN = 442.0;//RGB范数归一化最大值
public static final double rgbN = 442.0;//442.0;//RGB范数归一化最大值
public static Matrix Vertical;
public static Matrix Horizontal;
public static Matrix All;

View File

@ -169,9 +169,9 @@ public class Convolution extends Frequency {
Matrix matrixR = threeChannelMatrix.getMatrixR();
Matrix matrixG = threeChannelMatrix.getMatrixG();
Matrix matrixB = threeChannelMatrix.getMatrixB();
matrixR = late(matrixR, poolSize);
matrixG = late(matrixG, poolSize);
matrixB = late(matrixB, poolSize);
// matrixR = late(matrixR, poolSize);
// matrixG = late(matrixG, poolSize);
// matrixB = late(matrixB, poolSize);
RGBSort rgbSort = new RGBSort();
int x = matrixR.getX();
int y = matrixR.getY();
@ -187,9 +187,9 @@ public class Convolution extends Frequency {
Collections.sort(rgbNorms, rgbSort);
List<Double> features = new ArrayList<>();
for (int i = 0; i < sqNub; i++) {
//double[] rgb = rgbNorms.get(i).getRgb();
RgbRegression rgbRegression = rgbNorms.get(i).getRgbRegression();
double[] rgb = new double[]{rgbRegression.getWr(), rgbRegression.getWg(), rgbRegression.getB()};
double[] rgb = rgbNorms.get(i).getRgb();
// RgbRegression rgbRegression = rgbNorms.get(i).getRgbRegression();
//double[] rgb = new double[]{rgbRegression.getWr(), rgbRegression.getWg(), rgbRegression.getB()};
for (int j = 0; j < 3; j++) {
features.add(rgb[j]);
}
@ -200,44 +200,45 @@ public class Convolution extends Frequency {
return features;
}
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) / rgbN;
nubs[t * size + k] = nub;
public List<Double> getCenterTexture(ThreeChannelMatrix threeChannelMatrix, int size, int poolSize, TempleConfig templeConfig
, int sqNub) throws Exception {
RGBSort rgbSort = new RGBSort();
MeanClustering meanClustering = new MeanClustering(sqNub, templeConfig);
Matrix matrixR = threeChannelMatrix.getMatrixR();
Matrix matrixG = threeChannelMatrix.getMatrixG();
Matrix matrixB = threeChannelMatrix.getMatrixB();
int xn = matrixR.getX();
int yn = matrixR.getY();
for (int i = 0; i <= xn - size; i += size) {
for (int j = 0; j <= yn - size; j += size) {
Matrix sonR = late(matrixR.getSonOfMatrix(i, j, size, size), poolSize);
Matrix sonG = late(matrixG.getSonOfMatrix(i, j, size, size), poolSize);
Matrix sonB = late(matrixB.getSonOfMatrix(i, j, size, size), poolSize);
int tSize = sonR.getX();
int kSize = sonR.getY();
double[] rgb = new double[tSize * kSize * 3];
for (int t = 0; t < tSize; t++) {
for (int k = 0; k < kSize; k++) {
int index = t * kSize + k;
rgb[index] = sonR.getNumber(t, k);
rgb[tSize * kSize + index] = sonG.getNumber(t, k);
rgb[tSize * kSize * 2 + index] = sonB.getNumber(t, k);
}
}
double avg = average(nubs);//平均值
//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] = va;
meanClustering.setColor(rgb);
}
}
XYBody xyBody = new XYBody();
xyBody.setX(X);
xyBody.setY(Y);
return xyBody;
meanClustering.start();//开始聚类
List<RGBNorm> rgbNorms = meanClustering.getMatrices();
Collections.sort(rgbNorms, rgbSort);
List<Double> features = new ArrayList<>();
for (int i = 0; i < sqNub; i++) {
double[] rgb = rgbNorms.get(i).getRgb();
for (int j = 0; j < rgb.length; j++) {
features.add(rgb[j]);
}
}
return features;
}
@ -260,11 +261,13 @@ public class Convolution extends Frequency {
Matrix matrixR = threeChannelMatrix.getMatrixR().getSonOfMatrix(x, y, xSize, ySize);
Matrix matrixG = threeChannelMatrix.getMatrixG().getSonOfMatrix(x, y, xSize, ySize);
Matrix matrixB = threeChannelMatrix.getMatrixB().getSonOfMatrix(x, y, xSize, ySize);
Matrix matrixH = threeChannelMatrix.getH().getSonOfMatrix(x, y, xSize, ySize);
Matrix matrixRGB = threeChannelMatrix.getMatrixRGB().getSonOfMatrix(x, y, xSize, ySize);
threeChannelMatrix1.setMatrixR(matrixR);
threeChannelMatrix1.setMatrixG(matrixG);
threeChannelMatrix1.setMatrixB(matrixB);
threeChannelMatrix1.setH(threeChannelMatrix.getH());
threeChannelMatrix1.setMatrixRGB(threeChannelMatrix.getMatrixRGB());
threeChannelMatrix1.setH(matrixH);
threeChannelMatrix1.setMatrixRGB(matrixRGB);
return threeChannelMatrix1;
}
@ -359,7 +362,7 @@ public class Convolution extends Frequency {
return myMatrix;
}
protected Matrix late(Matrix matrix, int size) throws Exception {//池化处理
public Matrix late(Matrix matrix, int size) throws Exception {//池化处理
int xn = matrix.getX();
int yn = matrix.getY();
int x = xn / size;//求导后矩阵的行数
@ -380,7 +383,7 @@ public class Convolution extends Frequency {
}
}
}
maxNub = ArithUtil.div(sigma, n);
maxNub = sigma / n;
//迟化的最大值是 MAXNUB
myMatrix.setNub(i / size, j / size, maxNub);
}

View File

@ -96,7 +96,7 @@ public class MeanClustering {
for (int i = 0; i < speciesQuantity; i++) {//初始化均值向量
int index = random.nextInt(matrixList.size());
double[] rgb = matrixList.get(index);
RGBNorm rgbNorm = new RGBNorm(rgb);
RGBNorm rgbNorm = new RGBNorm(rgb, length);
//要进行深度克隆
matrices.add(rgbNorm);
}
@ -111,7 +111,7 @@ public class MeanClustering {
break;
}
}
startRegression();//开始进行回归
// startRegression();//开始进行回归
} else {
throw new Exception("matrixList number less than 2");
}

View File

@ -7,7 +7,6 @@ import org.wlld.config.Classifier;
import org.wlld.config.StudyPattern;
import org.wlld.i.OutBack;
import org.wlld.imageRecognition.border.*;
import org.wlld.imageRecognition.modelEntity.TrayBody;
import org.wlld.imageRecognition.segmentation.RegionBody;
import org.wlld.imageRecognition.segmentation.RgbRegression;
import org.wlld.imageRecognition.segmentation.Specifications;
@ -64,10 +63,10 @@ public class Operation {//进行计算
Matrix matrixR = threeChannelMatrix.getMatrixR();
Matrix matrixG = threeChannelMatrix.getMatrixG();
Matrix matrixB = threeChannelMatrix.getMatrixB();
Matrix matrixRGB = threeChannelMatrix.getMatrixRGB();
//Matrix matrixRGB = threeChannelMatrix.getMatrixRGB();
Random random = new Random();
int x = matrixRGB.getX();
int y = matrixRGB.getY();
int x = matrixR.getX();
int y = matrixR.getY();
int size = templeConfig.getFood().getRegressionNub();
RgbRegression rgbRegression = new RgbRegression(size);
for (int i = 0; i < size; i++) {
@ -88,11 +87,9 @@ public class Operation {//进行计算
Matrix matrixR = threeChannelMatrix.getMatrixR();
Matrix matrixG = threeChannelMatrix.getMatrixG();
Matrix matrixB = threeChannelMatrix.getMatrixB();
Matrix matrixRGB = threeChannelMatrix.getMatrixRGB();
threeChannelMatrix.setMatrixR(matrixR.getSonOfMatrix(x, y, xSize, ySize));
threeChannelMatrix.setMatrixG(matrixG.getSonOfMatrix(x, y, xSize, ySize));
threeChannelMatrix.setMatrixB(matrixB.getSonOfMatrix(x, y, xSize, ySize));
threeChannelMatrix.setMatrixRGB(matrixRGB.getSonOfMatrix(x, y, xSize, ySize));
}
public RegionBody colorStudy(ThreeChannelMatrix threeChannelMatrix, int tag, List<Specifications> specificationsList) throws Exception {
@ -107,15 +104,15 @@ public class Operation {//进行计算
int xSize = maxX - minX;
int ySize = maxY - minY;
ThreeChannelMatrix threeChannelMatrix1 = convolution.getRegionMatrix(threeChannelMatrix, minX, minY, xSize, ySize);
//convolution.filtering(threeChannelMatrix1);//光照过滤
int times = templeConfig.getFood().getTimes();
for (int i = 0; i < times; i++) {
List<Double> feature = convolution.getCenterColor(threeChannelMatrix1, templeConfig.getPoolSize(),
templeConfig.getFeatureNub(), templeConfig);
// List<Double> feature = convolution.getCenterColor(threeChannelMatrix1, templeConfig.getPoolSize(),
// templeConfig.getFeatureNub(), templeConfig);
List<Double> feature = convolution.getCenterTexture(threeChannelMatrix1, templeConfig.getFood().getRegionSize(),
templeConfig.getPoolSize(), templeConfig, templeConfig.getFeatureNub());
if (templeConfig.isShowLog()) {
System.out.println(tag + ":" + feature);
}
//System.out.println("=====================================");
int classifier = templeConfig.getClassifier();
switch (classifier) {
case Classifier.DNN:
@ -177,9 +174,10 @@ public class Operation {//进行计算
int xSize = maxX - minX;
int ySize = maxY - minY;
ThreeChannelMatrix threeChannelMatrix1 = convolution.getRegionMatrix(threeChannelMatrix, minX, minY, xSize, ySize);
//convolution.filtering(threeChannelMatrix1);//光照过滤
List<Double> feature = convolution.getCenterColor(threeChannelMatrix1, templeConfig.getPoolSize(),
templeConfig.getFeatureNub(), templeConfig);
// List<Double> feature = convolution.getCenterColor(threeChannelMatrix1, templeConfig.getPoolSize(),
// templeConfig.getFeatureNub(), templeConfig);
List<Double> feature = convolution.getCenterTexture(threeChannelMatrix1, templeConfig.getFood().getRegionSize(),
templeConfig.getPoolSize(), templeConfig, templeConfig.getFeatureNub());
if (templeConfig.isShowLog()) {
System.out.println(feature);
}

View File

@ -7,13 +7,14 @@ import java.util.ArrayList;
import java.util.List;
public class RGBNorm {
private double[] rgbAll = new double[3];
private double[] rgbAll;
private double norm;
private int nub;
private double[] rgb = new double[3];
private double[] rgb;
private double[] rgbUp;
private List<double[]> rgbs = new ArrayList<>();
private RgbRegression rgbRegression;
private int len;
public List<double[]> getRgbs() {
return rgbs;
@ -27,7 +28,10 @@ public class RGBNorm {
this.rgbRegression = rgbRegression;
}
RGBNorm(double[] rgb) {
RGBNorm(double[] rgb, int len) {
this.len = len;
rgbAll = new double[len];
this.rgb = new double[len];
this.rgbUp = rgb;
}
@ -36,12 +40,11 @@ public class RGBNorm {
}
public void clear() {
rgbAll = new double[3];
rgbAll = new double[len];
nub = 0;
for (int i = 0; i < rgb.length; i++) {
rgbUp[i] = rgb[i];
}
rgbs.clear();
//System.out.println("clear==" + Arrays.toString(rgbUp));
}
@ -76,7 +79,6 @@ public class RGBNorm {
for (int i = 0; i < rgb.length; i++) {
rgbAll[i] = rgbAll[i] + rgb[i];
}
rgbs.add(rgb);
nub++;
}

View File

@ -85,7 +85,7 @@ public class Knn {//KNN分类器
compare(dists, types, dist, type);
}
}
System.out.println(Arrays.toString(types));
//System.out.println(Arrays.toString(types));
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nub; i++) {
int type = types[i];

View File

@ -277,13 +277,13 @@ public class Watershed {
regionBodies.add(regionBody);
}
}
for (RegionBody regionBody : regionBodies) {
int minX = regionBody.getMinX();
int maxX = regionBody.getMaxX();
int minY = regionBody.getMinY();
int maxY = regionBody.getMaxY();
System.out.println("minX==" + minX + ",minY==" + minY + ",maxX==" + maxX + ",maxY==" + maxY);
}
// for (RegionBody regionBody : regionBodies) {
// int minX = regionBody.getMinX();
// int maxX = regionBody.getMaxX();
// int minY = regionBody.getMinY();
// int maxY = regionBody.getMaxY();
// System.out.println("minX==" + minX + ",minY==" + minY + ",maxX==" + maxX + ",maxY==" + maxY);
// }
return regionBodies;
}
@ -399,8 +399,8 @@ public class Watershed {
}
private void sigmaPixel() throws Exception {//生成降雨密度图
int x = matrix.getX();
int y = matrix.getY();
// int x = matrix.getX();
// int y = matrix.getY();
int size = xSize * ySize;
for (int i = 0; i < xSize * regionNub; i += xSize) {
for (int j = 0; j < ySize * regionNub; j += ySize) {
@ -429,7 +429,7 @@ public class Watershed {
int minIdx = 0;
for (int i = 0; i < array.length; i++) {
double nub = array[i];
if (nub > -1 && nub < mySelf - rainTh && nub < maxRain) {
if (nub > -1 && nub < mySelf) {
minIdx = minIdx | (1 << i);
}
}

View File

@ -19,6 +19,15 @@ public class Food {
private List<RgbRegression> trayBody = new ArrayList<>();//托盘实体参数
private int regressionNub = 10000;//回归次数
private double trayTh = 0.1;//托盘回归阈值
private int regionSize = 10;//纹理区域大小
public int getRegionSize() {
return regionSize;
}
public void setRegionSize(int regionSize) {
this.regionSize = regionSize;
}
public double getTrayTh() {
return trayTh;

View File

@ -16,35 +16,15 @@ import org.wlld.param.Food;
import org.wlld.tools.ArithUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class FoodTest {
public static void main(String[] args) throws Exception {
// ModelParameter parameter = JSON.parseObject(ModelData.DATA, ModelParameter.class);
// if (parameter.getKnnVector() != null) {
// System.out.println("空的");
// } else {
// System.out.println("不是空===");
// }
test();
}
public static void one(double[] test, double[] right, double[] wrong) {
int nub = 0;
for (int i = 0; i < test.length; i++) {
double test1 = test[i];
double right1 = right[i];
double wrong1 = wrong[i];
double sub1 = Math.abs(ArithUtil.sub(test1, right1));
double sub2 = Math.abs(ArithUtil.sub(test1, wrong1));
if (sub1 > sub2) {
nub++;
}
}
System.out.println(nub);
}
public static void test2(TempleConfig templeConfig) throws Exception {
if (templeConfig == null) {
ModelParameter parameter = JSON.parseObject(ModelData.DATA, ModelParameter.class);
@ -53,22 +33,24 @@ public class FoodTest {
Picture picture = new Picture();
List<Specifications> specificationsList = new ArrayList<>();
Specifications specifications = new Specifications();
specifications.setMinWidth(300);
specifications.setMinHeight(300);
specifications.setMinWidth(100);
specifications.setMinHeight(100);
specifications.setMaxWidth(950);
specifications.setMaxHeight(950);
specificationsList.add(specifications);
Operation operation = new Operation(templeConfig);
for (int i = 1; i <= 28; i++) {
ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix("/Users/lidapeng/Desktop/foodModel/test1/g" + i + ".jpg");
long a = System.currentTimeMillis();
for (int i = 1; i <= 1; i++) {
ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix("/Users/lidapeng/Desktop/test/d.jpg");
List<RegionBody> regionBody = operation.colorLook(threeChannelMatrix1, specificationsList);
long b = System.currentTimeMillis() - a;
System.out.println(b);
for (int j = 0; j < regionBody.size(); j++) {
RegionBody regionBody1 = regionBody.get(j);
System.out.println("minX==" + regionBody1.getMinX() + ",minY==" + regionBody1.getMinY()
+ ",maxX==" + regionBody1.getMaxX() + ",maxY==" + regionBody1.getMaxY());
System.out.println(regionBody.get(j).getType());
System.out.println("type==" + regionBody.get(j).getType());
}
System.out.println("===================================" + i);
}
}
@ -87,10 +69,10 @@ public class FoodTest {
//池化比例
templeConfig.setPoolSize(2);//缩小比例
//聚类
templeConfig.setFeatureNub(3);//聚类特征数量
templeConfig.setFeatureNub(5);//聚类特征数量
//菜品识别实体类
food.setShrink(20);//缩紧像素
food.setTimes(1);//聚类数据增强
food.setTimes(2);//聚类数据增强
food.setRowMark(0.1);//0.12
food.setColumnMark(0.1);//0.25
food.setRegressionNub(20000);
@ -109,21 +91,26 @@ public class FoodTest {
Operation operation = new Operation(templeConfig);
List<Specifications> specificationsList = new ArrayList<>();
Specifications specifications = new Specifications();
specifications.setMinWidth(300);
specifications.setMinHeight(300);
specifications.setMinWidth(100);
specifications.setMinHeight(100);
specifications.setMaxWidth(1000);
specifications.setMaxHeight(1000);
specificationsList.add(specifications);
ThreeChannelMatrix threeChannelMatrix = picture.getThreeMatrix("/Users/lidapeng/Desktop/myDocument/d.jpg");
operation.setTray(threeChannelMatrix);
for (int i = 1; i <= 1; i++) {
ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix("/Users/lidapeng/Desktop/myDocument/m.jpg");
ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix("/Users/lidapeng/Desktop/test/a1.jpg");
ThreeChannelMatrix threeChannelMatrix2 = picture.getThreeMatrix("/Users/lidapeng/Desktop/test/b.jpg");
ThreeChannelMatrix threeChannelMatrix3 = picture.getThreeMatrix("/Users/lidapeng/Desktop/test/c.jpg");
operation.colorStudy(threeChannelMatrix1, 1, specificationsList);
// System.out.println("=======================================" + i);
operation.colorStudy(threeChannelMatrix2, 2, specificationsList);
operation.colorStudy(threeChannelMatrix3, 3, specificationsList);
}
// minX==301,minY==430,maxX==854,maxY==920
// minX==497,minY==1090,maxX==994,maxY==1520
// test2(templeConfig);
test2(templeConfig);
}
public static void study() throws Exception {