增加特征选区筛选

This commit is contained in:
lidapeng 2020-10-09 09:28:54 +08:00
parent 382c3ca598
commit 877694fae2
15 changed files with 789 additions and 111 deletions

View File

@ -62,6 +62,23 @@ public class MatrixOperation {
}
}
public static double getEDistByMatrix(Matrix matrix1, Matrix matrix2) throws Exception {
if (matrix1.getX() == matrix2.getX() && matrix1.getY() == matrix2.getY()) {
int x = matrix1.getX();
int y = matrix1.getY();
double sigma = 0;
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
double sub = matrix1.getNumber(i, j) - matrix2.getNumber(i, j);
sigma = sigma + Math.pow(sub, 2);
}
}
return sigma / (x * y);
} else {
throw new Exception("two matrixes is not equals");
}
}
//返回两个向量之间的欧氏距离的平方
public static double getEDist(Matrix matrix1, Matrix matrix2) throws Exception {
if (matrix1.isRowVector() && matrix2.isRowVector() && matrix1.getY() == matrix2.getY()) {
@ -322,16 +339,16 @@ public class MatrixOperation {
return inverserNumber;
}
public static Matrix getInverseMatrixs(Matrix matrixs) throws Exception {//矩阵求逆
double def = matrixs.getDet();
public static Matrix getInverseMatrixs(Matrix matrix) throws Exception {//矩阵求逆
double def = matrix.getDet();
if (def != 0) {
def = ArithUtil.div(1, def);
Matrix myMatrix = adjointMatrix(matrixs);//伴随矩阵
Matrix myMatrix = adjointMatrix(matrix);//伴随矩阵
mathMul(myMatrix, def);
return myMatrix;
} else {
System.out.println(matrixs.getString());
throw new Exception("this matrixs do not have InverseMatrixs");
System.out.println(matrix.getString());
throw new Exception("this matrix do not have InverseMatrixs");
}
}

View File

@ -82,7 +82,7 @@ public class Convolution extends Frequency {
meanClustering.setColor(color);
}
}
meanClustering.start();
meanClustering.start(false);
List<RGBNorm> rgbNorms = meanClustering.getMatrices();
Collections.sort(rgbNorms, rgbSort);
for (RGBNorm rgbNorm : rgbNorms) {
@ -182,7 +182,7 @@ public class Convolution extends Frequency {
meanClustering.setColor(color);
}
}
meanClustering.start();
meanClustering.start(false);
List<RGBNorm> rgbNorms = meanClustering.getMatrices();
Collections.sort(rgbNorms, rgbSort);
List<Double> features = new ArrayList<>();
@ -203,32 +203,64 @@ public class Convolution extends Frequency {
public List<Double> getCenterTexture(ThreeChannelMatrix threeChannelMatrix, int size, int poolSize, TempleConfig templeConfig
, int sqNub) throws Exception {
RGBSort rgbSort = new RGBSort();
double dispersedThNub = templeConfig.getFood().getDispersedTh();
int step = templeConfig.getFood().getStep();
MeanClustering meanClustering = new MeanClustering(sqNub, templeConfig);
Matrix matrixR = threeChannelMatrix.getMatrixR();
Matrix matrixG = threeChannelMatrix.getMatrixG();
Matrix matrixB = threeChannelMatrix.getMatrixB();
Matrix matrixH = threeChannelMatrix.getH();
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);
// for (int i = 0; i < xn; i++) {
// for (int j = 0; j < yn; j++) {
// double[] rgb = new double[]{matrixR.getNumber(i, j), matrixG.getNumber(i, j)
// , matrixB.getNumber(i, j)};
// meanClustering.setColor(rgb);
// }
// }
//局部特征选区筛选
double sigma = 0;
int nub = 0;
for (int i = 0; i <= xn - size; i += step) {
for (int j = 0; j <= yn - size; j += step) {
Matrix sonH = matrixH.getSonOfMatrix(i, j, size, size);
double[] h = new double[size * size];
nub++;
for (int t = 0; t < size; t++) {
for (int k = 0; k < size; k++) {
int index = t * size + k;
h[index] = sonH.getNumber(t, k);
}
}
meanClustering.setColor(rgb);
sigma = dc(h) + sigma;
}
}
meanClustering.start();//开始聚类
double dispersedTh = (sigma / nub) * dispersedThNub;//离散阈值
for (int i = 0; i <= xn - size; i += step) {
for (int j = 0; j <= yn - size; j += step) {
Matrix sonR = matrixR.getSonOfMatrix(i, j, size, size);
Matrix sonG = matrixG.getSonOfMatrix(i, j, size, size);
Matrix sonB = matrixB.getSonOfMatrix(i, j, size, size);
Matrix sonH = matrixH.getSonOfMatrix(i, j, size, size);
double[] h = new double[size * size];
double[] rgb = new double[size * size * 3];
for (int t = 0; t < size; t++) {
for (int k = 0; k < size; k++) {
int index = t * size + k;
h[index] = sonH.getNumber(t, k);
rgb[index] = sonR.getNumber(t, k);
rgb[size * size + index] = sonG.getNumber(t, k);
rgb[size * size * 2 + index] = sonB.getNumber(t, k);
}
}
double dispersed = dc(h);
if (dispersed < dispersedTh) {
meanClustering.setColor(rgb);
}
}
}
meanClustering.start(false);//开始聚类
List<RGBNorm> rgbNorms = meanClustering.getMatrices();
Collections.sort(rgbNorms, rgbSort);
List<Double> features = new ArrayList<>();
@ -238,6 +270,7 @@ public class Convolution extends Frequency {
features.add(rgb[j]);
}
}
//System.out.println(features);
return features;
}
@ -256,6 +289,24 @@ public class Convolution extends Frequency {
return border;
}
public void imgNormalization(ThreeChannelMatrix threeChannelMatrix) throws Exception {
Matrix matrixR = threeChannelMatrix.getMatrixR();
Matrix matrixG = threeChannelMatrix.getMatrixG();
Matrix matrixB = threeChannelMatrix.getMatrixB();
int x = matrixR.getX();
int y = matrixR.getY();
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
double colorR = matrixR.getNumber(i, j) / 255;
double colorG = matrixG.getNumber(i, j) / 255;
double colorB = matrixB.getNumber(i, j) / 255;
matrixR.setNub(i, j, colorR);
matrixG.setNub(i, j, colorG);
matrixB.setNub(i, j, colorB);
}
}
}
public ThreeChannelMatrix getRegionMatrix(ThreeChannelMatrix threeChannelMatrix, int x, int y, int xSize, int ySize) {
ThreeChannelMatrix threeChannelMatrix1 = new ThreeChannelMatrix();
Matrix matrixR = threeChannelMatrix.getMatrixR().getSonOfMatrix(x, y, xSize, ySize);

View File

@ -0,0 +1,24 @@
package org.wlld.imageRecognition;
import org.wlld.imageRecognition.border.DistBody;
import java.util.Comparator;
/**
* @param
* @DATA
* @Author LiDaPeng
* @Description
*/
public class DistSort implements Comparator<DistBody> {
@Override
public int compare(DistBody o1, DistBody o2) {
if (o1.getDist() < o2.getDist()) {
return -1;
} else if (o1.getDist() > o2.getDist()) {
return 1;
} else {
return 0;
}
}
}

View File

@ -90,7 +90,7 @@ public class MeanClustering {
}
}
public void start() throws Exception {//开始聚类
public void start(boolean isRegression) throws Exception {//开始聚类
if (matrixList.size() > 1) {
Random random = new Random();
for (int i = 0; i < speciesQuantity; i++) {//初始化均值向量
@ -111,7 +111,9 @@ public class MeanClustering {
break;
}
}
// startRegression();//开始进行回归
if (isRegression) {
startRegression();//开始进行回归
}
} else {
throw new Exception("matrixList number less than 2");
}

View File

@ -103,43 +103,49 @@ public class Operation {//进行计算
int maxY = regionBody.getMaxY() - dif;
int xSize = maxX - minX;
int ySize = maxY - minY;
//convolution.imgNormalization(threeChannelMatrix);
ThreeChannelMatrix threeChannelMatrix1 = convolution.getRegionMatrix(threeChannelMatrix, minX, minY, xSize, ySize);
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.getCenterTexture(threeChannelMatrix1, templeConfig.getFood().getRegionSize(),
templeConfig.getPoolSize(), templeConfig, templeConfig.getFeatureNub());
if (templeConfig.isShowLog()) {
System.out.println(tag + ":" + feature);
}
int classifier = templeConfig.getClassifier();
switch (classifier) {
case Classifier.DNN:
Map<Integer, Double> map = new HashMap<>();
map.put(tag, 1.0);
if (templeConfig.getSensoryNerves().size() == templeConfig.getFeatureNub() * 3) {
intoDnnNetwork(1, feature, templeConfig.getSensoryNerves(), true, map, null);
} else {
throw new Exception("nerves number is not equal featureNub");
}
break;
case Classifier.LVQ:
Matrix vector = MatrixOperation.listToRowVector(feature);
lvqStudy(tag, vector);
break;
case Classifier.VAvg:
Matrix vec = MatrixOperation.listToRowVector(feature);
avgStudy(tag, vec);
break;
case Classifier.KNN:
Matrix veck = MatrixOperation.listToRowVector(feature);
knnStudy(tag, veck);
break;
}
List<Double> feature = convolution.getCenterTexture(threeChannelMatrix1, templeConfig.getFood().getRegionSize(),
templeConfig.getPoolSize(), templeConfig, templeConfig.getFeatureNub());
if (templeConfig.isShowLog()) {
System.out.println(tag + ":" + feature);
}
int classifier = templeConfig.getClassifier();
switch (classifier) {
case Classifier.DNN:
Map<Integer, Double> map = new HashMap<>();
map.put(tag, 1.0);
if (templeConfig.getSensoryNerves().size() == templeConfig.getFeatureNub() * 3) {
intoDnnNetwork(1, feature, templeConfig.getSensoryNerves(), true, map, null);
} else {
throw new Exception("nerves number is not equal featureNub");
}
break;
case Classifier.LVQ:
Matrix vector = MatrixOperation.listToRowVector(feature);
lvqStudy(tag, vector);
break;
case Classifier.VAvg:
Matrix vec = MatrixOperation.listToRowVector(feature);
avgStudy(tag, vec);
break;
case Classifier.KNN:
Matrix veck = MatrixOperation.listToRowVector(feature);
knnStudy(tag, veck);
break;
}
return regionBody;
} else {
for (RegionBody regionBody : regionBodies) {
int minX = regionBody.getMinX();
int minY = regionBody.getMinY();
int maxX = regionBody.getMaxX();
int maxY = regionBody.getMaxY();
System.out.println("异常minX==" + minX + ",minY==" + minY + ",maxX==" + maxX + ",maxY==" + maxY);
}
throw new Exception("Parameter exception region size==" + regionBodies.size());
}
}

View File

@ -45,6 +45,7 @@ public class RGBNorm {
for (int i = 0; i < rgb.length; i++) {
rgbUp[i] = rgb[i];
}
rgbs.clear();
//System.out.println("clear==" + Arrays.toString(rgbUp));
}
@ -79,6 +80,7 @@ public class RGBNorm {
for (int i = 0; i < rgb.length; i++) {
rgbAll[i] = rgbAll[i] + rgb[i];
}
rgbs.add(rgb);
nub++;
}

View File

@ -8,6 +8,24 @@ public class ThreeChannelMatrix {
Matrix matrixB;
Matrix H;
Matrix matrixRGB;
int similarId;//最大相似id
boolean isLine = false;//是否被连接
public int getSimilarId() {
return similarId;
}
public boolean isLine() {
return isLine;
}
public void setLine(boolean line) {
isLine = line;
}
public void setSimilarId(int similarId) {
this.similarId = similarId;
}
public Matrix getMatrixRGB() {
return matrixRGB;

View File

@ -0,0 +1,28 @@
package org.wlld.imageRecognition.border;
/**
* @param
* @DATA
* @Author LiDaPeng
* @Description
*/
public class DistBody {
private int id;//id
private double dist;//距离
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getDist() {
return dist;
}
public void setDist(double dist) {
this.dist = dist;
}
}

View File

@ -0,0 +1,138 @@
package org.wlld.imageRecognition.segmentation;
import org.wlld.MatrixTools.Matrix;
import org.wlld.MatrixTools.MatrixOperation;
import org.wlld.imageRecognition.ThreeChannelMatrix;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @param
* @DATA
* @Author LiDaPeng
* @Description 寻找相似度最大的选区
*/
public class FindMaxSimilar {
public void findMaxSimilar(ThreeChannelMatrix threeChannelMatrix, int size) throws Exception {
Map<Integer, ThreeChannelMatrix> threeChannelMatrices = new HashMap<>();
Matrix matrixR = threeChannelMatrix.getMatrixR();
Matrix matrixG = threeChannelMatrix.getMatrixG();
Matrix matrixB = threeChannelMatrix.getMatrixB();
Matrix matrixRGB = threeChannelMatrix.getMatrixRGB();
int x = matrixR.getX();
int y = matrixR.getY();
System.out.println("初始区域数量:" + (x * y));
int index = 0;
for (int i = 0; i <= x - size; i += size) {
for (int j = 0; j <= y - size; j += size) {
ThreeChannelMatrix threeChannelMatrix1 = new ThreeChannelMatrix();
Matrix bodyR = matrixR.getSonOfMatrix(i, j, size, size);
Matrix bodyG = matrixG.getSonOfMatrix(i, j, size, size);
Matrix bodyB = matrixB.getSonOfMatrix(i, j, size, size);
Matrix bodyRGB = matrixRGB.getSonOfMatrix(i, j, size, size);
threeChannelMatrix1.setMatrixR(bodyR);
threeChannelMatrix1.setMatrixG(bodyG);
threeChannelMatrix1.setMatrixB(bodyB);
threeChannelMatrix1.setMatrixRGB(bodyRGB);
threeChannelMatrices.put(index, threeChannelMatrix1);
index++;
}
}
//切割完毕 开始寻找最大相似性
for (Map.Entry<Integer, ThreeChannelMatrix> entry : threeChannelMatrices.entrySet()) {
int key = entry.getKey();
ThreeChannelMatrix threeChannelMatrix1 = entry.getValue();
Matrix matrix1 = threeChannelMatrix1.getMatrixRGB();
double minDist = -1;
int similarId = 0;
for (Map.Entry<Integer, ThreeChannelMatrix> entrySon : threeChannelMatrices.entrySet()) {
int sonKey = entrySon.getKey();
if (key != sonKey) {
Matrix matrix2 = entrySon.getValue().getMatrixRGB();
double dist = MatrixOperation.getEDistByMatrix(matrix1, matrix2);
if (minDist == -1 || dist < minDist) {
minDist = dist;
similarId = sonKey;
}
}
}
threeChannelMatrix1.setSimilarId(similarId);
}
//最大相似性区域已经查找完毕,开始进行连线
Map<Integer, List<Integer>> lineMap = line(threeChannelMatrices);
//System.out.println("size:" + lineMap.size());
int max = 0;
int maxK = 0;
for (Map.Entry<Integer, List<Integer>> entry : lineMap.entrySet()) {
int key = entry.getKey();
int nub = entry.getValue().size();
if (nub > max) {
max = nub;
maxK = key;
}
}
System.out.println("max:" + max);
int key = lineMap.get(maxK).get(0);
Matrix matrix = threeChannelMatrices.get(key).getMatrixR();
System.out.println(matrix.getString());
}
private void merge(Map<Integer, List<Integer>> lineMap, Map<Integer, ThreeChannelMatrix> map,
int x, int y) throws Exception {
Map<Integer, Matrix> map2 = new HashMap<>();
for (Map.Entry<Integer, List<Integer>> entry : lineMap.entrySet()) {
int key = entry.getKey();
Matrix matrixAll = new Matrix(x, y);
List<Integer> list = entry.getValue();
int len = list.size();
for (int i = 0; i < len; i++) {
ThreeChannelMatrix threeChannelMatrix = map.get(list.get(i));
Matrix matrixRGB = threeChannelMatrix.getMatrixRGB();
matrixAll = MatrixOperation.add(matrixAll, matrixRGB);
}
MatrixOperation.mathDiv(matrixAll, len);//矩阵数除
map2.put(key, matrixAll);
}
}
private Map<Integer, List<Integer>> line(Map<Integer, ThreeChannelMatrix> threeChannelMatrices) {//开始进行连线
Map<Integer, List<Integer>> lineMap = new HashMap<>();
for (Map.Entry<Integer, ThreeChannelMatrix> entry : threeChannelMatrices.entrySet()) {
int key = entry.getKey();//当前进行连线的id
ThreeChannelMatrix myThreeChannelMatrix = entry.getValue();
boolean isLine = myThreeChannelMatrix.isLine();//是否被连线
int upIndex = key;//上一个进行连线的id
if (!isLine) {//可以进行连线
List<Integer> list = new ArrayList<>();
lineMap.put(key, list);
list.add(key);
myThreeChannelMatrix.setLine(true);
boolean line;
do {
int similarId = myThreeChannelMatrix.getSimilarId();//距离它最近的id
ThreeChannelMatrix threeChannelMatrix = threeChannelMatrices.get(similarId);
line = threeChannelMatrix.isLine();
if (!line) {//进行连线
list.add(similarId);
threeChannelMatrix.setLine(true);
//如果当前被连线的矩阵的最近id为连线者本身则连线后停止遍历
if (upIndex == threeChannelMatrix.getSimilarId()) {//停止连线
line = true;
} else {//继续连线z
upIndex = similarId;
myThreeChannelMatrix = threeChannelMatrix;
}
}
} while (!line);
}
}
return lineMap;
}
}

View File

@ -0,0 +1,206 @@
package org.wlld.imageRecognition.segmentation;
import org.wlld.MatrixTools.Matrix;
import org.wlld.MatrixTools.MatrixOperation;
import org.wlld.imageRecognition.DistSort;
import org.wlld.imageRecognition.TempleConfig;
import org.wlld.imageRecognition.border.DistBody;
import java.util.*;
/**
* @param
* @DATA
* @Author LiDaPeng
* @Description 修正映射
*/
public class RegionMapping {
private Map<Integer, List<Matrix>> featureMap;
private double studyPoint = 0.01;
private Map<Integer, Matrix> powerMatrixMap = new HashMap<>();//映射层的权重矩阵
private int times = 200;
public RegionMapping(TempleConfig templeConfig) {
this.featureMap = templeConfig.getKnn().getFeatureMap();
}
public void detection() throws Exception {//对模型进行检查,先对当前模型最相近的三个模型进行检出
Map<Integer, List<DistBody>> map = new HashMap<>();
DistSort distSort = new DistSort();
for (Map.Entry<Integer, List<Matrix>> entry : featureMap.entrySet()) {
int key = entry.getKey();
List<Matrix> myFeature = entry.getValue();
//记录 哪个类别 每个类别距离这个类别的最小距离
List<DistBody> distBodies = new ArrayList<>();
map.put(key, distBodies);
for (Map.Entry<Integer, List<Matrix>> entrySon : featureMap.entrySet()) {
int type = entrySon.getKey();
if (key != type) {//当前其余类别
DistBody distBody = new DistBody();
List<Matrix> features = entrySon.getValue();//其余类别的所有特征
double minError = -1;
for (Matrix myMatrix : myFeature) {//待验证类别所有特征
for (Matrix otherFeature : features) {
double dist = MatrixOperation.getEDistByMatrix(myMatrix, otherFeature);
if (minError < 0 || dist < minError) {
minError = dist;
}
}
}
//当前类别遍历结束
distBody.setId(type);
distBody.setDist(minError);
distBodies.add(distBody);
}
}
}
//进行排序
for (Map.Entry<Integer, List<DistBody>> entry : map.entrySet()) {
List<DistBody> list = entry.getValue();
Collections.sort(list, distSort);
}
adjustWeight(map, 3);
test(12, map, 3);
// int testType = 5;
// List<DistBody> testBody = map.get(testType);
// Matrix matrix = powerMatrixMap.get(testType);
// System.out.println(matrix.getString());
}
private Matrix mapping(Matrix feature, Matrix mapping) throws Exception {
int y = feature.getY();
if (y == mapping.getY()) {
Matrix matrix = new Matrix(1, y);
for (int i = 0; i < y; i++) {
double nub = feature.getNumber(0, i) * mapping.getNumber(0, i);
matrix.setNub(0, i, nub);
}
return matrix;
} else {
throw new Exception("matrix is not equals");
}
}
private void test(int key, Map<Integer, List<DistBody>> map, int nub) throws Exception {
Matrix matrixMapping = powerMatrixMap.get(key);//映射
List<Matrix> rightFeature = featureMap.get(key);//正确的特征
System.out.println("正确特征====================");
for (Matrix matrix : rightFeature) {
System.out.println(matrix.getString());
}
int y = rightFeature.get(0).getY();
Matrix featureMatrix = getFeatureAvg(rightFeature, y);//均值
List<DistBody> distBodies = map.get(key);
List<Matrix> wrongFeature = new ArrayList<>();
for (int i = 0; i < nub; i++) {
DistBody distBody = distBodies.get(i);
int id = distBody.getId();//相近的特征id
wrongFeature.addAll(featureMap.get(id));
}
System.out.println("错误特征=============");
for (Matrix matrix : wrongFeature) {
System.out.println(matrix.getString());
}
//做验证
double minOtherDist = -1;//其余特征最小距离
for (Matrix wrongMatrix : wrongFeature) {
Matrix wrongMapping = mapping(wrongMatrix, matrixMapping);
double dist = MatrixOperation.getEDistByMatrix(featureMatrix, wrongMapping);
System.out.println("异类距离:" + dist);
if (minOtherDist < 0 || dist < minOtherDist) {
minOtherDist = dist;
}
}
System.out.println("异类最小距离:" + minOtherDist);
for (Matrix rightMatrix : rightFeature) {
Matrix rightMapping = mapping(rightMatrix, matrixMapping);
double dist = MatrixOperation.getEDistByMatrix(featureMatrix, rightMapping);
System.out.println("同类距离:" + dist);
}
}
private void adjustWeight(Map<Integer, List<DistBody>> map, int nub) throws Exception {//进行权重调整
for (Map.Entry<Integer, List<DistBody>> entry : map.entrySet()) {
int key = entry.getKey();//当前类别的id
List<Matrix> rightFeature = featureMap.get(key);//正确的特征
List<DistBody> distBodies = entry.getValue();
List<Matrix> wrongFeature = new ArrayList<>();
for (int i = 0; i < nub; i++) {
DistBody distBody = distBodies.get(i);
int id = distBody.getId();//相近的特征id
wrongFeature.addAll(featureMap.get(id));
}
//对每个分类的特征图进行权重调整
updatePower(wrongFeature, rightFeature, key);
}
}
//调整权重矩阵
private void updatePower(List<Matrix> wrongFeature, List<Matrix> rightFeature, int key) throws Exception {
int size = wrongFeature.size();
int y = rightFeature.get(0).getY();
//特征均值向量
Matrix featureMatrix = getFeatureAvg(rightFeature, y);
Matrix powerMatrix = new Matrix(1, y);
powerMatrixMap.put(key, powerMatrix);
Random random = new Random();
for (int j = 0; j < times; j++) {
for (int i = 0; i < size; i++) {
Matrix feature = rightFeature.get(random.nextInt(rightFeature.size()));
Matrix noFeature = wrongFeature.get(i);
powerDeflection(feature, featureMatrix, true, powerMatrix);
powerDeflection(noFeature, featureMatrix, false, powerMatrix);
}
}
end();
}
private void end() throws Exception {
for (Map.Entry<Integer, Matrix> entry : powerMatrixMap.entrySet()) {
Matrix powerMatrix = entry.getValue();
int y = powerMatrix.getY();
double min = 0;
for (int i = 0; i < y; i++) {
double nub = powerMatrix.getNumber(0, i);
if (nub < min) {
min = nub;
}
}
//获取最小值完毕
for (int i = 0; i < y; i++) {
double nub = powerMatrix.getNumber(0, i);
powerMatrix.setNub(0, i, nub - min);
}
}
}
private void powerDeflection(Matrix matrix1, Matrix matrix2, boolean polymerization,
Matrix powerMatrix) throws Exception {
int y = matrix1.getY();
for (int i = 0; i < y; i++) {
double sub = Math.abs(matrix1.getNumber(0, i) - matrix2.getNumber(0, i))
* studyPoint;
double power = powerMatrix.getNumber(0, i);//当前矩阵中的权值
if (polymerization) {//同类聚合 聚合是减
power = power - sub;
} else {//异类离散
power = power + sub;
}
powerMatrix.setNub(0, i, power);
}
}
private Matrix getFeatureAvg(List<Matrix> rightFeature, int size) throws Exception {//求特征均值
Matrix feature = new Matrix(1, size);
int nub = rightFeature.size();
for (int i = 0; i < nub; i++) {
Matrix matrix = rightFeature.get(i);
for (int j = 0; j < size; j++) {
double sigma = matrix.getNumber(0, j) + feature.getNumber(0, j);
feature.setNub(0, j, sigma);
}
}
MatrixOperation.mathDiv(feature, nub);
return feature;
}
}

View File

@ -0,0 +1,55 @@
package org.wlld.imageRecognition.segmentation;
import org.wlld.MatrixTools.Matrix;
import org.wlld.imageRecognition.MeanClustering;
import org.wlld.imageRecognition.RGBNorm;
import org.wlld.imageRecognition.TempleConfig;
import org.wlld.imageRecognition.ThreeChannelMatrix;
import java.util.List;
/**
* @param
* @DATA
* @Author LiDaPeng
* @Description
*/
public class WFilter {
private List<RGBNorm> rgbNorms;
public void filter(ThreeChannelMatrix threeChannelMatrix, TempleConfig templeConfig,
int speciesQuantity) throws Exception {
MeanClustering meanClustering = new MeanClustering(speciesQuantity, templeConfig);
Matrix matrixR = threeChannelMatrix.getMatrixR();
Matrix matrixG = threeChannelMatrix.getMatrixG();
Matrix matrixB = threeChannelMatrix.getMatrixB();
int x = matrixR.getX();
int y = matrixR.getY();
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
double[] color = new double[]{matrixR.getNumber(i, j), matrixG.getNumber(i, j), matrixB.getNumber(i, j)};
meanClustering.setColor(color);
}
}
meanClustering.start(true);
rgbNorms = meanClustering.getMatrices();
}
private void getDist() {
double min = -1;
for (RGBNorm rgbNorm : rgbNorms) {
double[] rgb = rgbNorm.getRgb();
}
}
private double dist(double[] a, double[] b) {
double sigma = 0;
for (int i = 0; i < a.length; i++) {
double sub = Math.pow(a[i] - b[i], 2);
sigma = sigma + sub;
}
return sigma / a.length;
}
}

View File

@ -75,15 +75,15 @@ public class Watershed {
private boolean isTray(int x, int y) throws Exception {
boolean isTray = false;
double[] rgb = new double[]{matrixR.getNumber(x, y) / 255, matrixG.getNumber(x, y) / 255,
matrixB.getNumber(x, y) / 255};
for (RgbRegression rgbRegression : trayBody) {
double dist = rgbRegression.getDisError(rgb);
if (dist < trayTh) {
isTray = true;
break;
}
}
// double[] rgb = new double[]{matrixR.getNumber(x, y) / 255, matrixG.getNumber(x, y) / 255,
// matrixB.getNumber(x, y) / 255};
// for (RgbRegression rgbRegression : trayBody) {
// double dist = rgbRegression.getDisError(rgb);
// if (dist < trayTh) {
// isTray = true;
// break;
// }
// }
return isTray;
}
@ -354,10 +354,12 @@ public class Watershed {
int width = maxY - minY;
int height = maxX - minX;
boolean isCenter = true;
if (edgeSize > 0) {
double h = this.height / edgeSize;
double w = this.width / edgeSize;
isCenter = maxX > h && maxY > w && maxX < (h * (edgeSize - 1)) && maxY < (w * (edgeSize - 1));
if (edgeSize > 0) {//边缘过滤
double top = this.height / edgeSize;//上边缘界限
double left = this.width / edgeSize;//左边缘界限
double bottom = this.height - top;//下边缘界限
double right = this.width - left;//右边缘界限
isCenter = maxX > top && maxY > left && minX < bottom && minY < right;
}
if (width >= specification.getMinWidth() && height >= specification.getMinHeight()
&& width <= specification.getMaxWidth() && height <= specification.getMaxHeight()

View File

@ -19,7 +19,25 @@ public class Food {
private List<RgbRegression> trayBody = new ArrayList<>();//托盘实体参数
private int regressionNub = 10000;//回归次数
private double trayTh = 0.1;//托盘回归阈值
private int regionSize = 10;//纹理区域大小
private int regionSize = 5;//纹理区域大小
private int step = 1;//特征取样步长
private double dispersedTh = 0.3;//选区筛选离散阈值
public int getStep() {
return step;
}
public void setStep(int step) {
this.step = step;
}
public double getDispersedTh() {
return dispersedTh;
}
public void setDispersedTh(double dispersedTh) {
this.dispersedTh = dispersedTh;
}
public int getRegionSize() {
return regionSize;

View File

@ -9,6 +9,7 @@ import org.wlld.config.RZ;
import org.wlld.config.StudyPattern;
import org.wlld.imageRecognition.*;
import org.wlld.imageRecognition.segmentation.RegionBody;
import org.wlld.imageRecognition.segmentation.RegionMapping;
import org.wlld.imageRecognition.segmentation.Specifications;
import org.wlld.nerveEntity.ModelParameter;
import org.wlld.param.Cutting;
@ -56,25 +57,27 @@ public class FoodTest {
public static TempleConfig getTemple(ModelParameter modelParameter) throws Exception {
TempleConfig templeConfig = new TempleConfig();
templeConfig.setEdge(10);
//templeConfig.isShowLog(true);//是否打印日志
Cutting cutting = templeConfig.getCutting();
Food food = templeConfig.getFood();
//
cutting.setMaxRain(360);//切割阈值
cutting.setTh(0.6);
cutting.setRegionNub(200);
cutting.setMaxIou(2.0);
cutting.setTh(0.8);
cutting.setRegionNub(100);
cutting.setMaxIou(2);
//knn参数
templeConfig.setKnnNub(1);
//池化比例
templeConfig.setPoolSize(2);//缩小比例
//聚类
templeConfig.setFeatureNub(5);//聚类特征数量
templeConfig.setFeatureNub(3);//聚类特征数量
//菜品识别实体类
food.setShrink(20);//缩紧像素
food.setTimes(2);//聚类数据增强
food.setRowMark(0.12);//0.12
food.setColumnMark(0.12);//0.25
food.setShrink(5);//缩紧像素
food.setTimes(1);//聚类数据增强
food.setRegionSize(5);
food.setRowMark(0.15);//0.12
food.setColumnMark(0.15);//0.25
food.setRegressionNub(20000);
food.setTrayTh(0.08);
templeConfig.setClassifier(Classifier.KNN);
@ -91,19 +94,121 @@ public class FoodTest {
Operation operation = new Operation(templeConfig);
List<Specifications> specificationsList = new ArrayList<>();
Specifications specifications = new Specifications();
specifications.setMinWidth(100);
specifications.setMinHeight(100);
specifications.setMaxWidth(1000);
specifications.setMaxHeight(1000);
specifications.setMinWidth(150);//150
specifications.setMinHeight(150);//150
specifications.setMaxWidth(600);
specifications.setMaxHeight(600);
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/test/test.jpg");
// ThreeChannelMatrix threeChannelMatrix = picture.getThreeMatrix("/Users/lidapeng/Desktop/myDocument/d.jpg");
// operation.setTray(threeChannelMatrix);
String name = "/Users/lidapeng/Desktop/test/testOne/";
for (int i = 0; i < 5; i++) {
System.out.println("轮数============================" + i);
ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix(name + "a" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix2 = picture.getThreeMatrix(name + "b" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix3 = picture.getThreeMatrix(name + "c" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix4 = picture.getThreeMatrix(name + "d" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix5 = picture.getThreeMatrix(name + "e" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix6 = picture.getThreeMatrix(name + "f" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix7 = picture.getThreeMatrix(name + "g" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix8 = picture.getThreeMatrix(name + "h" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix9 = picture.getThreeMatrix(name + "i" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix10 = picture.getThreeMatrix(name + "j" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix11 = picture.getThreeMatrix(name + "k" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix12 = picture.getThreeMatrix(name + "l" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix13 = picture.getThreeMatrix(name + "m" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix14 = picture.getThreeMatrix(name + "n" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix15 = picture.getThreeMatrix(name + "o" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix16 = picture.getThreeMatrix(name + "p" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix17 = picture.getThreeMatrix(name + "q" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix18 = picture.getThreeMatrix(name + "r" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix19 = picture.getThreeMatrix(name + "s" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix20 = picture.getThreeMatrix(name + "t" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix21 = picture.getThreeMatrix(name + "u" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix22 = picture.getThreeMatrix(name + "v" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix23 = picture.getThreeMatrix(name + "w" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix24 = picture.getThreeMatrix(name + "x" + i + ".jpg");
operation.colorStudy(threeChannelMatrix1, 1, specificationsList);
operation.colorStudy(threeChannelMatrix2, 2, specificationsList);
operation.colorStudy(threeChannelMatrix3, 3, specificationsList);
operation.colorStudy(threeChannelMatrix4, 4, specificationsList);
operation.colorStudy(threeChannelMatrix5, 5, specificationsList);
operation.colorStudy(threeChannelMatrix6, 6, specificationsList);
operation.colorStudy(threeChannelMatrix7, 7, specificationsList);
operation.colorStudy(threeChannelMatrix8, 8, specificationsList);
operation.colorStudy(threeChannelMatrix9, 9, specificationsList);
operation.colorStudy(threeChannelMatrix10, 10, specificationsList);
operation.colorStudy(threeChannelMatrix11, 11, specificationsList);
operation.colorStudy(threeChannelMatrix12, 12, specificationsList);
operation.colorStudy(threeChannelMatrix13, 13, specificationsList);
operation.colorStudy(threeChannelMatrix14, 14, specificationsList);
operation.colorStudy(threeChannelMatrix15, 15, specificationsList);
operation.colorStudy(threeChannelMatrix16, 16, specificationsList);
operation.colorStudy(threeChannelMatrix17, 17, specificationsList);
operation.colorStudy(threeChannelMatrix18, 18, specificationsList);
operation.colorStudy(threeChannelMatrix19, 19, specificationsList);
operation.colorStudy(threeChannelMatrix20, 20, specificationsList);
operation.colorStudy(threeChannelMatrix21, 21, specificationsList);
operation.colorStudy(threeChannelMatrix22, 22, specificationsList);
operation.colorStudy(threeChannelMatrix23, 23, specificationsList);
operation.colorStudy(threeChannelMatrix24, 24, specificationsList);
}
// test2(templeConfig);
int i = 4;
ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix(name + "a" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix2 = picture.getThreeMatrix(name + "b" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix3 = picture.getThreeMatrix(name + "c" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix4 = picture.getThreeMatrix(name + "d" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix5 = picture.getThreeMatrix(name + "e" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix6 = picture.getThreeMatrix(name + "f" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix7 = picture.getThreeMatrix(name + "g" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix8 = picture.getThreeMatrix(name + "h" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix9 = picture.getThreeMatrix(name + "i" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix10 = picture.getThreeMatrix(name + "j" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix11 = picture.getThreeMatrix(name + "k" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix12 = picture.getThreeMatrix(name + "l" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix13 = picture.getThreeMatrix(name + "m" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix14 = picture.getThreeMatrix(name + "n" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix15 = picture.getThreeMatrix(name + "o" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix16 = picture.getThreeMatrix(name + "p" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix17 = picture.getThreeMatrix(name + "q" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix18 = picture.getThreeMatrix(name + "r" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix19 = picture.getThreeMatrix(name + "s" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix20 = picture.getThreeMatrix(name + "t" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix21 = picture.getThreeMatrix(name + "u" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix22 = picture.getThreeMatrix(name + "v" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix23 = picture.getThreeMatrix(name + "w" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix24 = picture.getThreeMatrix(name + "x" + i + ".jpg");
test3(threeChannelMatrix1, operation, specificationsList);
test3(threeChannelMatrix2, operation, specificationsList);
test3(threeChannelMatrix3, operation, specificationsList);
test3(threeChannelMatrix4, operation, specificationsList);
test3(threeChannelMatrix5, operation, specificationsList);
test3(threeChannelMatrix6, operation, specificationsList);
test3(threeChannelMatrix7, operation, specificationsList);
test3(threeChannelMatrix8, operation, specificationsList);
test3(threeChannelMatrix9, operation, specificationsList);
test3(threeChannelMatrix10, operation, specificationsList);
test3(threeChannelMatrix11, operation, specificationsList);
test3(threeChannelMatrix12, operation, specificationsList);
test3(threeChannelMatrix13, operation, specificationsList);
test3(threeChannelMatrix14, operation, specificationsList);
test3(threeChannelMatrix15, operation, specificationsList);
test3(threeChannelMatrix16, operation, specificationsList);
test3(threeChannelMatrix17, operation, specificationsList);
test3(threeChannelMatrix18, operation, specificationsList);
test3(threeChannelMatrix19, operation, specificationsList);
test3(threeChannelMatrix20, operation, specificationsList);
test3(threeChannelMatrix21, operation, specificationsList);
test3(threeChannelMatrix22, operation, specificationsList);
test3(threeChannelMatrix23, operation, specificationsList);
test3(threeChannelMatrix24, operation, specificationsList);
}
private static void test3(ThreeChannelMatrix threeChannelMatrix, Operation operation, List<Specifications> specifications) throws Exception {
int type = operation.colorLook(threeChannelMatrix, specifications).get(0).getType();
System.out.println(type);
}
public static void study() throws Exception {

View File

@ -3,10 +3,8 @@ package coverTest;
import org.wlld.MatrixTools.Matrix;
import org.wlld.config.Classifier;
import org.wlld.config.StudyPattern;
import org.wlld.imageRecognition.Convolution;
import org.wlld.imageRecognition.Picture;
import org.wlld.imageRecognition.TempleConfig;
import org.wlld.imageRecognition.ThreeChannelMatrix;
import org.wlld.imageRecognition.*;
import org.wlld.imageRecognition.segmentation.FindMaxSimilar;
import org.wlld.imageRecognition.segmentation.RegionBody;
import org.wlld.imageRecognition.segmentation.Specifications;
import org.wlld.imageRecognition.segmentation.Watershed;
@ -27,7 +25,7 @@ public class ForestTest {
private static Convolution convolution = new Convolution();
public static void main(String[] args) throws Exception {
test();
testPic();
//int a = (int) (Math.log(4) / Math.log(2));//id22是第几层
//double a = Math.pow(2, 5) - 1; 第五层的第一个数
// System.out.println("a==" + a);
@ -37,24 +35,32 @@ public class ForestTest {
public static void testPic() throws Exception {
Picture picture = new Picture();
TempleConfig templeConfig = getTemple(null);
Operation operation = new Operation(templeConfig);
List<Specifications> specificationsList = new ArrayList<>();
Specifications specifications = new Specifications();
specifications.setMinWidth(100);
specifications.setMinHeight(100);
specifications.setMaxWidth(1000);
specifications.setMaxHeight(1000);
specifications.setMaxWidth(600);
specifications.setMaxHeight(600);
specificationsList.add(specifications);
ThreeChannelMatrix threeChannelMatrix = picture.getThreeMatrix("/Users/lidapeng/Desktop/myDocument/d.jpg");
ThreeChannelMatrix threeChannelMatrix = picture.getThreeMatrix("/Users/lidapeng/Desktop/train/a2.jpg");
Watershed watershed = new Watershed(threeChannelMatrix, specificationsList, templeConfig);
List<RegionBody> regionBodies = watershed.rainfall();
RegionBody regionBody = regionBodies.get(0);
int minX = regionBody.getMinX();
int minY = regionBody.getMinY();
int maxX = regionBody.getMaxX();
int maxY = regionBody.getMaxY();
int xSize = maxX - minX;
int ySize = maxY - minY;
ThreeChannelMatrix threeChannelMatrix1 = convolution.getRegionMatrix(threeChannelMatrix, minX, minY, xSize, ySize);
if (regionBodies.size() == 1) {
RegionBody regionBody = regionBodies.get(0);
int minX = regionBody.getMinX();
int minY = regionBody.getMinY();
int maxX = regionBody.getMaxX();
int maxY = regionBody.getMaxY();
int xSize = maxX - minX;
int ySize = maxY - minY;
ThreeChannelMatrix threeChannelMatrix1 = convolution.getRegionMatrix(threeChannelMatrix, minX, minY, xSize, ySize);
List<Double> feature = convolution.getCenterTexture(threeChannelMatrix1, templeConfig.getFood().getRegionSize(),
templeConfig.getPoolSize(), templeConfig, templeConfig.getFeatureNub());
System.out.println(feature);
} else {
System.out.println("size===" + regionBodies.size());
}
}
@ -70,9 +76,9 @@ public class ForestTest {
Food food = templeConfig.getFood();
//
cutting.setMaxRain(360);//切割阈值
cutting.setTh(0.6);
cutting.setRegionNub(200);
cutting.setMaxIou(0.5);
cutting.setTh(0.8);
cutting.setRegionNub(100);
cutting.setMaxIou(0.2);
//knn参数
templeConfig.setKnnNub(1);
//池化比例
@ -80,10 +86,10 @@ public class ForestTest {
//聚类
templeConfig.setFeatureNub(3);//聚类特征数量
//菜品识别实体类
food.setShrink(10);//缩紧像素
food.setTimes(2);//聚类数据增强
food.setRowMark(0.1);//0.12
food.setColumnMark(0.1);//0.25
food.setShrink(5);//缩紧像素
food.setTimes(1);//聚类数据增强
food.setRowMark(0.15);//0.12
food.setColumnMark(0.15);//0.25
food.setRegressionNub(20000);
food.setTrayTh(0.08);
templeConfig.setClassifier(Classifier.KNN);