From f399fac619c08181eff444431ff60337411fd7eb Mon Sep 17 00:00:00 2001 From: lidapeng <794757862@qq.com> Date: Wed, 21 Oct 2020 17:42:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=B2=92=E5=AD=90=E7=BE=A4?= =?UTF-8?q?=E7=89=B9=E5=BE=81=E7=AD=9B=E9=80=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/wlld/MatrixTools/Matrix.java | 1 + .../org/wlld/MatrixTools/MatrixOperation.java | 2 +- .../wlld/imageRecognition/Convolution.java | 150 ++++++------------ .../org/wlld/imageRecognition/Operation.java | 13 -- .../modelEntity/MappingBody.java | 35 ++++ .../segmentation/ColorFunction.java | 72 +++++++++ .../segmentation/DimensionMapping.java | 41 +++++ src/main/java/org/wlld/param/Food.java | 27 ++-- src/main/java/org/wlld/pso/PSO.java | 48 ++++-- src/test/java/coverTest/DataObservation.java | 6 +- src/test/java/coverTest/FoodTest.java | 2 - .../coverTest/regionCut/ColorFunction.java | 19 +++ .../java/coverTest/regionCut/RegionCut.java | 7 +- 13 files changed, 268 insertions(+), 155 deletions(-) create mode 100644 src/main/java/org/wlld/imageRecognition/modelEntity/MappingBody.java create mode 100644 src/main/java/org/wlld/imageRecognition/segmentation/ColorFunction.java create mode 100644 src/main/java/org/wlld/imageRecognition/segmentation/DimensionMapping.java diff --git a/src/main/java/org/wlld/MatrixTools/Matrix.java b/src/main/java/org/wlld/MatrixTools/Matrix.java index 8ee4155..14c22b8 100644 --- a/src/main/java/org/wlld/MatrixTools/Matrix.java +++ b/src/main/java/org/wlld/MatrixTools/Matrix.java @@ -56,6 +56,7 @@ public class Matrix { private void setState(int x, int y) { if (x == 1 && y == 1) { isZero = true; + isVector = true; } else if (x == 1 || y == 1) { isVector = true; if (x == 1) { diff --git a/src/main/java/org/wlld/MatrixTools/MatrixOperation.java b/src/main/java/org/wlld/MatrixTools/MatrixOperation.java index 0bc6765..dff2b67 100644 --- a/src/main/java/org/wlld/MatrixTools/MatrixOperation.java +++ b/src/main/java/org/wlld/MatrixTools/MatrixOperation.java @@ -46,7 +46,7 @@ public class MatrixOperation { //多元线性回归 public static Matrix getLinearRegression(Matrix parameter, Matrix out) throws Exception { - if (parameter.getX() == out.getX() && out.isVector() && !out.isRowVector()) { + if (parameter.getX() == out.getX() && out.isVector()) { //将参数矩阵转置 Matrix matrix1 = transPosition(parameter); //转置的参数矩阵乘以参数矩阵 diff --git a/src/main/java/org/wlld/imageRecognition/Convolution.java b/src/main/java/org/wlld/imageRecognition/Convolution.java index 419a4ed..83c6a24 100644 --- a/src/main/java/org/wlld/imageRecognition/Convolution.java +++ b/src/main/java/org/wlld/imageRecognition/Convolution.java @@ -6,6 +6,10 @@ import org.wlld.config.Kernel; import org.wlld.imageRecognition.border.Border; import org.wlld.imageRecognition.border.Frame; import org.wlld.imageRecognition.border.FrameBody; +import org.wlld.imageRecognition.segmentation.ColorFunction; +import org.wlld.imageRecognition.segmentation.DimensionMapping; +import org.wlld.param.Food; +import org.wlld.pso.PSO; import org.wlld.tools.ArithUtil; import org.wlld.tools.Frequency; @@ -94,113 +98,64 @@ public class Convolution extends Frequency { return features; } - public void filtering(ThreeChannelMatrix threeChannelMatrix) throws Exception {//平滑滤波 + + public List getCenterColor(ThreeChannelMatrix threeChannelMatrix, TempleConfig templeConfig, + boolean isStudy, int tag) throws Exception { Matrix matrixR = threeChannelMatrix.getMatrixR(); Matrix matrixG = threeChannelMatrix.getMatrixG(); Matrix matrixB = threeChannelMatrix.getMatrixB(); - int x = matrixR.getX(); - int y = matrixR.getY(); - double nub = x * y; - double sigmaR = 0; - double sigmaG = 0; - double sigmaB = 0; - for (int i = 0; i < x; i++) { - for (int j = 0; j < y; j++) { - sigmaR = matrixR.getNumber(i, j) + sigmaR; - sigmaG = matrixG.getNumber(i, j) + sigmaG; - sigmaB = matrixB.getNumber(i, j) + sigmaB; - } + int maxX = matrixR.getX(); + int maxY = matrixR.getY(); + ColorFunction colorFunction = new ColorFunction(threeChannelMatrix); + int[] minBorder = new int[]{0, 0}; + int[] maxBorder = new int[]{maxX, maxY}; + //创建粒子群 + PSO pso = new PSO(2, minBorder, maxBorder, 100, 100, + colorFunction, 0.2, 1, 0.5, true, 10, 1); + List positions = pso.start(0, 0); + List feature = new ArrayList<>();//像素特征 + for (int i = 0; i < positions.size(); i++) { + double[] parameter = positions.get(i); + //获取取样坐标 + int x = (int) parameter[0]; + int y = (int) parameter[1]; + double[] rgb = new double[]{matrixR.getNumber(x, y) / 255, matrixG.getNumber(x, y) / 255, + matrixB.getNumber(x, y) / 255}; + feature.add(rgb); } - double r = sigmaR / nub; - double g = sigmaG / nub; - double b = sigmaB / nub; - MatrixOperation.mathDiv(matrixR, r); - MatrixOperation.mathDiv(matrixG, g); - MatrixOperation.mathDiv(matrixB, b); - - } - - private double[] compareDis(double[] rgbTest, List rgbNorms) { - double[] feature = null; - double minDis = -1; - for (int i = 0; i < 3; i++) { - double[] rgb = rgbNorms.get(i).getRgb(); - double sigma = 0; - for (int j = 0; j < 3; j++) { - sigma = sigma + Math.pow(rgbTest[j] - rgb[j], 2); + if (isStudy) {//进行新维度映射排序 + pso = new PSO(3, null, null, 200, 50, + new DimensionMapping(feature), 0.5, 2, 2, true, 0.5, 0.001); + List mappings = pso.start(0, 0);//最小映射集合 + double[] sigmaMapping = new double[3];//最小映射 + for (int i = 0; i < mappings.size(); i++) { + double[] mapping = mappings.get(i); + for (int j = 0; j < sigmaMapping.length; j++) { + sigmaMapping[j] = sigmaMapping[j] + mapping[j]; + } } - if (sigma < minDis || minDis == -1) { - minDis = sigma; - feature = rgb; + for (int j = 0; j < sigmaMapping.length; j++) { + sigmaMapping[j] = sigmaMapping[j] / mappings.size(); } - } - return feature; - } - - private void dispersed(Matrix matrixR, Matrix matrixG, Matrix matrixB, List rgbNorms) throws Exception {//图像离散化 - ThreeChannelMatrix threeChannelMatrix = new ThreeChannelMatrix(); - int x = matrixR.getX(); - int y = matrixR.getY(); - Matrix matrixRD = new Matrix(x, y); - Matrix matrixGD = new Matrix(x, y); - Matrix matrixBD = new Matrix(x, y); - threeChannelMatrix.setMatrixR(matrixRD); - threeChannelMatrix.setMatrixG(matrixGD); - threeChannelMatrix.setMatrixB(matrixBD); - for (int i = 0; i < x; i++) { - for (int j = 0; j < y; j++) { - double r = matrixR.getNumber(i, j); - double g = matrixG.getNumber(i, j); - double b = matrixB.getNumber(i, j); - double[] rgb = new double[]{r, g, b}; - double[] rgbNow = compareDis(rgb, rgbNorms); - matrixRD.setNub(i, j, rgbNow[0]); - matrixGD.setNub(i, j, rgbNow[1]); - matrixBD.setNub(i, j, rgbNow[2]); + //保存类别及映射 + Food food = templeConfig.getFood(); + Map allMappings = food.getMappings(); + if (allMappings == null) { + allMappings = new HashMap<>(); + food.setMappings(allMappings); } - } - //输入结束进行卷积 - //System.out.println(matrixBD.getString()); - } + allMappings.put(tag, sigmaMapping); + //根据映射值进行排序 + + } else { - public List getCenterColor(ThreeChannelMatrix threeChannelMatrix, int poolSize, int sqNub, TempleConfig templeConfig) throws Exception { - Matrix matrixR = threeChannelMatrix.getMatrixR(); - Matrix matrixG = threeChannelMatrix.getMatrixG(); - Matrix matrixB = threeChannelMatrix.getMatrixB(); -// matrixR = late(matrixR, poolSize); -// matrixG = late(matrixG, poolSize); -// matrixB = late(matrixB, poolSize); - RGBSort rgbSort = new RGBSort(); - int x = matrixR.getX(); - int y = matrixR.getY(); - MeanClustering meanClustering = new MeanClustering(sqNub, templeConfig, true); - for (int i = 0; i < x; i++) { - for (int j = 0; j < y; j++) { - double[] color = new double[]{matrixR.getNumber(i, j), matrixG.getNumber(i, j), matrixB.getNumber(i, j)}; - meanClustering.setColor(color); - } - } - meanClustering.start(false); - List rgbNorms = meanClustering.getMatrices(); - Collections.sort(rgbNorms, rgbSort); - List features = new ArrayList<>(); - for (int i = 0; i < sqNub; i++) { - double[] rgb = rgbNorms.get(i).getRgb(); - // RgbRegression rgbRegression = rgbNorms.get(i).getRgbRegression(); - //double[] rgb = new double[]{rgbRegression.getWr(), rgbRegression.getWg(), rgbRegression.getB()}; - for (int j = 0; j < 3; j++) { - features.add(rgb[j]); - } } - //测试卷积 - //dispersed(matrixR, matrixG, matrixB, rgbNorms); - //System.out.println("feature==" + feature); - return features; + + return null; } public List getCenterTexture(ThreeChannelMatrix threeChannelMatrix, int size, int poolSize, TempleConfig templeConfig , int sqNub, int tag) throws Exception { - RGBSort rgbSort = new RGBSort(); MeanClustering meanClustering = new MeanClustering(sqNub, templeConfig, true); Matrix matrixR = threeChannelMatrix.getMatrixR(); Matrix matrixG = threeChannelMatrix.getMatrixG(); @@ -208,13 +163,6 @@ public class Convolution extends Frequency { Matrix matrixRGB = threeChannelMatrix.getMatrixRGB(); int xn = matrixR.getX(); int yn = matrixR.getY(); -// for (int i = 0; i < xn; i++) { -// for (int j = 0; j < yn; j++) { -// double[] rgb = new double[]{matrixR.getNumber(i, j), matrixG.getNumber(i, j) -// , matrixB.getNumber(i, j)}; -// meanClustering.setColor(rgb); -// } -// } //局部特征选区筛选 int nub = size * size; int twoNub = nub * 2; diff --git a/src/main/java/org/wlld/imageRecognition/Operation.java b/src/main/java/org/wlld/imageRecognition/Operation.java index aa9d8ce..6e7b088 100644 --- a/src/main/java/org/wlld/imageRecognition/Operation.java +++ b/src/main/java/org/wlld/imageRecognition/Operation.java @@ -63,7 +63,6 @@ public class Operation {//进行计算 Matrix matrixR = threeChannelMatrix.getMatrixR(); Matrix matrixG = threeChannelMatrix.getMatrixG(); Matrix matrixB = threeChannelMatrix.getMatrixB(); - //Matrix matrixRGB = threeChannelMatrix.getMatrixRGB(); Random random = new Random(); int x = matrixR.getX(); int y = matrixR.getY(); @@ -77,21 +76,9 @@ public class Operation {//进行计算 rgbRegression.insertRGB(rgb); } rgbRegression.regression(); -// double[] rgb = new double[]{164 / 255, 189 / 255, 193 / 255}; -// double dis = rgbRegression.getDisError(rgb); -// System.out.println("dis==" + dis); templeConfig.getFood().getTrayBody().add(rgbRegression); } - private void cutPic(ThreeChannelMatrix threeChannelMatrix, int x, int y, int xSize, int ySize) { - Matrix matrixR = threeChannelMatrix.getMatrixR(); - Matrix matrixG = threeChannelMatrix.getMatrixG(); - Matrix matrixB = threeChannelMatrix.getMatrixB(); - threeChannelMatrix.setMatrixR(matrixR.getSonOfMatrix(x, y, xSize, ySize)); - threeChannelMatrix.setMatrixG(matrixG.getSonOfMatrix(x, y, xSize, ySize)); - threeChannelMatrix.setMatrixB(matrixB.getSonOfMatrix(x, y, xSize, ySize)); - } - public RegionBody colorStudy(ThreeChannelMatrix threeChannelMatrix, int tag, List specificationsList , String url) throws Exception { Watershed watershed = new Watershed(threeChannelMatrix, specificationsList, templeConfig); diff --git a/src/main/java/org/wlld/imageRecognition/modelEntity/MappingBody.java b/src/main/java/org/wlld/imageRecognition/modelEntity/MappingBody.java new file mode 100644 index 0000000..dd61a68 --- /dev/null +++ b/src/main/java/org/wlld/imageRecognition/modelEntity/MappingBody.java @@ -0,0 +1,35 @@ +package org.wlld.imageRecognition.modelEntity; + +/** + * @param + * @DATA + * @Author LiDaPeng + * @Description 映射体 + */ +public class MappingBody { + private double[] feature;//特征 + private double mappingNub;//映射好的值 + + public MappingBody(double[] mapping, double[] feature) { + this.feature = feature; + double sigma = 0; + for (int i = 0; i < mapping.length; i++) { + sigma = sigma + Math.pow(mapping[i], 2); + } + sigma = Math.sqrt(sigma);//映射维度的莫 + double s = 0; + for (int j = 0; j < feature.length; j++) { + s = s + feature[j] * mapping[j]; + } + mappingNub = s / sigma; + + } + + public double[] getFeature() { + return feature; + } + + public double getMappingNub() { + return mappingNub; + } +} diff --git a/src/main/java/org/wlld/imageRecognition/segmentation/ColorFunction.java b/src/main/java/org/wlld/imageRecognition/segmentation/ColorFunction.java new file mode 100644 index 0000000..89f7ac4 --- /dev/null +++ b/src/main/java/org/wlld/imageRecognition/segmentation/ColorFunction.java @@ -0,0 +1,72 @@ +package org.wlld.imageRecognition.segmentation; + +import org.wlld.MatrixTools.Matrix; +import org.wlld.i.PsoFunction; +import org.wlld.imageRecognition.ThreeChannelMatrix; +import org.wlld.tools.Frequency; + +import java.util.HashMap; +import java.util.Map; + +/** + * @param + * @DATA + * @Author LiDaPeng + * @Description + */ +public class ColorFunction extends Frequency implements PsoFunction { + private Matrix matrixR; + private Matrix matrixG; + private Matrix matrixB; + private Map pixels = new HashMap<>(); + + public ColorFunction(ThreeChannelMatrix threeChannelMatrix) { + matrixR = threeChannelMatrix.getMatrixR(); + matrixG = threeChannelMatrix.getMatrixG(); + matrixB = threeChannelMatrix.getMatrixB(); + } + + @Override + public double getResult(double[] parameter, int id) throws Exception { + int x = (int) parameter[0]; + int y = (int) parameter[1]; + double[] rgb = new double[]{matrixR.getNumber(x, y) / 255, matrixG.getNumber(x, y) / 255, + matrixB.getNumber(x, y) / 255}; + pixels.put(id, rgb); + //计算当前方差 + return getDist(); + //return getRegression(); + } + + private double getRegression() throws Exception { + RgbRegression rgbRegression = new RgbRegression(pixels.size()); + for (Map.Entry entry : pixels.entrySet()) { + double[] rgb = entry.getValue(); + rgbRegression.insertRGB(rgb); + } + double sigma = 0; + if (rgbRegression.regression()) { + for (Map.Entry entry : pixels.entrySet()) { + double[] rgb = entry.getValue(); + sigma = sigma + rgbRegression.getDisError(rgb); + } + sigma = sigma / pixels.size(); + } + return sigma; + } + + private double getDist() {//计算当前均方误差 + double[] r = new double[pixels.size()]; + double[] g = new double[pixels.size()]; + double[] b = new double[pixels.size()]; + for (Map.Entry entry : pixels.entrySet()) { + double[] rgb = entry.getValue(); + int key = entry.getKey(); + r[key] = rgb[0]; + g[key] = rgb[1]; + b[key] = rgb[2]; + } + return dc(r) + dc(g) + dc(b); + } + +} diff --git a/src/main/java/org/wlld/imageRecognition/segmentation/DimensionMapping.java b/src/main/java/org/wlld/imageRecognition/segmentation/DimensionMapping.java new file mode 100644 index 0000000..68b3b15 --- /dev/null +++ b/src/main/java/org/wlld/imageRecognition/segmentation/DimensionMapping.java @@ -0,0 +1,41 @@ +package org.wlld.imageRecognition.segmentation; + +import org.wlld.i.PsoFunction; +import org.wlld.tools.Frequency; + +import java.util.List; + +/** + * @param + * @DATA + * @Author LiDaPeng + * @Description 维度映射 + */ +public class DimensionMapping extends Frequency implements PsoFunction { + private List features; + private int size; + + public DimensionMapping(List features) { + this.features = features; + size = features.size(); + } + + @Override + public double getResult(double[] parameter, int id) throws Exception { + double sigma = 0; + for (int i = 0; i < parameter.length; i++) { + sigma = sigma + Math.pow(parameter[i], 2); + } + sigma = Math.sqrt(sigma); + double[] mapping = new double[size];//在新维度的映射集合 + for (int i = 0; i < size; i++) { + double[] feature = features.get(i); + double s = 0; + for (int j = 0; j < feature.length; j++) { + s = s + feature[j] * parameter[j]; + } + mapping[i] = s / sigma; + } + return variance(mapping); + } +} diff --git a/src/main/java/org/wlld/param/Food.java b/src/main/java/org/wlld/param/Food.java index 9b8f133..025e11e 100644 --- a/src/main/java/org/wlld/param/Food.java +++ b/src/main/java/org/wlld/param/Food.java @@ -5,6 +5,7 @@ import org.wlld.imageRecognition.segmentation.RgbRegression; import java.util.ArrayList; import java.util.List; +import java.util.Map; /** * @param @@ -14,7 +15,6 @@ import java.util.List; */ public class Food { private int shrink = 60;//收缩参数 - private int times = 10;//聚类增强次数 private double rowMark = 0.12;//行痕迹过滤 private double columnMark = 0.25;//列痕迹过滤 private List trayBody = new ArrayList<>();//托盘实体参数 @@ -22,9 +22,17 @@ public class Food { private double trayTh = 0.1;//托盘回归阈值 private int regionSize = 5;//纹理区域大小 private int step = 1;//特征取样步长 - private double dispersedTh = 0.3;//选区筛选离散阈值 private int speciesNub = 24;//种类数 private KNerveManger kNerveManger; + private Map mappings;//类别映射集合 + + public Map getMappings() { + return mappings; + } + + public void setMappings(Map mappings) { + this.mappings = mappings; + } public KNerveManger getkNerveManger() { return kNerveManger; @@ -50,13 +58,6 @@ public class Food { this.step = step; } - public double getDispersedTh() { - return dispersedTh; - } - - public void setDispersedTh(double dispersedTh) { - this.dispersedTh = dispersedTh; - } public int getRegionSize() { return regionSize; @@ -113,12 +114,4 @@ public class Food { public void setShrink(int shrink) { this.shrink = shrink; } - - public int getTimes() { - return times; - } - - public void setTimes(int times) { - this.times = times; - } } diff --git a/src/main/java/org/wlld/pso/PSO.java b/src/main/java/org/wlld/pso/PSO.java index c41aa31..874f3d6 100644 --- a/src/main/java/org/wlld/pso/PSO.java +++ b/src/main/java/org/wlld/pso/PSO.java @@ -32,11 +32,13 @@ public class PSO { private Random random = new Random(); private int[] minBorder, maxBorder; private double maxSpeed; + private double initSpeed;//初始速度 public PSO(int dimensionNub, int[] minBorder, int[] maxBorder, int times, int particleNub, PsoFunction psoFunction, double inertialFactor, double selfStudyFactor, double socialStudyFactor - , boolean isMax, double maxSpeed) { + , boolean isMax, double maxSpeed, double initSpeed) { + this.initSpeed = initSpeed; this.times = times; this.psoFunction = psoFunction; this.isMax = isMax; @@ -64,17 +66,23 @@ public class PSO { this.allPar = allPar; } - public void start(int fatherX, int fatherY) throws Exception {//开始进行迭代 + public List start(int fatherX, int fatherY) throws Exception {//开始进行迭代 int size = allPar.size(); for (int i = 0; i < times; i++) { for (int j = 0; j < size; j++) { move(allPar.get(j), j); } } + List feature = new ArrayList<>(); + for (int i = 0; i < size; i++) { + feature.add(allPar.get(i).getParameter()); + } + return feature; //粒子群移动结束 - draw("/Users/lidapeng/Desktop/test/testOne/e2.jpg", fatherX, fatherY); + // draw("/Users/lidapeng/Desktop/test/testOne/e2.jpg", fatherX, fatherY); } + //绘图测试 private void draw(String path, int fatherX, int fatherY) throws Exception { File file = new File(path); FileInputStream fileInputStream = new FileInputStream(file); @@ -94,7 +102,7 @@ public class PSO { Rectangle2D rect = new Rectangle2D.Double(y, x, 1, 1);//声明并创建矩形对象,矩形的左上角是(20,30),宽是300,高是40 g2.draw(rect); } - String savePath = "/Users/lidapeng/Desktop/test/testTwo/a.jpg"; + String savePath = "/Users/lidapeng/Desktop/test/testTwo/d.jpg"; ImageIO.write(bi, "JPEG", new FileOutputStream(savePath)); } @@ -153,11 +161,13 @@ public class PSO { bestData[i].speed = speed; //更新该粒子该维度新的位置 double position = selfPosition + speed; - if (position < minBorder[i]) { - position = minBorder[i]; - } - if (position > maxBorder[i]) { - position = maxBorder[i]; + if (minBorder != null) { + if (position < minBorder[i]) { + position = minBorder[i]; + } + if (position > maxBorder[i]) { + position = maxBorder[i]; + } } bestData[i].selfPosition = position; } @@ -178,23 +188,29 @@ public class PSO { protected Particle(int dimensionNub) {//初始化随机位置 bestDataArray = new BestData[dimensionNub]; for (int i = 0; i < dimensionNub; i++) { - int min = minBorder[i]; - int max = maxBorder[i]; - int region = max - min + 1; - int position = random.nextInt(region) + min;//初始化该维度的位置 - bestDataArray[i] = new BestData(position); + double position; + if (minBorder != null && maxBorder != null) { + int min = minBorder[i]; + int max = maxBorder[i]; + int region = max - min + 1; + position = random.nextInt(region) + min;//初始化该维度的位置 + } else { + position = random.nextDouble(); + } + bestDataArray[i] = new BestData(position, initSpeed); } } } class BestData {//数据保存 - private BestData(double selfPosition) { + private BestData(double selfPosition, double initSpeed) { this.selfBestPosition = selfPosition; this.selfPosition = selfPosition; + speed = initSpeed; } - private double speed = 1;//该粒子当前维度的速度 + private double speed;//该粒子当前维度的速度 private double selfBestPosition;//当前维度自身最优的历史位置/自己最优位置的值 private double selfPosition;//当前维度自己现在的位置/也就是当前维度自己的值 } diff --git a/src/test/java/coverTest/DataObservation.java b/src/test/java/coverTest/DataObservation.java index 87ecbd5..d508a8b 100644 --- a/src/test/java/coverTest/DataObservation.java +++ b/src/test/java/coverTest/DataObservation.java @@ -24,9 +24,9 @@ public class DataObservation { public static void main(String[] args) throws Exception { //372,330,右 最大值 147.44 //377 ,330右 最大值 69.6 - int xp = 100; - int yp = 720;//290 - observation2("/Users/lidapeng/Desktop/test/testOne/e2.jpg", xp, yp); + int xp = 100;//100 2 + int yp = 720;//720 2 + observation2("/Users/lidapeng/Desktop/test/testOne/e2.jpg", xp, yp);//2 } public static void observation2(String url, int xp, int yp) throws Exception { diff --git a/src/test/java/coverTest/FoodTest.java b/src/test/java/coverTest/FoodTest.java index 0698d85..8474709 100644 --- a/src/test/java/coverTest/FoodTest.java +++ b/src/test/java/coverTest/FoodTest.java @@ -75,7 +75,6 @@ public class FoodTest { templeConfig.setFeatureNub(5);//聚类特征数量 //菜品识别实体类 food.setShrink(5);//缩紧像素 - food.setTimes(1);//聚类数据增强 food.setRegionSize(5); KNerveManger kNerveManger = new KNerveManger(12, 24, 6000); food.setkNerveManger(kNerveManger); @@ -83,7 +82,6 @@ public class FoodTest { food.setColumnMark(0.15);//0.25 food.setRegressionNub(20000); food.setTrayTh(0.08); - food.setDispersedTh(0.5); templeConfig.setClassifier(Classifier.KNN); templeConfig.init(StudyPattern.Cover_Pattern, true, 400, 400, 3); if (modelParameter != null) { diff --git a/src/test/java/coverTest/regionCut/ColorFunction.java b/src/test/java/coverTest/regionCut/ColorFunction.java index 5c7a089..f34e91c 100644 --- a/src/test/java/coverTest/regionCut/ColorFunction.java +++ b/src/test/java/coverTest/regionCut/ColorFunction.java @@ -3,6 +3,7 @@ package coverTest.regionCut; import org.wlld.MatrixTools.Matrix; import org.wlld.i.PsoFunction; import org.wlld.imageRecognition.ThreeChannelMatrix; +import org.wlld.imageRecognition.segmentation.RgbRegression; import org.wlld.tools.Frequency; import java.util.ArrayList; @@ -37,6 +38,24 @@ public class ColorFunction extends Frequency implements PsoFunction { pixels.put(id, rgb); //计算当前方差 return getDist(); + //return getRegression(); + } + + private double getRegression() throws Exception { + RgbRegression rgbRegression = new RgbRegression(pixels.size()); + for (Map.Entry entry : pixels.entrySet()) { + double[] rgb = entry.getValue(); + rgbRegression.insertRGB(rgb); + } + double sigma = 0; + if (rgbRegression.regression()) { + for (Map.Entry entry : pixels.entrySet()) { + double[] rgb = entry.getValue(); + sigma = sigma + rgbRegression.getDisError(rgb); + } + sigma = sigma / pixels.size(); + } + return sigma; } private double getDist() {//计算当前均方误差 diff --git a/src/test/java/coverTest/regionCut/RegionCut.java b/src/test/java/coverTest/regionCut/RegionCut.java index 6408f64..13c74e3 100644 --- a/src/test/java/coverTest/regionCut/RegionCut.java +++ b/src/test/java/coverTest/regionCut/RegionCut.java @@ -33,9 +33,12 @@ public class RegionCut { ColorFunction colorFunction = new ColorFunction(threeChannelMatrix); int[] minBorder = new int[]{0, 0}; int[] maxBorder = new int[]{299, 299}; - PSO pso = new PSO(2, minBorder, maxBorder, 400, 200, - colorFunction, 0.1, 0.1, 0.1, true, 10); + long a = System.currentTimeMillis(); + PSO pso = new PSO(2, minBorder, maxBorder, 100, 100, + colorFunction, 0.2, 1, 0.5, true, 10, 1); pso.start(fatherX, fatherY); + long b = System.currentTimeMillis() - a; + System.out.println("时间:" + b); // Matrix matrixR = threeChannelMatrix.getMatrixR(); // Matrix matrixG = threeChannelMatrix.getMatrixG(); // Matrix matrixB = threeChannelMatrix.getMatrixB();