From 35ab9c027cd2b225b6285a928ba9d0a1efd443c9 Mon Sep 17 00:00:00 2001 From: thenk008 <794757862@qq.com> Date: Fri, 22 May 2020 14:57:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9C=80=E5=A4=A7=E8=BE=B9?= =?UTF-8?q?=E6=A1=86=E9=99=90=E5=88=B6=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/wlld/imageRecognition/Operation.java | 16 +++++++ .../segmentation/Specifications.java | 38 +++++++++++----- .../segmentation/Watershed.java | 3 +- src/test/java/coverTest/FoodTest.java | 43 ++++++++++++------- 4 files changed, 73 insertions(+), 27 deletions(-) diff --git a/src/main/java/org/wlld/imageRecognition/Operation.java b/src/main/java/org/wlld/imageRecognition/Operation.java index 37e22ca..bd8db96 100644 --- a/src/main/java/org/wlld/imageRecognition/Operation.java +++ b/src/main/java/org/wlld/imageRecognition/Operation.java @@ -165,6 +165,22 @@ public class Operation {//进行计算 return regionList; } + private int getIdByCos(Matrix myVector) throws Exception {//VAG获取分类 + Map matrixK = templeConfig.getVectorK().getMatrixK(); + double maxCos = 0; + int id = 0; + for (Map.Entry entry : matrixK.entrySet()) { + Matrix matrix = entry.getValue(); + double cos = MatrixOperation.getNormCos(matrix, myVector); + //System.out.println("距离===" + dist + ",类别==" + entry.getKey()+",核心:"+matrix.getString()); + if (cos > maxCos) { + maxCos = cos; + id = entry.getKey(); + } + } + return id; + } + private int getIdByVag(Matrix myVector) throws Exception {//VAG获取分类 Map matrixK = templeConfig.getVectorK().getMatrixK(); double minDist = 0; diff --git a/src/main/java/org/wlld/imageRecognition/segmentation/Specifications.java b/src/main/java/org/wlld/imageRecognition/segmentation/Specifications.java index 95ec274..767d649 100644 --- a/src/main/java/org/wlld/imageRecognition/segmentation/Specifications.java +++ b/src/main/java/org/wlld/imageRecognition/segmentation/Specifications.java @@ -1,22 +1,40 @@ package org.wlld.imageRecognition.segmentation; public class Specifications { - private double width; - private double height; + private double minWidth; + private double minHeight; + private double maxWidth; + private double maxHeight; - public double getWidth() { - return width; + public double getMinWidth() { + return minWidth; } - public void setWidth(double width) { - this.width = width; + public void setMinWidth(double minWidth) { + this.minWidth = minWidth; } - public double getHeight() { - return height; + public double getMinHeight() { + return minHeight; } - public void setHeight(double height) { - this.height = height; + public void setMinHeight(double minHeight) { + this.minHeight = minHeight; + } + + public double getMaxWidth() { + return maxWidth; + } + + public void setMaxWidth(double maxWidth) { + this.maxWidth = maxWidth; + } + + public double getMaxHeight() { + return maxHeight; + } + + public void setMaxHeight(double maxHeight) { + this.maxHeight = maxHeight; } } diff --git a/src/main/java/org/wlld/imageRecognition/segmentation/Watershed.java b/src/main/java/org/wlld/imageRecognition/segmentation/Watershed.java index e473024..c6b5484 100644 --- a/src/main/java/org/wlld/imageRecognition/segmentation/Watershed.java +++ b/src/main/java/org/wlld/imageRecognition/segmentation/Watershed.java @@ -210,7 +210,8 @@ public class Watershed { for (Specifications specification : specifications) { int width = maxY - minY; int height = maxX - minX; - if (width >= specification.getWidth() && height >= specification.getHeight()) { + if (width >= specification.getMinWidth() && height >= specification.getMinHeight() + && width <= specification.getMaxWidth() && height <= specification.getMaxHeight()) { isRight = true; break; } diff --git a/src/test/java/coverTest/FoodTest.java b/src/test/java/coverTest/FoodTest.java index 4f50b47..03c9ac8 100644 --- a/src/test/java/coverTest/FoodTest.java +++ b/src/test/java/coverTest/FoodTest.java @@ -18,48 +18,59 @@ public class FoodTest { public static void main(String[] args) throws Exception { //test2(); - test(); + test2(); } - public static void test2(TempleConfig templeConfig) throws Exception { + public static void test2() throws Exception { //test(); + TempleConfig templeConfig = getTemple(); Picture picture = new Picture(); List specificationsList = new ArrayList<>(); Specifications specifications = new Specifications(); - specifications.setWidth(400); - specifications.setHeight(400); + specifications.setMinWidth(300); + specifications.setMinHeight(300); + specifications.setMaxWidth(700); + specifications.setMaxHeight(700); specificationsList.add(specifications); Operation operation = new Operation(templeConfig); - for (int i = 6; i <= 7; i++) { - ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix("D:\\pic\\f/f" + i + ".jpg"); + for (int i = 1; i <= 1; 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()); - } +// for (int j = 0; j < regionBody.size(); j++) { +// System.out.println(regionBody.get(j).getType()); +// } System.out.println("=============================" + i); } } - public static void test() throws Exception { + public static TempleConfig getTemple() throws Exception { TempleConfig templeConfig = new TempleConfig(); Picture picture = new Picture(); templeConfig.isShowLog(true);//是否打印日志 - templeConfig.setMaxRain(320);//切割阈值 + templeConfig.setMaxRain(340);//切割阈值 templeConfig.setFeatureNub(3); templeConfig.sethTh(0.88); templeConfig.setPoolSize(2); - //templeConfig.setRegionNub(100); + templeConfig.setRegionNub(200); templeConfig.setClassifier(Classifier.VAvg); templeConfig.init(StudyPattern.Cover_Pattern, true, 400, 400, 3); + return templeConfig; + } + + public static void test() throws Exception { + Picture picture = new Picture(); + TempleConfig templeConfig = getTemple(); Operation operation = new Operation(templeConfig); List specificationsList = new ArrayList<>(); Specifications specifications = new Specifications(); - specifications.setWidth(400); - specifications.setHeight(400); + specifications.setMinWidth(300); + specifications.setMinHeight(300); + specifications.setMaxWidth(700); + specifications.setMaxHeight(700); specificationsList.add(specifications); for (int j = 0; j < 1; j++) { for (int i = 1; i <= 10; i++) { - ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix("D:\\pic/a" + i + ".jpg"); + 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"); operation.colorStudy(threeChannelMatrix1, 1, specificationsList); @@ -69,7 +80,7 @@ public class FoodTest { } } templeConfig.finishStudy(); - test2(templeConfig); + test2(); } public static void study() throws Exception {