diff --git a/src/main/java/org/wlld/imageRecognition/Operation.java b/src/main/java/org/wlld/imageRecognition/Operation.java index 13c9495..b20c0bd 100644 --- a/src/main/java/org/wlld/imageRecognition/Operation.java +++ b/src/main/java/org/wlld/imageRecognition/Operation.java @@ -16,6 +16,7 @@ import org.wlld.nerveEntity.SensoryNerve; import org.wlld.tools.ArithUtil; import org.wlld.tools.IdCreator; +import javax.swing.text.html.parser.Entity; import java.util.*; public class Operation {//进行计算 @@ -24,7 +25,7 @@ public class Operation {//进行计算 private MatrixBack matrixBack = new MatrixBack(); private ImageBack imageBack = new ImageBack(); private OutBack outBack; - private int dif; + private double dif; public Operation(TempleConfig templeConfig) { this.templeConfig = templeConfig; @@ -63,45 +64,55 @@ public class Operation {//进行计算 List regionBodies = watershed.rainfall(); if (regionBodies.size() == 1) { RegionBody regionBody = regionBodies.get(0); - int minX = regionBody.getMinX() + dif; - int minY = regionBody.getMinY() + dif; - int maxX = regionBody.getMaxX() - dif; - int maxY = regionBody.getMaxY() - dif; + int integrate = templeConfig.getFood().getIntegrate(); + 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); - //convolution.filtering(threeChannelMatrix1);//光照过滤 - int times = templeConfig.getFood().getTimes(); - for (int i = 0; i < times; i++) { - List feature = convolution.getCenterColor(threeChannelMatrix1, templeConfig.getPoolSize(), - templeConfig.getFeatureNub()); - if (templeConfig.isShowLog()) { - System.out.println(tag + ":" + feature); - } - //System.out.println("====================================="); - int classifier = templeConfig.getClassifier(); - switch (classifier) { - case Classifier.DNN: - Map 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; + for (int j = 1; j <= integrate; j++) { + double move = dif * j; + int moveX = (int) (xSize * move); + int moveY = (int) (ySize * move); + int x = minX + moveX; + 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);//光照过滤 + int times = templeConfig.getFood().getTimes(); + for (int i = 0; i < times; i++) { + List feature = convolution.getCenterColor(threeChannelMatrix1, templeConfig.getPoolSize(), + templeConfig.getFeatureNub()); + if (templeConfig.isShowLog()) { + System.out.println(tag + ":" + feature); + } + //System.out.println("====================================="); + int classifier = templeConfig.getClassifier(); + switch (classifier) { + case Classifier.DNN: + Map 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, templeConfig.getKnns()[j]); + break; + } } } } else { @@ -109,8 +120,7 @@ public class Operation {//进行计算 } } - private void knnStudy(int tagging, Matrix vector) throws Exception { - Knn knn = templeConfig.getKnn(); + private void knnStudy(int tagging, Matrix vector, Knn knn) throws Exception { knn.insertMatrix(vector, tagging); } @@ -132,45 +142,70 @@ public class Operation {//进行计算 List regionList = watershed.rainfall(); for (RegionBody regionBody : regionList) { MaxPoint maxPoint = new MaxPoint(); - int minX = regionBody.getMinX() + dif; - int minY = regionBody.getMinY() + dif; - int maxX = regionBody.getMaxX() - dif; - int maxY = regionBody.getMaxY() - dif; + int integrate = templeConfig.getFood().getIntegrate(); + 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); - //convolution.filtering(threeChannelMatrix1);//光照过滤 - List feature = convolution.getCenterColor(threeChannelMatrix1, templeConfig.getPoolSize(), - templeConfig.getFeatureNub()); - if (templeConfig.isShowLog()) { - System.out.println(feature); + Map map = new HashMap<>(); + for (int j = 1; j <= integrate; j++) { + double move = dif * j; + int moveX = (int) (xSize * move); + int moveY = (int) (ySize * move); + int x = minX + moveX; + 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 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 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.getKnn(); - id = knn.getType(myMatrix2); - break; + int key = 0; + int max = 0; + for (Map.Entry entry : map.entrySet()) { + int nub = entry.getValue(); + if (nub > max) { + key = entry.getKey(); + } } - regionBody.setType(id); + regionBody.setType(key); //System.out.println("类别" + id); } return regionList; diff --git a/src/main/java/org/wlld/imageRecognition/TempleConfig.java b/src/main/java/org/wlld/imageRecognition/TempleConfig.java index 87a033f..e158ef2 100644 --- a/src/main/java/org/wlld/imageRecognition/TempleConfig.java +++ b/src/main/java/org/wlld/imageRecognition/TempleConfig.java @@ -66,11 +66,16 @@ public class TempleConfig { private int featureNub = 4;//聚类特征数量 private Food food = new Food(); private Knn knn;//KNN分类器 + private Knn[] knns;//集成knn分类 private int knnNub = 7;//KNN投票人数 private ThreeChannelMatrix backGround;//背景面板 private double backGroundError;//背景误差偏移量,0-255 private Cutting cutting = new Cutting(); + public Knn[] getKnns() { + return knns; + } + public Cutting getCutting() { return cutting; } @@ -325,7 +330,12 @@ public class TempleConfig { vectorK = new VectorK(featureNub * 3); break; case Classifier.KNN: - knn = new Knn(knnNub); + int integrate = food.getIntegrate();//集成分类次数 + knns = new Knn[integrate]; + for (int i = 0; i < integrate; i++) { + knns[i] = new Knn(knnNub); + } + //knn = new Knn(knnNub); break; } break; diff --git a/src/main/java/org/wlld/param/Food.java b/src/main/java/org/wlld/param/Food.java index 8cea9cc..2ff9382 100644 --- a/src/main/java/org/wlld/param/Food.java +++ b/src/main/java/org/wlld/param/Food.java @@ -7,17 +7,26 @@ package org.wlld.param; * @Description 菜品识别实体类 */ public class Food { - private int shrink = 60;//收缩参数 + private double shrink = 0.03;//收缩参数 private int times = 10;//聚类增强次数 + private int integrate = 5;//聚类集成次数 - public int getShrink() { + public double getShrink() { return shrink; } - public void setShrink(int shrink) { + public void setShrink(double shrink) { this.shrink = shrink; } + public int getIntegrate() { + return integrate; + } + + public void setIntegrate(int integrate) { + this.integrate = integrate; + } + public int getTimes() { return times; } diff --git a/src/test/java/coverTest/FoodTest.java b/src/test/java/coverTest/FoodTest.java index d9cd62e..4b6def9 100644 --- a/src/test/java/coverTest/FoodTest.java +++ b/src/test/java/coverTest/FoodTest.java @@ -80,7 +80,7 @@ public class FoodTest { //聚类 templeConfig.setFeatureNub(3);//聚类特征数量 //菜品识别实体类 - food.setShrink(50);//缩紧像素 + //food.setShrink(50);//缩紧像素 food.setTimes(2);//聚类数据增强 templeConfig.setClassifier(Classifier.KNN); templeConfig.init(StudyPattern.Cover_Pattern, true, 400, 400, 3);