pull/43/head
thenk008 5 years ago
parent a3a09678ac
commit 50da4ed045

@ -16,7 +16,6 @@ import org.wlld.nerveEntity.SensoryNerve;
import org.wlld.tools.ArithUtil; import org.wlld.tools.ArithUtil;
import org.wlld.tools.IdCreator; import org.wlld.tools.IdCreator;
import javax.swing.text.html.parser.Entity;
import java.util.*; import java.util.*;
public class Operation {//进行计算 public class Operation {//进行计算
@ -25,7 +24,7 @@ public class Operation {//进行计算
private MatrixBack matrixBack = new MatrixBack(); private MatrixBack matrixBack = new MatrixBack();
private ImageBack imageBack = new ImageBack(); private ImageBack imageBack = new ImageBack();
private OutBack outBack; private OutBack outBack;
private double dif; private int dif;
public Operation(TempleConfig templeConfig) { public Operation(TempleConfig templeConfig) {
this.templeConfig = templeConfig; this.templeConfig = templeConfig;
@ -64,55 +63,45 @@ public class Operation {//进行计算
List<RegionBody> regionBodies = watershed.rainfall(); List<RegionBody> regionBodies = watershed.rainfall();
if (regionBodies.size() == 1) { if (regionBodies.size() == 1) {
RegionBody regionBody = regionBodies.get(0); RegionBody regionBody = regionBodies.get(0);
int integrate = templeConfig.getFood().getIntegrate(); int minX = regionBody.getMinX() + dif;
int minX = regionBody.getMinX(); int minY = regionBody.getMinY() + dif;
int minY = regionBody.getMinY(); int maxX = regionBody.getMaxX() - dif;
int maxX = regionBody.getMaxX(); int maxY = regionBody.getMaxY() - dif;
int maxY = regionBody.getMaxY();
int xSize = maxX - minX; int xSize = maxX - minX;
int ySize = maxY - minY; int ySize = maxY - minY;
for (int j = 1; j <= integrate; j++) { ThreeChannelMatrix threeChannelMatrix1 = convolution.getRegionMatrix(threeChannelMatrix, minX, minY, xSize, ySize);
double move = dif * j; //convolution.filtering(threeChannelMatrix1);//光照过滤
int moveX = (int) (xSize * move); int times = templeConfig.getFood().getTimes();
int moveY = (int) (ySize * move); for (int i = 0; i < times; i++) {
int x = minX + moveX; List<Double> feature = convolution.getCenterColor(threeChannelMatrix1, templeConfig.getPoolSize(),
int y = minY + moveY; templeConfig.getFeatureNub());
int xLength = xSize - moveX * 2; if (templeConfig.isShowLog()) {
int yLength = ySize - moveY * 2; System.out.println(tag + ":" + feature);
ThreeChannelMatrix threeChannelMatrix1 = convolution.getRegionMatrix(threeChannelMatrix, x, y, xLength, yLength); }
//convolution.filtering(threeChannelMatrix1);//光照过滤 //System.out.println("=====================================");
int times = templeConfig.getFood().getTimes(); int classifier = templeConfig.getClassifier();
for (int i = 0; i < times; i++) { switch (classifier) {
List<Double> feature = convolution.getCenterColor(threeChannelMatrix1, templeConfig.getPoolSize(), case Classifier.DNN:
templeConfig.getFeatureNub()); Map<Integer, Double> map = new HashMap<>();
if (templeConfig.isShowLog()) { map.put(tag, 1.0);
System.out.println(tag + ":" + feature); if (templeConfig.getSensoryNerves().size() == templeConfig.getFeatureNub() * 3) {
} intoDnnNetwork(1, feature, templeConfig.getSensoryNerves(), true, map, null);
//System.out.println("====================================="); } else {
int classifier = templeConfig.getClassifier(); throw new Exception("nerves number is not equal featureNub");
switch (classifier) { }
case Classifier.DNN: break;
Map<Integer, Double> map = new HashMap<>(); case Classifier.LVQ:
map.put(tag, 1.0); Matrix vector = MatrixOperation.listToRowVector(feature);
if (templeConfig.getSensoryNerves().size() == templeConfig.getFeatureNub() * 3) { lvqStudy(tag, vector);
intoDnnNetwork(1, feature, templeConfig.getSensoryNerves(), true, map, null); break;
} else { case Classifier.VAvg:
throw new Exception("nerves number is not equal featureNub"); Matrix vec = MatrixOperation.listToRowVector(feature);
} avgStudy(tag, vec);
break; break;
case Classifier.LVQ: case Classifier.KNN:
Matrix vector = MatrixOperation.listToRowVector(feature); Matrix veck = MatrixOperation.listToRowVector(feature);
lvqStudy(tag, vector); knnStudy(tag, veck);
break; break;
case Classifier.VAvg:
Matrix vec = MatrixOperation.listToRowVector(feature);
avgStudy(tag, vec);
break;
case Classifier.KNN:
Matrix veck = MatrixOperation.listToRowVector(feature);
knnStudy(tag, veck, templeConfig.getKnns()[j]);
break;
}
} }
} }
} else { } else {
@ -120,7 +109,8 @@ public class Operation {//进行计算
} }
} }
private void knnStudy(int tagging, Matrix vector, Knn knn) throws Exception { private void knnStudy(int tagging, Matrix vector) throws Exception {
Knn knn = templeConfig.getKnn();
knn.insertMatrix(vector, tagging); knn.insertMatrix(vector, tagging);
} }
@ -142,70 +132,45 @@ public class Operation {//进行计算
List<RegionBody> regionList = watershed.rainfall(); List<RegionBody> regionList = watershed.rainfall();
for (RegionBody regionBody : regionList) { for (RegionBody regionBody : regionList) {
MaxPoint maxPoint = new MaxPoint(); MaxPoint maxPoint = new MaxPoint();
int integrate = templeConfig.getFood().getIntegrate(); int minX = regionBody.getMinX() + dif;
int minX = regionBody.getMinX(); int minY = regionBody.getMinY() + dif;
int minY = regionBody.getMinY(); int maxX = regionBody.getMaxX() - dif;
int maxX = regionBody.getMaxX(); int maxY = regionBody.getMaxY() - dif;
int maxY = regionBody.getMaxY();
int xSize = maxX - minX; int xSize = maxX - minX;
int ySize = maxY - minY; int ySize = maxY - minY;
Map<Integer, Integer> map = new HashMap<>(); ThreeChannelMatrix threeChannelMatrix1 = convolution.getRegionMatrix(threeChannelMatrix, minX, minY, xSize, ySize);
for (int j = 1; j <= integrate; j++) { //convolution.filtering(threeChannelMatrix1);//光照过滤
double move = dif * j; List<Double> feature = convolution.getCenterColor(threeChannelMatrix1, templeConfig.getPoolSize(),
int moveX = (int) (xSize * move); templeConfig.getFeatureNub());
int moveY = (int) (ySize * move); if (templeConfig.isShowLog()) {
int x = minX + moveX; System.out.println(feature);
int y = minY + moveY;
int xLength = xSize - moveX * 2;
int yLength = ySize - moveY * 2;
ThreeChannelMatrix threeChannelMatrix1 = convolution.getRegionMatrix(threeChannelMatrix, x, y, xLength, yLength);
//convolution.filtering(threeChannelMatrix1);//光照过滤
List<Double> feature = convolution.getCenterColor(threeChannelMatrix1, templeConfig.getPoolSize(),
templeConfig.getFeatureNub());
if (templeConfig.isShowLog()) {
System.out.println(feature);
}
int classifier = templeConfig.getClassifier();
int id = 0;
switch (classifier) {
case Classifier.LVQ:
Matrix myMatrix = MatrixOperation.listToRowVector(feature);
id = getIdByLVQ(myMatrix);
break;
case Classifier.DNN:
if (templeConfig.getSensoryNerves().size() == templeConfig.getFeatureNub() * 3) {
intoDnnNetwork(IdCreator.get().nextId(), feature, templeConfig.getSensoryNerves(), false, null, maxPoint);
id = maxPoint.getId();
} else {
throw new Exception("nerves number is not equal featureNub");
}
break;
case Classifier.VAvg:
Matrix myMatrix1 = MatrixOperation.listToRowVector(feature);
id = getIdByVag(myMatrix1);
break;
case Classifier.KNN:
Matrix myMatrix2 = MatrixOperation.listToRowVector(feature);
Knn knn = templeConfig.getKnns()[j];
id = knn.getType(myMatrix2);
break;
}
//完成一个结果,统计所有结果
if (map.containsKey(id)) {
map.put(id, map.get(id) + 1);
} else {
map.put(id, 1);
}
} }
int key = 0; int classifier = templeConfig.getClassifier();
int max = 0; int id = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) { switch (classifier) {
int nub = entry.getValue(); case Classifier.LVQ:
if (nub > max) { Matrix myMatrix = MatrixOperation.listToRowVector(feature);
key = entry.getKey(); id = getIdByLVQ(myMatrix);
} break;
case Classifier.DNN:
if (templeConfig.getSensoryNerves().size() == templeConfig.getFeatureNub() * 3) {
intoDnnNetwork(IdCreator.get().nextId(), feature, templeConfig.getSensoryNerves(), false, null, maxPoint);
id = maxPoint.getId();
} else {
throw new Exception("nerves number is not equal featureNub");
}
break;
case Classifier.VAvg:
Matrix myMatrix1 = MatrixOperation.listToRowVector(feature);
id = getIdByVag(myMatrix1);
break;
case Classifier.KNN:
Matrix myMatrix2 = MatrixOperation.listToRowVector(feature);
Knn knn = templeConfig.getKnn();
id = knn.getType(myMatrix2);
break;
} }
regionBody.setType(key); regionBody.setType(id);
//System.out.println("类别" + id); //System.out.println("类别" + id);
} }
return regionList; return regionList;

@ -66,16 +66,11 @@ public class TempleConfig {
private int featureNub = 4;//聚类特征数量 private int featureNub = 4;//聚类特征数量
private Food food = new Food(); private Food food = new Food();
private Knn knn;//KNN分类器 private Knn knn;//KNN分类器
private Knn[] knns;//集成knn分类
private int knnNub = 7;//KNN投票人数 private int knnNub = 7;//KNN投票人数
private ThreeChannelMatrix backGround;//背景面板 private ThreeChannelMatrix backGround;//背景面板
private double backGroundError;//背景误差偏移量0-255 private double backGroundError;//背景误差偏移量0-255
private Cutting cutting = new Cutting(); private Cutting cutting = new Cutting();
public Knn[] getKnns() {
return knns;
}
public Cutting getCutting() { public Cutting getCutting() {
return cutting; return cutting;
} }
@ -330,12 +325,7 @@ public class TempleConfig {
vectorK = new VectorK(featureNub * 3); vectorK = new VectorK(featureNub * 3);
break; break;
case Classifier.KNN: case Classifier.KNN:
int integrate = food.getIntegrate();//集成分类次数 knn = new Knn(knnNub);
knns = new Knn[integrate];
for (int i = 0; i < integrate; i++) {
knns[i] = new Knn(knnNub);
}
//knn = new Knn(knnNub);
break; break;
} }
break; break;

@ -7,26 +7,17 @@ package org.wlld.param;
* @Description * @Description
*/ */
public class Food { public class Food {
private double shrink = 0.03;//收缩参数 private int shrink = 60;//收缩参数
private int times = 10;//聚类增强次数 private int times = 10;//聚类增强次数
private int integrate = 5;//聚类集成次数
public double getShrink() { public int getShrink() {
return shrink; return shrink;
} }
public void setShrink(double shrink) { public void setShrink(int shrink) {
this.shrink = shrink; this.shrink = shrink;
} }
public int getIntegrate() {
return integrate;
}
public void setIntegrate(int integrate) {
this.integrate = integrate;
}
public int getTimes() { public int getTimes() {
return times; return times;
} }

@ -51,8 +51,8 @@ public class FoodTest {
specifications.setMaxHeight(750); specifications.setMaxHeight(750);
specificationsList.add(specifications); specificationsList.add(specifications);
Operation operation = new Operation(templeConfig); Operation operation = new Operation(templeConfig);
for (int i = 1; i <= 1; i++) { for (int i = 1; i <= 28; i++) {
ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix("D:\\share\\cai\\g/g2.jpg"); ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix("D:\\share\\cai\\g/g" + i + ".jpg");
List<RegionBody> regionBody = operation.colorLook(threeChannelMatrix1, specificationsList); List<RegionBody> regionBody = operation.colorLook(threeChannelMatrix1, specificationsList);
for (int j = 0; j < regionBody.size(); j++) { for (int j = 0; j < regionBody.size(); j++) {
RegionBody regionBody1 = regionBody.get(j); RegionBody regionBody1 = regionBody.get(j);
@ -80,7 +80,7 @@ public class FoodTest {
//聚类 //聚类
templeConfig.setFeatureNub(3);//聚类特征数量 templeConfig.setFeatureNub(3);//聚类特征数量
//菜品识别实体类 //菜品识别实体类
//food.setShrink(50);//缩紧像素 food.setShrink(60);//缩紧像素
food.setTimes(2);//聚类数据增强 food.setTimes(2);//聚类数据增强
templeConfig.setClassifier(Classifier.KNN); templeConfig.setClassifier(Classifier.KNN);
templeConfig.init(StudyPattern.Cover_Pattern, true, 400, 400, 3); templeConfig.init(StudyPattern.Cover_Pattern, true, 400, 400, 3);

Loading…
Cancel
Save