修改KNN分类器

pull/32/head
thenk008 5 years ago
parent 35ab9c027c
commit 2dac3dadf9

@ -188,7 +188,7 @@ public class Operation {//进行计算
for (Map.Entry<Integer, Matrix> entry : matrixK.entrySet()) { for (Map.Entry<Integer, Matrix> entry : matrixK.entrySet()) {
Matrix matrix = entry.getValue(); Matrix matrix = entry.getValue();
double dist = MatrixOperation.getEDist(matrix, myVector); 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) { if (minDist == 0 || dist < minDist) {
minDist = dist; minDist = dist;
id = entry.getKey(); id = entry.getKey();

@ -65,7 +65,7 @@ public class TempleConfig {
private double maxRain = 340;//不降雨RGB阈值 private double maxRain = 340;//不降雨RGB阈值
private int featureNub = 4;//聚类特征数量 private int featureNub = 4;//聚类特征数量
private Knn knn;//KNN分类器 private Knn knn;//KNN分类器
private int knnNub = 3;//KNN投票人数 private int knnNub = 7;//KNN投票人数
private Matrix backGround;//背景 private Matrix backGround;//背景
public Matrix getBackGround() { public Matrix getBackGround() {

@ -299,7 +299,7 @@ public class Watershed {
} }
createMerge(); createMerge();
merge(); merge();
//System.out.println(regionMap.getString()); // System.out.println(regionMap.getString());
} }
private int getMinIndex(double[] array, double mySelf) {//获取最小值 private int getMinIndex(double[] array, double mySelf) {//获取最小值

@ -17,42 +17,47 @@ import java.util.List;
public class FoodTest { public class FoodTest {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
//test2(); //test2(null);
test2(); test();
} }
public static void test2() throws Exception { public static void test2(TempleConfig templeConfig) throws Exception {
//test(); //test();
TempleConfig templeConfig = getTemple(); System.out.println("开始测试");
if (templeConfig == null) {
templeConfig = getTemple();
}
Picture picture = new Picture(); Picture picture = new Picture();
List<Specifications> specificationsList = new ArrayList<>(); List<Specifications> specificationsList = new ArrayList<>();
Specifications specifications = new Specifications(); Specifications specifications = new Specifications();
specifications.setMinWidth(300); specifications.setMinWidth(250);
specifications.setMinHeight(300); specifications.setMinHeight(250);
specifications.setMaxWidth(700); specifications.setMaxWidth(700);
specifications.setMaxHeight(700); specifications.setMaxHeight(700);
specificationsList.add(specifications); specificationsList.add(specifications);
Operation operation = new Operation(templeConfig); 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"); ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix("D:\\share\\cai\\g/g" + i + ".jpg");
List<RegionBody> regionBody = operation.colorLook(threeChannelMatrix1, specificationsList); List<RegionBody> regionBody = operation.colorLook(threeChannelMatrix1, specificationsList);
// for (int j = 0; j < regionBody.size(); j++) { for (int j = 0; j < regionBody.size(); j++) {
// System.out.println(regionBody.get(j).getType()); RegionBody regionBody1 = regionBody.get(j);
// } System.out.println("minX==" + regionBody1.getMinX() + ",minY==" + regionBody1.getMinY()
System.out.println("=============================" + i); + ",maxX==" + regionBody1.getMaxX() + ",maxY==" + regionBody1.getMaxY());
System.out.println(regionBody.get(j).getType());
}
System.out.println("===================================" + i);
} }
} }
public static TempleConfig getTemple() throws Exception { public static TempleConfig getTemple() throws Exception {
TempleConfig templeConfig = new TempleConfig(); TempleConfig templeConfig = new TempleConfig();
Picture picture = new Picture();
templeConfig.isShowLog(true);//是否打印日志 templeConfig.isShowLog(true);//是否打印日志
templeConfig.setMaxRain(340);//切割阈值 templeConfig.setMaxRain(320);//切割阈值
templeConfig.setFeatureNub(3); templeConfig.setFeatureNub(4);
templeConfig.sethTh(0.88); templeConfig.sethTh(0.88);
templeConfig.setPoolSize(2); templeConfig.setPoolSize(2);
templeConfig.setRegionNub(200); templeConfig.setRegionNub(200);
templeConfig.setClassifier(Classifier.VAvg); templeConfig.setClassifier(Classifier.KNN);
templeConfig.init(StudyPattern.Cover_Pattern, true, 400, 400, 3); templeConfig.init(StudyPattern.Cover_Pattern, true, 400, 400, 3);
return templeConfig; return templeConfig;
} }
@ -63,24 +68,30 @@ public class FoodTest {
Operation operation = new Operation(templeConfig); Operation operation = new Operation(templeConfig);
List<Specifications> specificationsList = new ArrayList<>(); List<Specifications> specificationsList = new ArrayList<>();
Specifications specifications = new Specifications(); Specifications specifications = new Specifications();
specifications.setMinWidth(300); specifications.setMinWidth(250);
specifications.setMinHeight(300); specifications.setMinHeight(250);
specifications.setMaxWidth(700); specifications.setMaxWidth(700);
specifications.setMaxHeight(700); specifications.setMaxHeight(700);
specificationsList.add(specifications); specificationsList.add(specifications);
for (int j = 0; j < 1; j++) { for (int j = 0; j < 10; j++) {
for (int i = 1; i <= 10; i++) { for (int i = 1; i <= 10; i++) {
ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix("E:\\food\\train/a.jpg"); ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix("D:\\share\\cai\\a/a" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix2 = picture.getThreeMatrix("D:\\pic/b" + i + ".jpg"); ThreeChannelMatrix threeChannelMatrix2 = picture.getThreeMatrix("D:\\share\\cai\\b/b" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix3 = picture.getThreeMatrix("D:\\pic/c" + 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(threeChannelMatrix1, 1, specificationsList);
operation.colorStudy(threeChannelMatrix2, 2, specificationsList); operation.colorStudy(threeChannelMatrix2, 2, specificationsList);
operation.colorStudy(threeChannelMatrix3, 3, 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); System.out.println("=======================================" + i);
} }
} }
templeConfig.finishStudy(); templeConfig.finishStudy();
test2(); test2(templeConfig);
} }
public static void study() throws Exception { public static void study() throws Exception {

Loading…
Cancel
Save