From 36a63eb5122254ca9a38f2fcad82f7b648c29f66 Mon Sep 17 00:00:00 2001 From: lidapeng <794757862@qq.com> Date: Mon, 9 Nov 2020 14:56:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A4=9A=E7=A7=8D=E7=B1=BB?= =?UTF-8?q?=E5=B9=B2=E9=A3=9F=E5=88=87=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wlld/imageRecognition/Convolution.java | 12 +++-- .../org/wlld/imageRecognition/CutFood.java | 45 ++++++------------- .../org/wlld/imageRecognition/Operation.java | 12 ++--- .../org/wlld/imageRecognition/RGBNorm.java | 12 +++++ .../wlld/imageRecognition/TempleConfig.java | 1 - .../imageRecognition/border/GMClustering.java | 2 +- .../segmentation/FeatureMapping.java | 2 +- src/main/java/org/wlld/param/Food.java | 1 - src/test/java/coverTest/FoodTest.java | 4 -- 9 files changed, 42 insertions(+), 49 deletions(-) diff --git a/src/main/java/org/wlld/imageRecognition/Convolution.java b/src/main/java/org/wlld/imageRecognition/Convolution.java index d336f8a..017c56a 100644 --- a/src/main/java/org/wlld/imageRecognition/Convolution.java +++ b/src/main/java/org/wlld/imageRecognition/Convolution.java @@ -102,7 +102,6 @@ public class Convolution extends Frequency { if (foodTypes == null) { foods = new int[]{type}; } else { - foods = new int[foodTypes.length + 1]; boolean isLife = false; for (int myType : foodTypes) { if (type == myType) { @@ -111,9 +110,14 @@ public class Convolution extends Frequency { } } if (!isLife) { - for (int i = 0; i < foodTypes.length; i++) { - foods[i] = foodTypes[i]; - } + foods = new int[foodTypes.length + 1]; + } else { + foods = new int[foodTypes.length]; + } + for (int i = 0; i < foodTypes.length; i++) { + foods[i] = foodTypes[i]; + } + if (!isLife) { foods[foodTypes.length] = type; } } diff --git a/src/main/java/org/wlld/imageRecognition/CutFood.java b/src/main/java/org/wlld/imageRecognition/CutFood.java index 5b1e2b5..566d4d2 100644 --- a/src/main/java/org/wlld/imageRecognition/CutFood.java +++ b/src/main/java/org/wlld/imageRecognition/CutFood.java @@ -56,7 +56,7 @@ public class CutFood { return sigma / (x * y); } - private boolean getAvgPro(ThreeChannelMatrix threeChannelMatrix, GMClustering gm) throws Exception { + private void getAvgPro(ThreeChannelMatrix threeChannelMatrix) throws Exception { int size = templeConfig.getFood().getRegionSize(); Matrix matrixR = threeChannelMatrix.getMatrixR(); Matrix matrixG = threeChannelMatrix.getMatrixG(); @@ -64,35 +64,25 @@ public class CutFood { int x = matrixR.getX(); int y = matrixR.getY(); regionMap = new Matrix(x / size, y / size); - double sigmaPro = 0; - double sigmaOther = 0; for (int i = 0; i <= x - size; i += size) { for (int j = 0; j <= y - size; j += size) { double r = getAvg(matrixR.getSonOfMatrix(i, j, size, size)); double g = getAvg(matrixG.getSonOfMatrix(i, j, size, size)); double b = getAvg(matrixB.getSonOfMatrix(i, j, size, size)); double[] rgb = new double[]{r / 255, g / 255, b / 255}; - TypePro typePro = getType(rgb); - int index = typePro.index;//该区域所属类别 - sigmaPro = sigmaPro + typePro.pro; - if (gm != null) { - sigmaOther = sigmaOther + gm.getProbabilityDensity(rgb);//概率密度 - } + int index = getType(rgb); regionMap.setNub(i / size, j / size, index); } } - return sigmaPro > sigmaOther; } - public Map getTypeNub(ThreeChannelMatrix threeChannelMatrix, - GMClustering gm) throws Exception { - if (getAvgPro(threeChannelMatrix, gm)) {//进行二次判定 - return getTypeNub(); - } - return null; + public Map getTypeNub(ThreeChannelMatrix threeChannelMatrix + , int myType) throws Exception { + getAvgPro(threeChannelMatrix); + return getTypeNub(myType); } - private Map getTypeNub() throws Exception { + private Map getTypeNub(int myType) throws Exception { int size = templeConfig.getFood().getRegionSize(); double s = Math.pow(size, 2); int xr = regionMap.getX(); @@ -113,7 +103,7 @@ public class CutFood { int type = gmBody.getType(); if (type != 1) {//背景直接过滤 double oneSize = meanMap.get(type).getRegionSize(); - int nub = (int) (regionSize / oneSize); + int nub = (int) (regionSize / oneSize);//数量 if (nub > 0) { if (gmTypeNub.containsKey(type)) { int myNub = gmTypeNub.get(type); @@ -126,6 +116,9 @@ public class CutFood { } } } + if (gmTypeNub.size() == 0) { + gmTypeNub.put(myType, 1); + } return gmTypeNub; } @@ -140,17 +133,7 @@ public class CutFood { return isInsert; } - class TypePro { - private int index; - private double pro;//概率密度 - - TypePro(int index, double pro) { - this.index = index; - this.pro = pro; - } - } - - private TypePro getType(double[] rgb) throws Exception { + private int getType(double[] rgb) throws Exception { int index = 0; double max = 0; for (Map.Entry entry : meanMap.entrySet()) { @@ -163,10 +146,8 @@ public class CutFood { } if (max < 2) { index = 1; - max = 0; } - - return new TypePro(index, max); + return index; } public void study(int type, ThreeChannelMatrix threeChannelMatrix) throws Exception { diff --git a/src/main/java/org/wlld/imageRecognition/Operation.java b/src/main/java/org/wlld/imageRecognition/Operation.java index 0d437cf..aeeb7a0 100644 --- a/src/main/java/org/wlld/imageRecognition/Operation.java +++ b/src/main/java/org/wlld/imageRecognition/Operation.java @@ -167,11 +167,13 @@ public class Operation {//进行计算 } } } - CutFood cutFood = new CutFood(templeConfig, food.getFoodMeanMap()); - if (isFood) {//一次判定就属于干食,则无需进行二次判定 - regionBody.setTypeNub(cutFood.getTypeNub(threeChannelMatrix1, null)); - } else {//一次判定属于非干食,则进行二次判定 - regionBody.setTypeNub(cutFood.getTypeNub(threeChannelMatrix1, food.getNotFoodMeanMap().get(id))); + if (isFood) {//判断类别,获取积分 + Map foods = food.getFoodMeanMap(); + Map test = new HashMap<>(); + test.put(1, foods.get(1)); + test.put(id, foods.get(id)); + CutFood cutFood = new CutFood(templeConfig, test); + regionBody.setTypeNub(cutFood.getTypeNub(threeChannelMatrix1, id)); } regionBody.setType(id); System.out.println("类别" + id); diff --git a/src/main/java/org/wlld/imageRecognition/RGBNorm.java b/src/main/java/org/wlld/imageRecognition/RGBNorm.java index 13825bb..561ca59 100644 --- a/src/main/java/org/wlld/imageRecognition/RGBNorm.java +++ b/src/main/java/org/wlld/imageRecognition/RGBNorm.java @@ -22,6 +22,18 @@ public class RGBNorm { private double gmParameter;//gm混合系数 private double probabilitySigma = 0;//后验概率求和 + public double getVarAll() { + return varAll; + } + + public Matrix getAvgMatrix() { + return avgMatrix; + } + + public Matrix getVarMatrix() { + return varMatrix; + } + public List getRgbs() { return rgbs; } diff --git a/src/main/java/org/wlld/imageRecognition/TempleConfig.java b/src/main/java/org/wlld/imageRecognition/TempleConfig.java index e03e1d3..bf927e1 100644 --- a/src/main/java/org/wlld/imageRecognition/TempleConfig.java +++ b/src/main/java/org/wlld/imageRecognition/TempleConfig.java @@ -707,7 +707,6 @@ public class TempleConfig { food.getNotFoodMeanMap().put(key, gmClustering); } } - // DimensionMappingStudy dimensionAll = new DimensionMappingStudy(this); dimensionAll.start();//生成映射层,并将已经保存的knn特征进行映射 food.setDimensionMappingStudy(dimensionAll); diff --git a/src/main/java/org/wlld/imageRecognition/border/GMClustering.java b/src/main/java/org/wlld/imageRecognition/border/GMClustering.java index fec5c71..17b6264 100644 --- a/src/main/java/org/wlld/imageRecognition/border/GMClustering.java +++ b/src/main/java/org/wlld/imageRecognition/border/GMClustering.java @@ -53,7 +53,7 @@ public class GMClustering extends MeanClustering { RGBNorm rgbNorm = new RGBNorm(); matrices.add(rgbNorm); for (int j = i; j < i + size; j++) { - feature[j - i] = matrix.getNumber(0, i); + feature[j - i] = matrix.getNumber(0, j); } rgbNorm.insertFeature(feature); } diff --git a/src/main/java/org/wlld/imageRecognition/segmentation/FeatureMapping.java b/src/main/java/org/wlld/imageRecognition/segmentation/FeatureMapping.java index 1a3717b..5ad2a22 100644 --- a/src/main/java/org/wlld/imageRecognition/segmentation/FeatureMapping.java +++ b/src/main/java/org/wlld/imageRecognition/segmentation/FeatureMapping.java @@ -48,7 +48,7 @@ public class FeatureMapping extends Frequency implements PsoFunction { @Override public double getResult(double[] parameter, int id) throws Exception { - return featuresMapping2(parameter); + return featuresMapping(parameter); } private double[] mapping(Matrix matrix, double[] parameter) throws Exception { diff --git a/src/main/java/org/wlld/param/Food.java b/src/main/java/org/wlld/param/Food.java index d4b31f6..af5f904 100644 --- a/src/main/java/org/wlld/param/Food.java +++ b/src/main/java/org/wlld/param/Food.java @@ -1,6 +1,5 @@ package org.wlld.param; -import org.wlld.imageRecognition.CutFood; import org.wlld.imageRecognition.border.GMClustering; import org.wlld.imageRecognition.segmentation.DimensionMappingStudy; import org.wlld.imageRecognition.segmentation.RgbRegression; diff --git a/src/test/java/coverTest/FoodTest.java b/src/test/java/coverTest/FoodTest.java index 36292cb..0372feb 100644 --- a/src/test/java/coverTest/FoodTest.java +++ b/src/test/java/coverTest/FoodTest.java @@ -80,7 +80,6 @@ public class FoodTest { ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix(a); Watershed watershed = new Watershed(threeChannelMatrix1, specificationsList, templeConfig); List regionList = watershed.rainfall(); - System.out.println("=="); } private static void look(ThreeChannelMatrix threeChannelMatrix, TempleConfig templeConfig, List specifications, @@ -95,9 +94,6 @@ public class FoodTest { int maxY = regionBody.getMaxY(); int xSize = maxX - minX; int ySize = maxY - minY; - ThreeChannelMatrix threeChannelMatrix1 = convolution.getRegionMatrix(threeChannelMatrix, minX, minY, xSize, ySize); - cutFood.getTypeNub(threeChannelMatrix1, null); - System.out.println("结束==================="); } }