!32 修改KNN 分类器

Merge pull request !32 from 逐光/test
pull/32/MERGE
逐光 5 years ago committed by Gitee
commit 61849bf0f8

@ -165,6 +165,22 @@ public class Operation {//进行计算
return regionList;
}
private int getIdByCos(Matrix myVector) throws Exception {//VAG获取分类
Map<Integer, Matrix> matrixK = templeConfig.getVectorK().getMatrixK();
double maxCos = 0;
int id = 0;
for (Map.Entry<Integer, Matrix> 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<Integer, Matrix> matrixK = templeConfig.getVectorK().getMatrixK();
double minDist = 0;
@ -172,7 +188,7 @@ public class Operation {//进行计算
for (Map.Entry<Integer, Matrix> 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();

@ -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() {

@ -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;
}
}

@ -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;
}
@ -298,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) {//获取最小值

@ -17,54 +17,76 @@ import java.util.List;
public class FoodTest {
public static void main(String[] args) throws Exception {
//test2();
//test2(null);
test();
}
public static void test2(TempleConfig templeConfig) throws Exception {
//test();
System.out.println("开始测试");
if (templeConfig == null) {
templeConfig = getTemple();
}
Picture picture = new Picture();
List<Specifications> specificationsList = new ArrayList<>();
Specifications specifications = new Specifications();
specifications.setWidth(400);
specifications.setHeight(400);
specifications.setMinWidth(250);
specifications.setMinHeight(250);
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 <= 28; i++) {
ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix("D:\\share\\cai\\g/g" + i + ".jpg");
List<RegionBody> regionBody = operation.colorLook(threeChannelMatrix1, specificationsList);
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);
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.setFeatureNub(3);
templeConfig.setFeatureNub(4);
templeConfig.sethTh(0.88);
templeConfig.setPoolSize(2);
//templeConfig.setRegionNub(100);
templeConfig.setClassifier(Classifier.VAvg);
templeConfig.setRegionNub(200);
templeConfig.setClassifier(Classifier.KNN);
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<Specifications> specificationsList = new ArrayList<>();
Specifications specifications = new Specifications();
specifications.setWidth(400);
specifications.setHeight(400);
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("D:\\pic/a" + i + ".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);
}
}

Loading…
Cancel
Save