!42 清除冗余

Merge pull request !42 from 逐光/test
pull/42/MERGE
逐光 5 years ago committed by Gitee
commit ceed4e3373

@ -132,6 +132,49 @@ public class Convolution extends Frequency {
}
private double[] compareDis(double[] rgbTest, List<RGBNorm> 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 (sigma < minDis || minDis == -1) {
minDis = sigma;
feature = rgb;
}
}
return feature;
}
private void dispersed(Matrix matrixR, Matrix matrixG, Matrix matrixB, List<RGBNorm> 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]);
}
}
//输入结束进行卷积
//System.out.println(matrixBD.getString());
}
public List<Double> getCenterColor(ThreeChannelMatrix threeChannelMatrix, int poolSize, int sqNub) throws Exception {
Matrix matrixR = threeChannelMatrix.getMatrixR();
Matrix matrixG = threeChannelMatrix.getMatrixG();
@ -159,6 +202,8 @@ public class Convolution extends Frequency {
features.add(rgb[j]);
}
}
//测试卷积
//dispersed(matrixR, matrixG, matrixB, rgbNorms);
//System.out.println("feature==" + feature);
return features;
}
@ -252,6 +297,33 @@ public class Convolution extends Frequency {
return frameBodies;
}
private void convolutionByThree(ThreeChannelMatrix threeChannelMatrix, Matrix kernel) throws Exception {
Matrix matrixR = threeChannelMatrix.getMatrixR();
Matrix matrixG = threeChannelMatrix.getMatrixG();
Matrix matrixB = threeChannelMatrix.getMatrixB();
int x = matrixR.getX() - 2;//求导后矩阵的行数
int y = matrixR.getY() - 2;//求导后矩阵的列数
Matrix myMatrixR = new Matrix(x, y);
Matrix myMatrixG = new Matrix(x, y);
Matrix myMatrixB = new Matrix(x, y);
for (int i = 0; i < x; i++) {//遍历行
for (int j = 0; j < y; j++) {//遍历每行的列
double rm = MatrixOperation.convolution(matrixR, kernel, i, j, false);
double gm = MatrixOperation.convolution(matrixG, kernel, i, j, false);
double bm = MatrixOperation.convolution(matrixB, kernel, i, j, false);
myMatrixR.setNub(i, j, rm);
myMatrixG.setNub(i, j, gm);
myMatrixB.setNub(i, j, bm);
}
}
myMatrixR = late(myMatrixR, 2);
myMatrixG = late(myMatrixG, 2);
myMatrixB = late(myMatrixB, 2);
threeChannelMatrix.setMatrixR(myMatrixR);
threeChannelMatrix.setMatrixG(myMatrixG);
threeChannelMatrix.setMatrixB(myMatrixB);
}
private Matrix convolution(Matrix matrix, Matrix kernel, boolean isFirst
, Border border, boolean isOnce) throws Exception {
int x = matrix.getX() - 2;//求导后矩阵的行数

@ -90,15 +90,6 @@ public class MeanClustering {
break;
}
}
// boolean isNext;
// do {
// averageMatrix();
// isNext = isNext();
// if (isNext) {
// clear();
// }
// }
// while (isNext);
} else {
throw new Exception("matrixList number less than 2");

@ -105,7 +105,7 @@ public class Operation {//进行计算
}
}
} else {
throw new Exception("Parameter exception");
throw new Exception("Parameter exception region size==" + regionBodies.size());
}
}

