From 50da4ed045489cca79260923ff553fe43ef59e85 Mon Sep 17 00:00:00 2001 From: thenk008 <794757862@qq.com> Date: Thu, 11 Jun 2020 15:03:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=81=A2=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/wlld/imageRecognition/Operation.java | 185 +++++++----------- .../wlld/imageRecognition/TempleConfig.java | 12 +- src/main/java/org/wlld/param/Food.java | 15 +- src/test/java/coverTest/FoodTest.java | 6 +- 4 files changed, 82 insertions(+), 136 deletions(-) diff --git a/src/main/java/org/wlld/imageRecognition/Operation.java b/src/main/java/org/wlld/imageRecognition/Operation.java index b20c0bd..13c9495 100644 --- a/src/main/java/org/wlld/imageRecognition/Operation.java +++ b/src/main/java/org/wlld/imageRecognition/Operation.java @@ -16,7 +16,6 @@ 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 {//进行计算 @@ -25,7 +24,7 @@ public class Operation {//进行计算 private MatrixBack matrixBack = new MatrixBack(); private ImageBack imageBack = new ImageBack(); private OutBack outBack; - private double dif; + private int dif; public Operation(TempleConfig templeConfig) { this.templeConfig = templeConfig; @@ -64,55 +63,45 @@ public class Operation {//进行计算 List regionBodies = watershed.rainfall(); if (regionBodies.size() == 1) { RegionBody regionBody = regionBodies.get(0); - int integrate = templeConfig.getFood().getIntegrate(); - int minX = regionBody.getMinX(); - int minY = regionBody.getMinY(); - int maxX = regionBody.getMaxX(); - int maxY = regionBody.getMaxY(); + int minX = regionBody.getMinX() + dif; + int minY = regionBody.getMinY() + dif; + int maxX = regionBody.getMaxX() - dif; + int maxY = regionBody.getMaxY() - dif; int xSize = maxX - minX; int ySize = maxY - minY; - 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; - } + 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; } } } 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); } @@ -142,70 +132,45 @@ public class Operation {//进行计算 List regionList = watershed.rainfall(); for (RegionBody regionBody : regionList) { MaxPoint maxPoint = new MaxPoint(); - int integrate = templeConfig.getFood().getIntegrate(); - int minX = regionBody.getMinX(); - int minY = regionBody.getMinY(); - int maxX = regionBody.getMaxX(); - int maxY = regionBody.getMaxY(); + int minX = regionBody.getMinX() + dif; + int minY = regionBody.getMinY() + dif; + int maxX = regionBody.getMaxX() - dif; + int maxY = regionBody.getMaxY() - dif; int xSize = maxX - minX; int ySize = maxY - minY; - 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); - } + 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); } - int key = 0; - int max = 0; - for (Map.Entry entry : map.entrySet()) { - int nub = entry.getValue(); - if (nub > max) { - key = entry.getKey(); - } + 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; } - regionBody.setType(key); + regionBody.setType(id); //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 e158ef2..87a033f 100644 --- a/src/main/java/org/wlld/imageRecognition/TempleConfig.java +++ b/src/main/java/org/wlld/imageRecognition/TempleConfig.java @@ -66,16 +66,11 @@ 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; } @@ -330,12 +325,7 @@ public class TempleConfig { vectorK = new VectorK(featureNub * 3); break; case Classifier.KNN: - int integrate = food.getIntegrate();//集成分类次数 - knns = new Knn[integrate]; - for (int i = 0; i < integrate; i++) { - knns[i] = new Knn(knnNub); - } - //knn = 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 2ff9382..8cea9cc 100644 --- a/src/main/java/org/wlld/param/Food.java +++ b/src/main/java/org/wlld/param/Food.java @@ -7,26 +7,17 @@ package org.wlld.param; * @Description 菜品识别实体类 */ public class Food { - private double shrink = 0.03;//收缩参数 + private int shrink = 60;//收缩参数 private int times = 10;//聚类增强次数 - private int integrate = 5;//聚类集成次数 - public double getShrink() { + public int getShrink() { return shrink; } - public void setShrink(double shrink) { + public void setShrink(int 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 4b6def9..08e0e0c 100644 --- a/src/test/java/coverTest/FoodTest.java +++ b/src/test/java/coverTest/FoodTest.java @@ -51,8 +51,8 @@ public class FoodTest { specifications.setMaxHeight(750); specificationsList.add(specifications); Operation operation = new Operation(templeConfig); - for (int i = 1; i <= 1; i++) { - ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix("D:\\share\\cai\\g/g2.jpg"); + for (int i = 1; i <= 28; i++) { + ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix("D:\\share\\cai\\g/g" + i + ".jpg"); List regionBody = operation.colorLook(threeChannelMatrix1, specificationsList); for (int j = 0; j < regionBody.size(); j++) { RegionBody regionBody1 = regionBody.get(j); @@ -80,7 +80,7 @@ public class FoodTest { //聚类 templeConfig.setFeatureNub(3);//聚类特征数量 //菜品识别实体类 - //food.setShrink(50);//缩紧像素 + food.setShrink(60);//缩紧像素 food.setTimes(2);//聚类数据增强 templeConfig.setClassifier(Classifier.KNN); templeConfig.init(StudyPattern.Cover_Pattern, true, 400, 400, 3);