diff --git a/src/main/java/org/wlld/imageRecognition/CutFood.java b/src/main/java/org/wlld/imageRecognition/CutFood.java new file mode 100644 index 0000000..f75d941 --- /dev/null +++ b/src/main/java/org/wlld/imageRecognition/CutFood.java @@ -0,0 +1,147 @@ +package org.wlld.imageRecognition; + + +import org.wlld.MatrixTools.Matrix; +import org.wlld.imageRecognition.border.GMClustering; +import org.wlld.imageRecognition.modelEntity.GMBody; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @param + * @DATA + * @Author LiDaPeng + * @Description + */ +public class CutFood { + private TempleConfig templeConfig; + private Map meanMap = new HashMap<>(); + private Matrix regionMap; + + public CutFood(TempleConfig templeConfig) { + this.templeConfig = templeConfig; + } + + private void mean(ThreeChannelMatrix threeChannelMatrix, GMClustering mean) 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[] rgb = new double[]{matrixR.getNumber(i, j) / 255, matrixG.getNumber(i, j) / 255 + , matrixB.getNumber(i, j) / 255}; + mean.setColor(rgb); + } + } + mean.start(true); + } + + private double getAvg(Matrix matrix) throws Exception { + double sigma = 0; + int x = matrix.getX(); + int y = matrix.getY(); + for (int i = 0; i < x; i++) { + for (int j = 0; j < y; j++) { + sigma = sigma + matrix.getNumber(i, j); + } + } + return sigma / (x * y); + } + + public void createRegion(ThreeChannelMatrix threeChannelMatrix) throws Exception { + int size = templeConfig.getFood().getRegionSize(); + Matrix matrixR = threeChannelMatrix.getMatrixR(); + Matrix matrixG = threeChannelMatrix.getMatrixG(); + Matrix matrixB = threeChannelMatrix.getMatrixB(); + int x = matrixR.getX(); + int y = matrixR.getY(); + double s = Math.pow(size, 2); + regionMap = new Matrix(x / size, y / size); + 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}; + int index = getType(rgb);//该区域所属类别 + regionMap.setNub(i / size, j / size, index); + } + } + int xr = regionMap.getX(); + int yr = regionMap.getY(); + List gmBodies = new ArrayList<>(); + for (int i = 0; i < xr; i++) { + for (int j = 0; j < yr; j++) { + int type = (int) regionMap.getNumber(i, j); + if (!insertBodies(gmBodies, i, j, type)) {//需要创建一个新的 + gmBodies.add(new GMBody(type, i, j)); + } + } + } + List gmBodies2 = new ArrayList<>(); + for (int i = 0; i < gmBodies.size(); i++) {//过滤侯选区 + GMBody gmBody = gmBodies.get(i); + double regionSize = gmBody.getPixelNub() * s; + int type = gmBody.getType(); + if (type != 1) {//背景直接过滤 + int oneSize = meanMap.get(type).getRegionSize(); + if (regionSize > oneSize * 0.8) { + gmBodies2.add(gmBody); + } + } + } + for (GMBody gmBody : gmBodies2) { + int type = gmBody.getType(); + double regionSize = gmBody.getPixelNub() * s; + int oneSize = meanMap.get(type).getRegionSize(); + double nub = regionSize / (double) oneSize; + System.out.println("type==" + type + ",nub==" + nub + ",onSize==" + oneSize + ",gmNub==" + + gmBody.getPixelNub()); + } + + } + + private boolean insertBodies(List gmBodies, int x, int y, int type) { + boolean isInsert = false; + for (GMBody gmBody : gmBodies) { + if (gmBody.insertRgb(x, y, type)) { + isInsert = true; + break; + } + } + return isInsert; + } + + private int getType(double[] rgb) throws Exception { + int index = 0; + double max = 0; + for (Map.Entry entry : meanMap.entrySet()) { + GMClustering gmClustering = entry.getValue(); + double probability = gmClustering.getProbabilityDensity(rgb); + if (probability > max) { + max = probability; + index = entry.getKey(); + } + } + if (max < 2) { + index = 1; + } + return index; + } + + public void study(int type, ThreeChannelMatrix threeChannelMatrix) throws Exception { + Matrix matrixR = threeChannelMatrix.getMatrixR(); + int x = matrixR.getX(); + int y = matrixR.getY(); + GMClustering mean = new GMClustering(templeConfig.getFeatureNub(), templeConfig); + mean.setRegionSize(x * y); + meanMap.put(type, mean); + mean(threeChannelMatrix, mean); + //记录非背景的单物体面积 + } +} diff --git a/src/main/java/org/wlld/imageRecognition/Operation.java b/src/main/java/org/wlld/imageRecognition/Operation.java index db4744b..0fc3f62 100644 --- a/src/main/java/org/wlld/imageRecognition/Operation.java +++ b/src/main/java/org/wlld/imageRecognition/Operation.java @@ -131,10 +131,9 @@ public class Operation {//进行计算 int minY = regionBody.getMinY(); int maxX = regionBody.getMaxX(); int maxY = regionBody.getMaxY(); - System.out.println("异常:minX==" + minX + ",minY==" + minY + ",maxX==" + maxX + ",maxY==" + maxY + ",tag==" + tag - + "url==" + url); + System.out.println("异常:minX==" + minX + ",minY==" + minY + ",maxX==" + maxX + ",maxY==" + maxY + ",tag==" + tag); } - throw new Exception("Parameter exception region size==" + regionBodies.size()); + throw new Exception("Parameter exception region size==" + regionBodies.size() + ",url" + url); } } diff --git a/src/main/java/org/wlld/imageRecognition/RGBNorm.java b/src/main/java/org/wlld/imageRecognition/RGBNorm.java index e493fbe..f7a516c 100644 --- a/src/main/java/org/wlld/imageRecognition/RGBNorm.java +++ b/src/main/java/org/wlld/imageRecognition/RGBNorm.java @@ -202,7 +202,7 @@ public class RGBNorm { } } - public double getGMProbability(double[] feature) throws Exception {//计算正态分布概率 + public double getGMProbability(double[] feature) throws Exception {//计算正态分布概率密度 double zSigma = 0; int size = feature.length; for (int i = 0; i < size; i++) { diff --git a/src/main/java/org/wlld/imageRecognition/border/GMClustering.java b/src/main/java/org/wlld/imageRecognition/border/GMClustering.java index be90732..9d574ef 100644 --- a/src/main/java/org/wlld/imageRecognition/border/GMClustering.java +++ b/src/main/java/org/wlld/imageRecognition/border/GMClustering.java @@ -4,7 +4,6 @@ import org.wlld.imageRecognition.MeanClustering; import org.wlld.imageRecognition.RGBNorm; import org.wlld.imageRecognition.TempleConfig; -import java.util.Arrays; /** * @param @@ -13,11 +12,28 @@ import java.util.Arrays; * @Description */ public class GMClustering extends MeanClustering { + private int regionSize;//单区域面积 + + public int getRegionSize() { + return regionSize; + } + + public void setRegionSize(int regionSize) { + this.regionSize = regionSize; + } public GMClustering(int speciesQuantity, TempleConfig templeConfig) throws Exception { super(speciesQuantity, templeConfig); } + public double getProbabilityDensity(double[] feature) throws Exception {//获取总概率密度 + double sigma = 0; + for (RGBNorm rgbNorm : matrices) { + sigma = sigma + rgbNorm.getGMProbability(feature); + } + return sigma; + } + @Override public void start(boolean isRegression) throws Exception { super.start(isRegression); diff --git a/src/main/java/org/wlld/imageRecognition/modelEntity/GMBody.java b/src/main/java/org/wlld/imageRecognition/modelEntity/GMBody.java new file mode 100644 index 0000000..f35b3f0 --- /dev/null +++ b/src/main/java/org/wlld/imageRecognition/modelEntity/GMBody.java @@ -0,0 +1,55 @@ +package org.wlld.imageRecognition.modelEntity; + +import java.util.ArrayList; +import java.util.List; + +/** + * @param + * @DATA + * @Author LiDaPeng + * @Description 混合高斯切割区域 + */ +public class GMBody { + private int type;//类别 + private List pixels = new ArrayList<>(); + private int pixelNub = 0; + + public GMBody(int type, int x, int y) { + this.type = type; + pixelNub++; + pixels.add((x << 12) | y); + } + + public int getPixelNub() { + return pixelNub; + } + + public int getType() { + return type; + } + + private boolean isAdjacent(int x, int y, int i, int j) { + boolean adjacent = false; + if (Math.abs(x - i) == 1 || Math.abs(y - j) == 1) { + adjacent = true; + } + return adjacent; + } + + public boolean insertRgb(int x, int y, int type) { + boolean isRight = false; + if (this.type == type) { + for (int pixel : pixels) { + int i = (pixel >> 12) & 0xfff; + int j = pixel & 0xfff; + if (isAdjacent(x, y, i, j)) {//相邻 + isRight = true; + pixelNub++; + pixels.add((x << 12) | y); + break; + } + } + } + return isRight; + } +} diff --git a/src/main/java/org/wlld/imageRecognition/segmentation/Watershed.java b/src/main/java/org/wlld/imageRecognition/segmentation/Watershed.java index 4885282..d5402f3 100644 --- a/src/main/java/org/wlld/imageRecognition/segmentation/Watershed.java +++ b/src/main/java/org/wlld/imageRecognition/segmentation/Watershed.java @@ -75,7 +75,7 @@ public class Watershed { private boolean isTray(int x, int y) throws Exception { boolean isTray = false; - if (trayBody != null && trayBody.size() > 0) { + if (trayBody.size() > 0) { double[] rgb = new double[]{matrixR.getNumber(x, y) / 255, matrixG.getNumber(x, y) / 255, matrixB.getNumber(x, y) / 255}; for (RgbRegression rgbRegression : trayBody) { @@ -279,14 +279,16 @@ public class Watershed { regionBodies.add(regionBody); } } - for (RegionBody regionBody : regionBodies) { + regionBodies = iou(regionBodies); + for (int i = 0; i < regionBodies.size(); i++) { + RegionBody regionBody = regionBodies.get(i); int minX = regionBody.getMinX(); int maxX = regionBody.getMaxX(); int minY = regionBody.getMinY(); int maxY = regionBody.getMaxY(); - System.out.println("minX==" + minX + ",minY==" + minY + ",maxX==" + maxX + ",maxY==" + maxY); + System.out.println("minX==" + minX + ",maxX==" + maxX + ",minY==" + minY + ",maxY==" + maxY); } - return iou(regionBodies); + return regionBodies; } private List iou(List regionBodies) { @@ -361,7 +363,7 @@ public class Watershed { double left = this.width / edgeSize;//左边缘界限 double bottom = this.height - top;//下边缘界限 double right = this.width - left;//右边缘界限 - isCenter = maxX > top && maxY > left && minX < bottom && minY < right; + isCenter = minX > top && minY > left && minX < bottom && minY < right; } if (width >= specification.getMinWidth() && height >= specification.getMinHeight() && width <= specification.getMaxWidth() && height <= specification.getMaxHeight() @@ -494,7 +496,7 @@ public class Watershed { int minIdx = 0; for (int i = 0; i < array.length; i++) { double nub = array[i]; - if (nub > -1 && nub < mySelf) { + if (nub > -1 && nub < mySelf && nub < 280) { minIdx = minIdx | (1 << i); } } diff --git a/src/test/java/coverTest/FoodTest.java b/src/test/java/coverTest/FoodTest.java index 6865073..65e1c70 100644 --- a/src/test/java/coverTest/FoodTest.java +++ b/src/test/java/coverTest/FoodTest.java @@ -31,8 +31,8 @@ public class FoodTest { Picture picture = new Picture(); List specificationsList = new ArrayList<>(); Specifications specifications = new Specifications(); - specifications.setMinWidth(100); - specifications.setMinHeight(100); + specifications.setMinWidth(70); + specifications.setMinHeight(70); specifications.setMaxWidth(950); specifications.setMaxHeight(950); specificationsList.add(specifications); @@ -54,30 +54,29 @@ 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.8); + cutting.setTh(0.5); cutting.setRegionNub(100); cutting.setMaxIou(2); + templeConfig.setEdge(15); //knn参数 templeConfig.setKnnNub(1); //池化比例 templeConfig.setPoolSize(2);//缩小比例 //聚类 - templeConfig.setFeatureNub(2);//聚类特征数量 + templeConfig.setFeatureNub(3);//聚类特征数量 //菜品识别实体类 food.setShrink(0);//缩紧像素 - food.setRegionSize(6); + food.setRegionSize(2); KNerveManger kNerveManger = new KNerveManger(12, 24, 6000); food.setkNerveManger(kNerveManger); - food.setRowMark(0.15);//0.12 - food.setColumnMark(0.15);//0.25 - food.setRegressionNub(20000); - food.setTrayTh(0.08); + food.setRowMark(0.05);//0.12 + food.setColumnMark(0.05);//0.25 + food.setRegressionNub(50000); + food.setTrayTh(0.05);//封死 templeConfig.setClassifier(Classifier.KNN); templeConfig.init(StudyPattern.Cover_Pattern, true, 400, 400, 3); if (modelParameter != null) { @@ -92,152 +91,96 @@ public class FoodTest { Operation operation = new Operation(templeConfig); List specificationsList = new ArrayList<>(); Specifications specifications = new Specifications(); - specifications.setMinWidth(150);//150 - specifications.setMinHeight(150);//150 + specifications.setMinWidth(60);//150 + specifications.setMinHeight(60);//150 specifications.setMaxWidth(600); specifications.setMaxHeight(600); specificationsList.add(specifications); + CutFood cutFood = new CutFood(templeConfig); //KNerveManger kNerveManger = templeConfig.getFood().getkNerveManger(); -// 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 < 4; 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, name); - operation.colorStudy(threeChannelMatrix2, 2, specificationsList, name); - operation.colorStudy(threeChannelMatrix3, 3, specificationsList, name); - operation.colorStudy(threeChannelMatrix4, 4, specificationsList, name); - operation.colorStudy(threeChannelMatrix5, 5, specificationsList, name); - operation.colorStudy(threeChannelMatrix6, 6, specificationsList, name); - operation.colorStudy(threeChannelMatrix7, 7, specificationsList, name); - operation.colorStudy(threeChannelMatrix8, 8, specificationsList, name); - operation.colorStudy(threeChannelMatrix9, 9, specificationsList, name); - operation.colorStudy(threeChannelMatrix10, 10, specificationsList, name); - operation.colorStudy(threeChannelMatrix11, 11, specificationsList, name); - operation.colorStudy(threeChannelMatrix12, 12, specificationsList, name); - operation.colorStudy(threeChannelMatrix13, 13, specificationsList, name); - operation.colorStudy(threeChannelMatrix14, 14, specificationsList, name); - operation.colorStudy(threeChannelMatrix15, 15, specificationsList, name); - operation.colorStudy(threeChannelMatrix16, 16, specificationsList, name); - operation.colorStudy(threeChannelMatrix17, 17, specificationsList, name); - operation.colorStudy(threeChannelMatrix18, 18, specificationsList, name); - operation.colorStudy(threeChannelMatrix19, 19, specificationsList, name); - operation.colorStudy(threeChannelMatrix20, 20, specificationsList, name); - operation.colorStudy(threeChannelMatrix21, 21, specificationsList, name); - operation.colorStudy(threeChannelMatrix22, 22, specificationsList, name); - operation.colorStudy(threeChannelMatrix23, 23, specificationsList, name); - operation.colorStudy(threeChannelMatrix24, 24, specificationsList, name); - } - DeepMappingBody deepMappingBody = new DeepMappingBody(templeConfig); - templeConfig.getFood().setDeepMappingBody(deepMappingBody); - //dimensionMappingStudy.selfTest(6);//检查 - //System.out.println("========================"); - // kNerveManger.startStudy(); - 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"); - operation.colorLook(threeChannelMatrix1, specificationsList); - operation.colorLook(threeChannelMatrix2, specificationsList); - operation.colorLook(threeChannelMatrix3, specificationsList); - operation.colorLook(threeChannelMatrix4, specificationsList); - operation.colorLook(threeChannelMatrix5, specificationsList); - operation.colorLook(threeChannelMatrix6, specificationsList); - operation.colorLook(threeChannelMatrix7, specificationsList); - operation.colorLook(threeChannelMatrix8, specificationsList); - operation.colorLook(threeChannelMatrix9, specificationsList); - operation.colorLook(threeChannelMatrix10, specificationsList); - operation.colorLook(threeChannelMatrix11, specificationsList); - operation.colorLook(threeChannelMatrix12, specificationsList); - operation.colorLook(threeChannelMatrix13, specificationsList); - operation.colorLook(threeChannelMatrix14, specificationsList); - operation.colorLook(threeChannelMatrix15, specificationsList); - operation.colorLook(threeChannelMatrix16, specificationsList); - operation.colorLook(threeChannelMatrix17, specificationsList); - operation.colorLook(threeChannelMatrix18, specificationsList); - operation.colorLook(threeChannelMatrix19, specificationsList); - operation.colorLook(threeChannelMatrix20, specificationsList); - operation.colorLook(threeChannelMatrix21, specificationsList); - operation.colorLook(threeChannelMatrix22, specificationsList); - operation.colorLook(threeChannelMatrix23, specificationsList); - operation.colorLook(threeChannelMatrix24, specificationsList); + ThreeChannelMatrix threeChannelMatrixB = picture.getThreeMatrix("/Users/lidapeng/Desktop/myDocument/d.jpg"); + //背景也是盘子 + ThreeChannelMatrix threeChannelMatrix = picture.getThreeMatrix("/Users/lidapeng/Desktop/myDocument/pan1.jpg"); + //设定背景回归 + operation.setTray(threeChannelMatrixB); + //学习背景 + cutFood.study(1, threeChannelMatrix); + //训练数据,单张馒头 + String a = "/Users/lidapeng/Desktop/test/testOne/a.jpg"; + //训练数据,一个鸡蛋 + String c = "/Users/lidapeng/Desktop/test/testOne/c.jpg"; + //测试数据,两个鸡蛋 + String d = "/Users/lidapeng/Desktop/test/testOne/d.jpg"; + //测试数据,两个馒头 + String b = "/Users/lidapeng/Desktop/test/testOne/b.jpg"; + String f = "/Users/lidapeng/Desktop/test/testOne/f.jpeg"; -// test3(threeChannelMatrix1, operation, specificationsList, 1); -// test3(threeChannelMatrix2, operation, specificationsList, 2); -// test3(threeChannelMatrix3, operation, specificationsList, 3); -// test3(threeChannelMatrix4, operation, specificationsList, 4); -// test3(threeChannelMatrix5, operation, specificationsList, 5); -// test3(threeChannelMatrix6, operation, specificationsList, 6); -// test3(threeChannelMatrix7, operation, specificationsList, 7); -// test3(threeChannelMatrix8, operation, specificationsList, 8); -// test3(threeChannelMatrix9, operation, specificationsList, 9); -// test3(threeChannelMatrix10, operation, specificationsList, 10); -// test3(threeChannelMatrix11, operation, specificationsList, 11); -// test3(threeChannelMatrix12, operation, specificationsList, 12); -// test3(threeChannelMatrix13, operation, specificationsList, 13); -// test3(threeChannelMatrix14, operation, specificationsList, 14); -// test3(threeChannelMatrix15, operation, specificationsList, 15); -// test3(threeChannelMatrix16, operation, specificationsList, 16); -// test3(threeChannelMatrix17, operation, specificationsList, 17); -// test3(threeChannelMatrix18, operation, specificationsList, 18); -// test3(threeChannelMatrix19, operation, specificationsList, 19); -// test3(threeChannelMatrix20, operation, specificationsList, 20); -// test3(threeChannelMatrix21, operation, specificationsList, 21); -// test3(threeChannelMatrix22, operation, specificationsList, 22); -// test3(threeChannelMatrix23, operation, specificationsList, 23); -// test3(threeChannelMatrix24, operation, specificationsList, 24); + //测试数据,一个鸡蛋和一个馒头 + String e = "/Users/lidapeng/Desktop/test/testOne/e.jpg"; + //测试数据,两个鸡蛋 一个馒头 + String g = "/Users/lidapeng/Desktop/test/testOne/f.jpg"; + //测试数据,两个鸡蛋两个馒头 + String h = "/Users/lidapeng/Desktop/test/testOne/g.jpg"; + //训练馒头 + ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix(a); + //训练鸡蛋 + ThreeChannelMatrix threeChannelMatrix2 = picture.getThreeMatrix(c); + study(threeChannelMatrix1, templeConfig, specificationsList, cutFood, 2); + study(threeChannelMatrix2, templeConfig, specificationsList, cutFood, 3); + /// + ThreeChannelMatrix threeChannelMatrix3 = picture.getThreeMatrix(g); + long time1 = System.currentTimeMillis(); + look(threeChannelMatrix3, templeConfig, specificationsList, cutFood); + long end = System.currentTimeMillis() - time1; + System.out.println("time:" + end); + } + + private static void look(ThreeChannelMatrix threeChannelMatrix, TempleConfig templeConfig, List specifications, + CutFood cutFood) throws Exception { + Convolution convolution = new Convolution(); + Watershed watershed = new Watershed(threeChannelMatrix, specifications, templeConfig); + List regionList = watershed.rainfall(); + for (RegionBody regionBody : regionList) { + 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); + cutFood.createRegion(threeChannelMatrix1); + System.out.println("结束==================="); + } } - private static void test3(ThreeChannelMatrix threeChannelMatrix, Operation operation, List specifications, - int realType) throws Exception { - int type = operation.colorLook(threeChannelMatrix, specifications).get(0).getType(); - System.out.println(type + ",realType==" + realType); + private static void study(ThreeChannelMatrix threeChannelMatrix, TempleConfig templeConfig, List specifications, + CutFood cutFood, int type) throws Exception { + Convolution convolution = new Convolution(); + Watershed watershed = new Watershed(threeChannelMatrix, specifications, templeConfig); + List regionList = watershed.rainfall(); + if (regionList.size() == 1) { + RegionBody regionBody = regionList.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; + System.out.println("正常:minX==" + minX + ",minY==" + minY + ",maxX==" + maxX + ",maxY==" + maxY); + ThreeChannelMatrix threeChannelMatrix1 = convolution.getRegionMatrix(threeChannelMatrix, minX, minY, xSize, ySize); + cutFood.study(type, threeChannelMatrix1); + } else { + for (RegionBody regionBody : regionList) { + 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:" + regionList.size()); + } + } public static void study() throws Exception {