@ -2,8 +2,6 @@ package org.wlld.imageRecognition;
import org.wlld.tools.ArithUtil;
import java.util.Arrays;
public class RGBNorm {
private double[] rgbAll = new double[3];
private double norm;

@ -68,105 +68,6 @@ public class Knn {//KNN分类器
}
}
public int getType2(Matrix vector) throws Exception {
Map<Integer, Matrix> map = new HashMap<>();
double[] sub = new double[vector.getY()];//记录最小差
for (int t = 0; t < sub.length; t++) {
sub[t] = -1;
}
int[] types = new int[vector.getY()];//记录每个位置的最小类别
for (Map.Entry<Integer, List<Matrix>> entry : featureMap.entrySet()) {
int type = entry.getKey();
List<Matrix> matrices = entry.getValue();
double minDist = -1;
Matrix minMatrix = null;
for (Matrix matrix : matrices) {
double dist = MatrixOperation.getEDist(matrix, vector);
if (minDist > dist || minDist == -1) {
minDist = dist;
minMatrix = matrix;
}
}
map.put(type, minMatrix);
}
//从所有最小的里面选取三名最小的
int[] ty = getTypes(vector);
System.out.println(Arrays.toString(ty));
//从所有最小的里面选取
for (Map.Entry<Integer, Matrix> entry : map.entrySet()) {
int type = entry.getKey();
if (isLive(ty, type)) {
Matrix matrix = entry.getValue();
setMin(matrix, types, sub, vector, type);
}
}
System.out.println("最小位置:" + Arrays.toString(types));
Map<Integer, Integer> typeMap = new HashMap<>();
for (int i = 0; i < types.length; i++) {
int type = types[i];
if (typeMap.containsKey(type)) {
typeMap.put(type, typeMap.get(type) + 1);
} else {
typeMap.put(type, 1);
}
}
int type = 0;
int max = 0;
for (Map.Entry<Integer, Integer> entry : typeMap.entrySet()) {
int key = entry.getKey();
int nub = entry.getValue();
if (nub > max) {
max = nub;
type = key;
}
}
return type;
}
private boolean isLive(int[] types, int type) {
boolean isLive = false;
for (int ty : types) {
if (ty == type) {
isLive = true;
break;
}
}
return isLive;
}
private void setMin(Matrix matrix, int[] types, double[] sub, Matrix vector, int type) throws Exception {
int y = vector.getY();
for (int j = 0; j < y; j++) {
double nub = vector.getNumber(0, j);
double myNub = matrix.getNumber(0, j);
double subs = sub[j];
double cha = Math.abs(nub - myNub);
if (subs > cha || subs == -1) {
sub[j] = cha;
types[j] = type;
}
}
}
public int[] getTypes(Matrix vector) throws Exception {
double[] dists = new double[nub];
// System.out.println("测试:" + vector.getString());
int[] types = new int[nub];
for (int i = 0; i < nub; i++) {
dists[i] = -1;
}
for (Map.Entry<Integer, List<Matrix>> entry : featureMap.entrySet()) {
int type = entry.getKey();
List<Matrix> matrices = entry.getValue();
for (Matrix matrix : matrices) {
//double dist = MatrixOperation.errorNub(vector, matrix, 0);
double dist = MatrixOperation.getEDist(matrix, vector);
compare(dists, types, dist, type);
}
}
return types;
}
public int getType(Matrix vector) throws Exception {//识别分类
int ty = 0;
double[] dists = new double[nub];

@ -59,7 +59,7 @@ public abstract class Frequency {//统计频数
return var;
}
public double varianceByAve(double[] m, double ave) {//计算方差 计算方差,依赖平均值
public double varianceByAve(double[] m, double ave) {// 计算方差,依赖平均值
double allNub = 0;
for (int i = 0; i < m.length; i++) {
allNub = allNub + Math.pow(m[i] - ave, 2);

@ -51,8 +51,8 @@ public class FoodTest {
specifications.setMaxHeight(750);
specificationsList.add(specifications);
Operation operation = new Operation(templeConfig);
for (int i = 1; i <= 28; i++) {
ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix("D:\\share\\cai\\g/g" + i + ".jpg");
for (int i = 1; i <= 1; i++) {
ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix("D:\\share\\cai\\g/g2.jpg");
List<RegionBody> regionBody = operation.colorLook(threeChannelMatrix1, specificationsList);
for (int j = 0; j < regionBody.size(); j++) {
RegionBody regionBody1 = regionBody.get(j);
@ -80,7 +80,7 @@ public class FoodTest {
//聚类
templeConfig.setFeatureNub(3);//聚类特征数量
//菜品识别实体类
food.setShrink(60);//缩紧像素
food.setShrink(50);//缩紧像素
food.setTimes(2);//聚类数据增强
templeConfig.setClassifier(Classifier.KNN);
templeConfig.init(StudyPattern.Cover_Pattern, true, 400, 400, 3);
@ -124,12 +124,7 @@ public class FoodTest {
operation.colorStudy(threeChannelMatrix10, 10, specificationsList);
System.out.println("=======================================" + i);
}
ModelParameter modelParameter = templeConfig.getModel();
String model = JSON.toJSONString(modelParameter);
System.out.println(model);
ModelParameter modelParameter1 = JSONObject.parseObject(model, ModelParameter.class);
//templeConfig.finishStudy();
test2(getTemple(modelParameter1));
test2(templeConfig);
}
public static void study() throws Exception {

Loading…
Cancel
Save