From 2dac3dadf9d07bfbe38c25fe0ff6d005e6aaace0 Mon Sep 17 00:00:00 2001 From: thenk008 <794757862@qq.com> Date: Fri, 22 May 2020 17:23:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9KNN=E5=88=86=E7=B1=BB?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/wlld/imageRecognition/Operation.java | 2 +- .../wlld/imageRecognition/TempleConfig.java | 2 +- .../segmentation/Watershed.java | 2 +- src/test/java/coverTest/FoodTest.java | 55 +++++++++++-------- 4 files changed, 36 insertions(+), 25 deletions(-) diff --git a/src/main/java/org/wlld/imageRecognition/Operation.java b/src/main/java/org/wlld/imageRecognition/Operation.java index bd8db96..09dcb82 100644 --- a/src/main/java/org/wlld/imageRecognition/Operation.java +++ b/src/main/java/org/wlld/imageRecognition/Operation.java @@ -188,7 +188,7 @@ public class Operation {//进行计算 for (Map.Entry entry : matrixK.entrySet()) { Matrix matrix = entry.getValue(); double dist = MatrixOperation.getEDist(matrix, myVector); - //System.out.println("距离===" + dist + ",类别==" + entry.getKey()+",核心:"+matrix.getString()); + System.out.println("距离===" + dist + ",类别==" + entry.getKey()+",核心:"+matrix.getString()); if (minDist == 0 || dist < minDist) { minDist = dist; id = entry.getKey(); diff --git a/src/main/java/org/wlld/imageRecognition/TempleConfig.java b/src/main/java/org/wlld/imageRecognition/TempleConfig.java index 59fabc6..d238d85 100644 --- a/src/main/java/org/wlld/imageRecognition/TempleConfig.java +++ b/src/main/java/org/wlld/imageRecognition/TempleConfig.java @@ -65,7 +65,7 @@ public class TempleConfig { private double maxRain = 340;//不降雨RGB阈值 private int featureNub = 4;//聚类特征数量 private Knn knn;//KNN分类器 - private int knnNub = 3;//KNN投票人数 + private int knnNub = 7;//KNN投票人数 private Matrix backGround;//背景 public Matrix getBackGround() { diff --git a/src/main/java/org/wlld/imageRecognition/segmentation/Watershed.java b/src/main/java/org/wlld/imageRecognition/segmentation/Watershed.java index c6b5484..533a04f 100644 --- a/src/main/java/org/wlld/imageRecognition/segmentation/Watershed.java +++ b/src/main/java/org/wlld/imageRecognition/segmentation/Watershed.java @@ -299,7 +299,7 @@ public class Watershed { } createMerge(); merge(); - //System.out.println(regionMap.getString()); + // System.out.println(regionMap.getString()); } private int getMinIndex(double[] array, double mySelf) {//获取最小值 diff --git a/src/test/java/coverTest/FoodTest.java b/src/test/java/coverTest/FoodTest.java index 03c9ac8..fb9d878 100644 --- a/src/test/java/coverTest/FoodTest.java +++ b/src/test/java/coverTest/FoodTest.java @@ -17,42 +17,47 @@ import java.util.List; public class FoodTest { public static void main(String[] args) throws Exception { - //test2(); - test2(); + //test2(null); + test(); } - public static void test2() throws Exception { + public static void test2(TempleConfig templeConfig) throws Exception { //test(); - TempleConfig templeConfig = getTemple(); + System.out.println("开始测试"); + if (templeConfig == null) { + templeConfig = getTemple(); + } Picture picture = new Picture(); List specificationsList = new ArrayList<>(); Specifications specifications = new Specifications(); - specifications.setMinWidth(300); - specifications.setMinHeight(300); + specifications.setMinWidth(250); + specifications.setMinHeight(250); specifications.setMaxWidth(700); specifications.setMaxHeight(700); specificationsList.add(specifications); Operation operation = new Operation(templeConfig); - for (int i = 1; i <= 1; i++) { + 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++) { -// System.out.println(regionBody.get(j).getType()); -// } - System.out.println("=============================" + i); + for (int j = 0; j < regionBody.size(); j++) { + RegionBody regionBody1 = regionBody.get(j); + System.out.println("minX==" + regionBody1.getMinX() + ",minY==" + regionBody1.getMinY() + + ",maxX==" + regionBody1.getMaxX() + ",maxY==" + regionBody1.getMaxY()); + System.out.println(regionBody.get(j).getType()); + } + System.out.println("===================================" + i); } } public static TempleConfig getTemple() throws Exception { TempleConfig templeConfig = new TempleConfig(); - Picture picture = new Picture(); templeConfig.isShowLog(true);//是否打印日志 - templeConfig.setMaxRain(340);//切割阈值 - templeConfig.setFeatureNub(3); + templeConfig.setMaxRain(320);//切割阈值 + templeConfig.setFeatureNub(4); templeConfig.sethTh(0.88); templeConfig.setPoolSize(2); templeConfig.setRegionNub(200); - templeConfig.setClassifier(Classifier.VAvg); + templeConfig.setClassifier(Classifier.KNN); templeConfig.init(StudyPattern.Cover_Pattern, true, 400, 400, 3); return templeConfig; } @@ -63,24 +68,30 @@ public class FoodTest { Operation operation = new Operation(templeConfig); List specificationsList = new ArrayList<>(); Specifications specifications = new Specifications(); - specifications.setMinWidth(300); - specifications.setMinHeight(300); + specifications.setMinWidth(250); + specifications.setMinHeight(250); specifications.setMaxWidth(700); specifications.setMaxHeight(700); specificationsList.add(specifications); - for (int j = 0; j < 1; j++) { + for (int j = 0; j < 10; j++) { for (int i = 1; i <= 10; i++) { - ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix("E:\\food\\train/a.jpg"); - ThreeChannelMatrix threeChannelMatrix2 = picture.getThreeMatrix("D:\\pic/b" + i + ".jpg"); - ThreeChannelMatrix threeChannelMatrix3 = picture.getThreeMatrix("D:\\pic/c" + i + ".jpg"); + ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix("D:\\share\\cai\\a/a" + i + ".jpg"); + ThreeChannelMatrix threeChannelMatrix2 = picture.getThreeMatrix("D:\\share\\cai\\b/b" + i + ".jpg"); + ThreeChannelMatrix threeChannelMatrix3 = picture.getThreeMatrix("D:\\share\\cai\\c/c" + i + ".jpg"); + ThreeChannelMatrix threeChannelMatrix4 = picture.getThreeMatrix("D:\\share\\cai\\d/d" + i + ".jpg"); + ThreeChannelMatrix threeChannelMatrix5 = picture.getThreeMatrix("D:\\share\\cai\\e/e" + i + ".jpg"); + ThreeChannelMatrix threeChannelMatrix6 = picture.getThreeMatrix("D:\\share\\cai\\f/f" + i + ".jpg"); operation.colorStudy(threeChannelMatrix1, 1, specificationsList); operation.colorStudy(threeChannelMatrix2, 2, specificationsList); operation.colorStudy(threeChannelMatrix3, 3, specificationsList); + operation.colorStudy(threeChannelMatrix4, 4, specificationsList); + operation.colorStudy(threeChannelMatrix5, 5, specificationsList); + operation.colorStudy(threeChannelMatrix6, 6, specificationsList); System.out.println("=======================================" + i); } } templeConfig.finishStudy(); - test2(); + test2(templeConfig); } public static void study() throws Exception {