增加粒子群

pull/59/head
lidapeng 4 years ago
parent fd0353b3a3
commit 824874c078

@ -425,6 +425,7 @@ public class Matrix {
if (this.x >= x && this.y >= y) { if (this.x >= x && this.y >= y) {
return matrix[x][y]; return matrix[x][y];
} else { } else {
//System.out.println("x==" + x + ",y==" + y);
throw new Exception("matrix length too little"); throw new Exception("matrix length too little");
} }
} }

@ -0,0 +1,7 @@
package org.wlld.i;
public interface PsoFunction {//粒子群回调函数
//根据参数返回函数值
double getResult(double[] parameter,int id) throws Exception;
}

@ -235,24 +235,24 @@ public class Convolution extends Frequency {
rgb[twoNub + index] = sonB.getNumber(t, k) / 255; rgb[twoNub + index] = sonB.getNumber(t, k) / 255;
} }
} }
//900 200
double dispersed = variance(h); double dispersed = variance(h);
if (dispersed < 900 && dispersed > 200) { if (dispersed < 900 && dispersed > 200) {
for (int m = 0; m < nub; m++) { for (int m = 0; m < nub; m++) {
double[] color = new double[]{rgb[m], rgb[m + nub], rgb[m + twoNub]}; double[] color = new double[]{rgb[m], rgb[m + nub], rgb[m + twoNub]};
meanClustering.setColor(color); meanClustering.setColor(color);
} }
// meanClustering.setColor(rgb);
} }
} }
} }
List<double[]> list = meanClustering.start(true);//开始聚类 //List<double[]> list = meanClustering.start(true);//开始聚类
if (tag == 0) {//识别 meanClustering.start(true);
templeConfig.getFood().getkNerveManger().look(list); // if (tag == 0) {//识别
} else {//训练 // templeConfig.getFood().getkNerveManger().look(list);
templeConfig.getFood().getkNerveManger().setFeature(tag, list); // } else {//训练
} // templeConfig.getFood().getkNerveManger().setFeature(tag, list);
// }
List<RGBNorm> rgbNorms = meanClustering.getMatrices(); List<RGBNorm> rgbNorms = meanClustering.getMatrices();
Collections.sort(rgbNorms, rgbSort);
List<Double> features = new ArrayList<>(); List<Double> features = new ArrayList<>();
for (int i = 0; i < sqNub; i++) { for (int i = 0; i < sqNub; i++) {
double[] rgb = rgbNorms.get(i).getRgb(); double[] rgb = rgbNorms.get(i).getRgb();

@ -84,12 +84,43 @@ public class MeanClustering {
} }
} }
private double[] getListAvg(List<double[]> list) {
int len = list.get(0).length;
double[] sigma = new double[len];
for (double[] rgb : list) {
for (int i = 0; i < len; i++) {
sigma[i] = sigma[i] + rgb[i];
}
}
int size = list.size();
for (int i = 0; i < len; i++) {
sigma[i] = sigma[i] / size;
}
return sigma;
}
private List<double[]> listK(List<double[]> listOne, int nub) {
int size = listOne.size();
int oneSize = size / nub;//几份取一份平均值
//System.out.println("oneSize==" + oneSize);
List<double[]> allList = new ArrayList<>();
for (int i = 0; i <= size - oneSize; i += oneSize) {
double[] avg = getListAvg(listOne.subList(i, i + oneSize));
allList.add(avg);
}
return allList;
}
private List<double[]> startBp() { private List<double[]> startBp() {
int times = 2000; int times = 2000;
int index = 0;
List<double[]> features = new ArrayList<>(); List<double[]> features = new ArrayList<>();
List<List<double[]>> lists = new ArrayList<>(); List<List<double[]>> lists = new ArrayList<>();
for (int j = 0; j < matrices.size(); j++) { for (int j = 0; j < matrices.size(); j++) {
List<double[]> list = matrices.get(j).getRgbs().subList(0, times); List<double[]> listOne = matrices.get(j).getRgbs();
// List<double[]> list = listK(listOne, times);
//System.out.println(listOne.size());
List<double[]> list = listOne.subList(index, times + index);
lists.add(list); lists.add(list);
} }
for (int j = 0; j < times; j++) { for (int j = 0; j < times; j++) {
@ -137,7 +168,7 @@ public class MeanClustering {
return features; return features;
} }
public List<double[]> start(boolean isRegression) throws Exception {//开始聚类 public void start(boolean isRegression) throws Exception {//开始聚类
if (matrixList.size() > 1) { if (matrixList.size() > 1) {
Random random = new Random(); Random random = new Random();
for (int i = 0; i < speciesQuantity; i++) {//初始化均值向量 for (int i = 0; i < speciesQuantity; i++) {//初始化均值向量
@ -149,10 +180,10 @@ public class MeanClustering {
} }
//进行两者的比较 //进行两者的比较
boolean isNext; boolean isNext;
for (int i = 0; i < 40; i++) { for (int i = 0; i < 50; i++) {
averageMatrix(); averageMatrix();
isNext = isNext(); isNext = isNext();
if (isNext && i < 39) { if (isNext && i < 49) {
clear(); clear();
} else { } else {
break; break;
@ -160,15 +191,10 @@ public class MeanClustering {
} }
RGBSort rgbSort = new RGBSort(); RGBSort rgbSort = new RGBSort();
Collections.sort(matrices, rgbSort); Collections.sort(matrices, rgbSort);
for (RGBNorm rgbNorm : matrices) { // for (RGBNorm rgbNorm : matrices) {
rgbNorm.finish(); // rgbNorm.finish();
}
// if (isRegression) {
// return startRegression();
// } else {
// return null;
// } // }
return startBp(); // return startBp();
} else { } else {
throw new Exception("matrixList number less than 2"); throw new Exception("matrixList number less than 2");
} }

@ -121,9 +121,9 @@ public class RGBNorm {
o2Norm = o2Norm + Math.pow(o2[i], 2); o2Norm = o2Norm + Math.pow(o2[i], 2);
} }
if (o1Norm > o2Norm) { if (o1Norm > o2Norm) {
return -1;
} else if (o1Norm < o2Norm) {
return 1; return 1;
} else if (o1Norm < o2Norm) {
return -1;
} }
return 0; return 0;
} }

@ -101,7 +101,7 @@ public class Knn {//KNN分类器
compare(dists, types, dist, type); compare(dists, types, dist, type);
} }
} }
//System.out.println(Arrays.toString(types)); System.out.println(Arrays.toString(types));
Map<Integer, Integer> map = new HashMap<>(); Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nub; i++) { for (int i = 0; i < nub; i++) {
int type = types[i]; int type = types[i];

@ -11,10 +11,10 @@ import org.wlld.i.OutBack;
*/ */
public class RgbBack implements OutBack { public class RgbBack implements OutBack {
private int id = 0; private int id = 0;
private double out = 0; private double out = -2;
public void clear() { public void clear() {
out = 0; out = -2;
id = 0; id = 0;
} }

@ -33,20 +33,13 @@ public class KNerveManger {
this.times = times; this.times = times;
nerveManager = new NerveManager(sensoryNerveNub, 24, speciesNub, nerveManager = new NerveManager(sensoryNerveNub, 24, speciesNub,
1, new Tanh(),//0.008 l1 0.02 1, new Tanh(),//0.008 l1 0.02
false, false, 0.008, RZ.L1, 0.01); false, false, 0.008, RZ.L1, 0.02);
nerveManager.init(true, false, true, true); nerveManager.init(true, false, true, true);
} }
private Map<Integer, Double> createTag(int tag) {//创建一个标注 private Map<Integer, Double> createTag(int tag) {//创建一个标注
Map<Integer, Double> tagging = new HashMap<>(); Map<Integer, Double> tagging = new HashMap<>();
Set<Integer> set = featureMap.keySet(); tagging.put(tag, 1.0);
for (int key : set) {
double value = 0.0;
if (key == tag) {
value = 1.0;
}
tagging.put(key, value);
}
return tagging; return tagging;
} }
@ -78,6 +71,7 @@ public class KNerveManger {
} }
public void startStudy() throws Exception { public void startStudy() throws Exception {
for (int j = 0; j < 2; j++) {
for (int i = 0; i < times; i++) { for (int i = 0; i < times; i++) {
for (Map.Entry<Integer, List<double[]>> entry : featureMap.entrySet()) { for (Map.Entry<Integer, List<double[]>> entry : featureMap.entrySet()) {
int type = entry.getKey(); int type = entry.getKey();
@ -87,13 +81,14 @@ public class KNerveManger {
post(feature, tag, true); post(feature, tag, true);
} }
} }
}
// for (Map.Entry<Integer, List<double[]>> entry : featureMap.entrySet()) { for (Map.Entry<Integer, List<double[]>> entry : featureMap.entrySet()) {
// int type = entry.getKey(); int type = entry.getKey();
// System.out.println("=============================" + type); System.out.println("=============================" + type);
// List<double[]> list = entry.getValue(); List<double[]> list = entry.getValue();
// look(list); look(list);
// } }
} }
private void post(double[] data, Map<Integer, Double> tagging, boolean isStudy) throws Exception { private void post(double[] data, Map<Integer, Double> tagging, boolean isStudy) throws Exception {

@ -58,7 +58,7 @@ public class OutNerve extends Nerve {
if (E.containsKey(getId())) { if (E.containsKey(getId())) {
this.E = E.get(getId()); this.E = E.get(getId());
} else { } else {
this.E = 0; this.E = -1;
} }
if (isShowLog) { if (isShowLog) {
System.out.println("E==" + this.E + ",out==" + out + ",nerveId==" + getId()); System.out.println("E==" + this.E + ",out==" + out + ",nerveId==" + getId());

@ -0,0 +1,201 @@
package org.wlld.pso;
import org.wlld.i.PsoFunction;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* @param
* @DATA
* @Author LiDaPeng
* @Description
*/
public class PSO {
private double globalValue = -1;//当前全局最优值
private int times;//迭代次数
private List<Particle> allPar = new ArrayList<>();//全部粒子集合
private PsoFunction psoFunction;//粒子群执行函数
private double inertialFactor = 0.5;//惯性因子
private double selfStudyFactor = 2;//个体学习因子
private double socialStudyFactor = 2;//社会学习因子
private boolean isMax;//取最大值还是最小值
private double[] allBest;//全局最佳位置
private Random random = new Random();
private int[] minBorder, maxBorder;
private double maxSpeed;
public PSO(int dimensionNub, int[] minBorder, int[] maxBorder,
int times, int particleNub, PsoFunction psoFunction,
double inertialFactor, double selfStudyFactor, double socialStudyFactor
, boolean isMax, double maxSpeed) {
this.times = times;
this.psoFunction = psoFunction;
this.isMax = isMax;
allBest = new double[dimensionNub];
this.minBorder = minBorder;
this.maxBorder = maxBorder;
this.maxSpeed = maxSpeed;
if (inertialFactor > 0) {
this.inertialFactor = inertialFactor;
}
if (selfStudyFactor >= 0 && selfStudyFactor <= 4) {
this.selfStudyFactor = selfStudyFactor;
}
if (socialStudyFactor >= 0 && socialStudyFactor <= 4) {
this.socialStudyFactor = socialStudyFactor;
}
for (int i = 0; i < particleNub; i++) {//初始化生成粒子群
Particle particle = new Particle(dimensionNub);
allPar.add(particle);
}
}
public void setAllPar(List<Particle> allPar) {//外置粒子群注入
this.allPar = allPar;
}
public void start(int fatherX, int fatherY) throws Exception {//开始进行迭代
int size = allPar.size();
for (int i = 0; i < times; i++) {
for (int j = 0; j < size; j++) {
move(allPar.get(j), j);
}
}
//粒子群移动结束
draw("/Users/lidapeng/Desktop/test/testOne/e2.jpg", fatherX, fatherY);
}
private void draw(String path, int fatherX, int fatherY) throws Exception {
File file = new File(path);
FileInputStream fileInputStream = new FileInputStream(file);
BufferedImage image2 = ImageIO.read(fileInputStream);
int width = image2.getWidth();
int height = image2.getHeight();
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) bi.getGraphics();
g2.setColor(Color.CYAN);
g2.drawImage(image2, 0, 0, width, height, null);
int size = allPar.size();
for (int j = 0; j < size; j++) {//输出
Particle particle = allPar.get(j);
double[] parameter = particle.getParameter();
int x = (int) (fatherX + parameter[0]);
int y = (int) (fatherY + parameter[1]);
Rectangle2D rect = new Rectangle2D.Double(y, x, 1, 1);//声明并创建矩形对象,矩形的左上角是(2030)宽是300高是40
g2.draw(rect);
}
String savePath = "/Users/lidapeng/Desktop/test/testTwo/a.jpg";
ImageIO.write(bi, "JPEG", new FileOutputStream(savePath));
}
private void move(Particle particle, int id) throws Exception {//粒子群开始移动
double[] parameter = particle.getParameter();//当前粒子的位置
BestData[] bestData = particle.bestDataArray;//该粒子的信息
double value = psoFunction.getResult(parameter, id);
double selfValue = particle.selfBestValue;//局部最佳值
if (isMax) {//取最大值
if (value > globalValue) {//更新全局最大值
globalValue = value;
//更新全局最佳位置
for (int i = 0; i < allBest.length; i++) {
allBest[i] = parameter[i];
}
}
if (value > selfValue) {//更新局部最大值
particle.selfBestValue = value;
//更新局部最佳位置
for (int i = 0; i < bestData.length; i++) {
bestData[i].selfBestPosition = parameter[i];
}
}
} else {//取最小值
if (globalValue < 0 || value < globalValue) {//更新全局最小值
globalValue = value;
//更新全局最佳位置
for (int i = 0; i < allBest.length; i++) {
allBest[i] = parameter[i];
}
}
if (selfValue < 0 || value < selfValue) {//更新全局最小值
particle.selfBestValue = value;
//更新局部最佳位置
for (int i = 0; i < bestData.length; i++) {
bestData[i].selfBestPosition = parameter[i];
}
}
}
//先更新粒子每个维度的速度
for (int i = 0; i < bestData.length; i++) {
double speed = bestData[i].speed;//当前维度的速度
double pid = bestData[i].selfBestPosition;//当前自己的最佳位置
double selfPosition = parameter[i];//当前自己的位置
double pgd = allBest[i];//当前维度的全局最佳位置
//当前维度更新后的速度
speed = inertialFactor * speed + selfStudyFactor * random.nextDouble() * (pid - selfPosition)
+ socialStudyFactor * random.nextDouble() * (pgd - selfPosition);
if (Math.abs(speed) > maxSpeed) {
if (speed > 0) {
speed = maxSpeed;
} else {
speed = -maxSpeed;
}
}
bestData[i].speed = speed;
//更新该粒子该维度新的位置
double position = selfPosition + speed;
if (position < minBorder[i]) {
position = minBorder[i];
}
if (position > maxBorder[i]) {
position = maxBorder[i];
}
bestData[i].selfPosition = position;
}
}
class Particle {//粒子
private BestData[] bestDataArray;
private double selfBestValue = -1;//自身最优的值
private double[] getParameter() {//获取粒子位置信息
double[] parameter = new double[bestDataArray.length];
for (int i = 0; i < parameter.length; i++) {
parameter[i] = bestDataArray[i].selfPosition;
}
return parameter;
}
protected Particle(int dimensionNub) {//初始化随机位置
bestDataArray = new BestData[dimensionNub];
for (int i = 0; i < dimensionNub; i++) {
int min = minBorder[i];
int max = maxBorder[i];
int region = max - min + 1;
int position = random.nextInt(region) + min;//初始化该维度的位置
bestDataArray[i] = new BestData(position);
}
}
}
class BestData {//数据保存
private BestData(double selfPosition) {
this.selfBestPosition = selfPosition;
this.selfPosition = selfPosition;
}
private double speed = 1;//该粒子当前维度的速度
private double selfBestPosition;//当前维度自身最优的历史位置/自己最优位置的值
private double selfPosition;//当前维度自己现在的位置/也就是当前维度自己的值
}
}

@ -82,11 +82,14 @@ public abstract class Frequency {//统计频数
public double dc(double... m) {//计算离散系数 public double dc(double... m) {//计算离散系数
double ave = average(m);//先计算出平均值 double ave = average(m);//先计算出平均值
double dc = 0;
if (ave > 0) {
double allNub = 0; double allNub = 0;
for (int i = 0; i < m.length; i++) { for (int i = 0; i < m.length; i++) {
allNub = allNub + Math.pow(m[i] - ave, 2); allNub = allNub + Math.pow(m[i] - ave, 2);
} }
double dc = ArithUtil.div(Math.sqrt(ArithUtil.div(allNub, m.length)), ave);//离散系数 dc = ArithUtil.div(Math.sqrt(ArithUtil.div(allNub, m.length)), ave);//离散系数
}
return dc; return dc;
} }

@ -24,17 +24,17 @@ public class DataObservation {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
//372,330,右 最大值 147.44 //372,330,右 最大值 147.44
//377 ,330右 最大值 69.6 //377 ,330右 最大值 69.6
int xp = 123; int xp = 100;
int yp = 165;//290 int yp = 720;//290
observation2("/Users/lidapeng/Desktop/test/testOne/a0.jpg", xp, yp); observation2("/Users/lidapeng/Desktop/test/testOne/e2.jpg", xp, yp);
} }
public static void observation2(String url, int xp, int yp) throws Exception { public static void observation2(String url, int xp, int yp) throws Exception {
Picture picture = new Picture(); Picture picture = new Picture();
ThreeChannelMatrix threeChannelMatrix = picture.getThreeMatrix(url); ThreeChannelMatrix threeChannelMatrix = picture.getThreeMatrix(url);
ThreeChannelMatrix myThreeChannelMatrix = convolution.getRegionMatrix(threeChannelMatrix, xp, yp, 270, 274); ThreeChannelMatrix myThreeChannelMatrix = convolution.getRegionMatrix(threeChannelMatrix, xp, yp, 300, 300);
RegionFeature regionFeature = new RegionFeature(myThreeChannelMatrix, xp, yp); RegionCut regionCut = new RegionCut(myThreeChannelMatrix, xp, yp, 30);
regionFeature.start(); regionCut.start();
} }
public static void observation(String url, int xp, int yp, int size) throws Exception { public static void observation(String url, int xp, int yp, int size) throws Exception {

@ -68,16 +68,16 @@ public class FoodTest {
cutting.setRegionNub(100); cutting.setRegionNub(100);
cutting.setMaxIou(2); cutting.setMaxIou(2);
//knn参数 //knn参数
templeConfig.setKnnNub(1); templeConfig.setKnnNub(15);
//池化比例 //池化比例
templeConfig.setPoolSize(2);//缩小比例 templeConfig.setPoolSize(2);//缩小比例
//聚类 //聚类
templeConfig.setFeatureNub(3);//聚类特征数量 templeConfig.setFeatureNub(5);//聚类特征数量
//菜品识别实体类 //菜品识别实体类
food.setShrink(5);//缩紧像素 food.setShrink(5);//缩紧像素
food.setTimes(1);//聚类数据增强 food.setTimes(1);//聚类数据增强
food.setRegionSize(5); food.setRegionSize(5);
KNerveManger kNerveManger = new KNerveManger(9, 24, 2000); KNerveManger kNerveManger = new KNerveManger(12, 24, 6000);
food.setkNerveManger(kNerveManger); food.setkNerveManger(kNerveManger);
food.setRowMark(0.15);//0.12 food.setRowMark(0.15);//0.12
food.setColumnMark(0.15);//0.25 food.setColumnMark(0.15);//0.25
@ -107,7 +107,7 @@ public class FoodTest {
// ThreeChannelMatrix threeChannelMatrix = picture.getThreeMatrix("/Users/lidapeng/Desktop/myDocument/d.jpg"); // ThreeChannelMatrix threeChannelMatrix = picture.getThreeMatrix("/Users/lidapeng/Desktop/myDocument/d.jpg");
// operation.setTray(threeChannelMatrix); // operation.setTray(threeChannelMatrix);
String name = "/Users/lidapeng/Desktop/test/testOne/"; String name = "/Users/lidapeng/Desktop/test/testOne/";
for (int i = 0; i < 1; i++) { for (int i = 0; i < 3; i++) {
System.out.println("轮数============================" + i); System.out.println("轮数============================" + i);
ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix(name + "a" + i + ".jpg"); ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix(name + "a" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix2 = picture.getThreeMatrix(name + "b" + i + ".jpg"); ThreeChannelMatrix threeChannelMatrix2 = picture.getThreeMatrix(name + "b" + i + ".jpg");
@ -160,62 +160,88 @@ public class FoodTest {
} }
System.out.println("========================"); System.out.println("========================");
kNerveManger.startStudy(); kNerveManger.startStudy();
int i = 0; int i = 3;
ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix(name + "a" + i + ".jpg"); // ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix(name + "a" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix2 = picture.getThreeMatrix(name + "b" + i + ".jpg"); // ThreeChannelMatrix threeChannelMatrix2 = picture.getThreeMatrix(name + "b" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix3 = picture.getThreeMatrix(name + "c" + i + ".jpg"); // ThreeChannelMatrix threeChannelMatrix3 = picture.getThreeMatrix(name + "c" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix4 = picture.getThreeMatrix(name + "d" + i + ".jpg"); // ThreeChannelMatrix threeChannelMatrix4 = picture.getThreeMatrix(name + "d" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix5 = picture.getThreeMatrix(name + "e" + i + ".jpg"); // ThreeChannelMatrix threeChannelMatrix5 = picture.getThreeMatrix(name + "e" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix6 = picture.getThreeMatrix(name + "f" + i + ".jpg"); // ThreeChannelMatrix threeChannelMatrix6 = picture.getThreeMatrix(name + "f" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix7 = picture.getThreeMatrix(name + "g" + i + ".jpg"); // ThreeChannelMatrix threeChannelMatrix7 = picture.getThreeMatrix(name + "g" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix8 = picture.getThreeMatrix(name + "h" + i + ".jpg"); // ThreeChannelMatrix threeChannelMatrix8 = picture.getThreeMatrix(name + "h" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix9 = picture.getThreeMatrix(name + "i" + i + ".jpg"); // ThreeChannelMatrix threeChannelMatrix9 = picture.getThreeMatrix(name + "i" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix10 = picture.getThreeMatrix(name + "j" + i + ".jpg"); // ThreeChannelMatrix threeChannelMatrix10 = picture.getThreeMatrix(name + "j" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix11 = picture.getThreeMatrix(name + "k" + i + ".jpg"); ThreeChannelMatrix threeChannelMatrix11 = picture.getThreeMatrix(name + "k" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix12 = picture.getThreeMatrix(name + "l" + i + ".jpg"); // ThreeChannelMatrix threeChannelMatrix12 = picture.getThreeMatrix(name + "l" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix13 = picture.getThreeMatrix(name + "m" + i + ".jpg"); ThreeChannelMatrix threeChannelMatrix13 = picture.getThreeMatrix(name + "m" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix14 = picture.getThreeMatrix(name + "n" + i + ".jpg"); // ThreeChannelMatrix threeChannelMatrix14 = picture.getThreeMatrix(name + "n" + i + ".jpg");
//
ThreeChannelMatrix threeChannelMatrix15 = picture.getThreeMatrix(name + "o" + i + ".jpg"); // ThreeChannelMatrix threeChannelMatrix15 = picture.getThreeMatrix(name + "o" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix16 = picture.getThreeMatrix(name + "p" + i + ".jpg"); // ThreeChannelMatrix threeChannelMatrix16 = picture.getThreeMatrix(name + "p" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix17 = picture.getThreeMatrix(name + "q" + i + ".jpg"); // ThreeChannelMatrix threeChannelMatrix17 = picture.getThreeMatrix(name + "q" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix18 = picture.getThreeMatrix(name + "r" + i + ".jpg"); // ThreeChannelMatrix threeChannelMatrix18 = picture.getThreeMatrix(name + "r" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix19 = picture.getThreeMatrix(name + "s" + i + ".jpg"); // ThreeChannelMatrix threeChannelMatrix19 = picture.getThreeMatrix(name + "s" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix20 = picture.getThreeMatrix(name + "t" + i + ".jpg"); // ThreeChannelMatrix threeChannelMatrix20 = picture.getThreeMatrix(name + "t" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix21 = picture.getThreeMatrix(name + "u" + i + ".jpg"); // ThreeChannelMatrix threeChannelMatrix21 = picture.getThreeMatrix(name + "u" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix22 = picture.getThreeMatrix(name + "v" + i + ".jpg"); // ThreeChannelMatrix threeChannelMatrix22 = picture.getThreeMatrix(name + "v" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix23 = picture.getThreeMatrix(name + "w" + i + ".jpg"); // ThreeChannelMatrix threeChannelMatrix23 = picture.getThreeMatrix(name + "w" + i + ".jpg");
ThreeChannelMatrix threeChannelMatrix24 = picture.getThreeMatrix(name + "x" + i + ".jpg"); // ThreeChannelMatrix threeChannelMatrix24 = picture.getThreeMatrix(name + "x" + i + ".jpg");
operation.colorLook(threeChannelMatrix1, specificationsList); // operation.colorLook(threeChannelMatrix1, specificationsList);
operation.colorLook(threeChannelMatrix2, specificationsList); // operation.colorLook(threeChannelMatrix2, specificationsList);
operation.colorLook(threeChannelMatrix3, specificationsList); // operation.colorLook(threeChannelMatrix3, specificationsList);
operation.colorLook(threeChannelMatrix4, specificationsList); // operation.colorLook(threeChannelMatrix4, specificationsList);
operation.colorLook(threeChannelMatrix5, specificationsList); // operation.colorLook(threeChannelMatrix5, specificationsList);
operation.colorLook(threeChannelMatrix6, specificationsList); // operation.colorLook(threeChannelMatrix6, specificationsList);
operation.colorLook(threeChannelMatrix7, specificationsList); // operation.colorLook(threeChannelMatrix7, specificationsList);
operation.colorLook(threeChannelMatrix8, specificationsList); // operation.colorLook(threeChannelMatrix8, specificationsList);
operation.colorLook(threeChannelMatrix9, specificationsList); // operation.colorLook(threeChannelMatrix9, specificationsList);
operation.colorLook(threeChannelMatrix10, specificationsList); // operation.colorLook(threeChannelMatrix10, specificationsList);
operation.colorLook(threeChannelMatrix11, specificationsList); operation.colorLook(threeChannelMatrix11, specificationsList);
operation.colorLook(threeChannelMatrix12, specificationsList); // operation.colorLook(threeChannelMatrix12, specificationsList);
operation.colorLook(threeChannelMatrix13, specificationsList); operation.colorLook(threeChannelMatrix13, specificationsList);
operation.colorLook(threeChannelMatrix14, specificationsList); // operation.colorLook(threeChannelMatrix14, specificationsList);
operation.colorLook(threeChannelMatrix15, specificationsList); // operation.colorLook(threeChannelMatrix15, specificationsList);
operation.colorLook(threeChannelMatrix16, specificationsList); // operation.colorLook(threeChannelMatrix16, specificationsList);
operation.colorLook(threeChannelMatrix17, specificationsList); // operation.colorLook(threeChannelMatrix17, specificationsList);
operation.colorLook(threeChannelMatrix18, specificationsList); // operation.colorLook(threeChannelMatrix18, specificationsList);
operation.colorLook(threeChannelMatrix19, specificationsList); // operation.colorLook(threeChannelMatrix19, specificationsList);
operation.colorLook(threeChannelMatrix20, specificationsList); // operation.colorLook(threeChannelMatrix20, specificationsList);
operation.colorLook(threeChannelMatrix21, specificationsList); // operation.colorLook(threeChannelMatrix21, specificationsList);
operation.colorLook(threeChannelMatrix22, specificationsList); // operation.colorLook(threeChannelMatrix22, specificationsList);
operation.colorLook(threeChannelMatrix23, specificationsList); // operation.colorLook(threeChannelMatrix23, specificationsList);
operation.colorLook(threeChannelMatrix24, specificationsList); // operation.colorLook(threeChannelMatrix24, specificationsList);
//
// test3(threeChannelMatrix1, operation, specificationsList, 1);
// test3(threeChannelMatrix2, operation, specificationsList, 2);
// test3(threeChannelMatrix3, operation, specificationsList, 3);
// test3(threeChannelMatrix4, operation, specificationsList, 4);
// test3(threeChannelMatrix5, operation, specificationsList, 5);
// test3(threeChannelMatrix6, operation, specificationsList, 6);
// test3(threeChannelMatrix7, operation, specificationsList, 7);
// test3(threeChannelMatrix8, operation, specificationsList, 8);
// test3(threeChannelMatrix9, operation, specificationsList, 9);
// test3(threeChannelMatrix10, operation, specificationsList, 10);
// test3(threeChannelMatrix11, operation, specificationsList, 11);
// test3(threeChannelMatrix12, operation, specificationsList, 12);
// test3(threeChannelMatrix13, operation, specificationsList, 13);
// test3(threeChannelMatrix14, operation, specificationsList, 14);
// test3(threeChannelMatrix15, operation, specificationsList, 15);
// test3(threeChannelMatrix16, operation, specificationsList, 16);
// test3(threeChannelMatrix17, operation, specificationsList, 17);
// test3(threeChannelMatrix18, operation, specificationsList, 18);
// test3(threeChannelMatrix19, operation, specificationsList, 19);
// test3(threeChannelMatrix20, operation, specificationsList, 20);
// test3(threeChannelMatrix21, operation, specificationsList, 21);
// test3(threeChannelMatrix22, operation, specificationsList, 22);
// test3(threeChannelMatrix23, operation, specificationsList, 23);
//test3(threeChannelMatrix24, operation, specificationsList, 24);
} }
private static void test3(ThreeChannelMatrix threeChannelMatrix, Operation operation, List<Specifications> specifications) throws Exception { private static void test3(ThreeChannelMatrix threeChannelMatrix, Operation operation, List<Specifications> specifications,
int realType) throws Exception {
int type = operation.colorLook(threeChannelMatrix, specifications).get(0).getType(); int type = operation.colorLook(threeChannelMatrix, specifications).get(0).getType();
System.out.println(type); System.out.println(type + ",realType==" + realType);
} }
public static void study() throws Exception { public static void study() throws Exception {

@ -0,0 +1,56 @@
package coverTest.regionCut;
import org.wlld.MatrixTools.Matrix;
import org.wlld.i.PsoFunction;
import org.wlld.imageRecognition.ThreeChannelMatrix;
import org.wlld.tools.Frequency;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @param
* @DATA
* @Author LiDaPeng
* @Description
*/
public class ColorFunction extends Frequency implements PsoFunction {
private Matrix matrixR;
private Matrix matrixG;
private Matrix matrixB;
private Map<Integer, double[]> pixels = new HashMap<>();
public ColorFunction(ThreeChannelMatrix threeChannelMatrix) {
matrixR = threeChannelMatrix.getMatrixR();
matrixG = threeChannelMatrix.getMatrixG();
matrixB = threeChannelMatrix.getMatrixB();
}
@Override
public double getResult(double[] parameter, int id) throws Exception {
int x = (int) parameter[0];
int y = (int) parameter[1];
double[] rgb = new double[]{matrixR.getNumber(x, y) / 255, matrixG.getNumber(x, y) / 255,
matrixB.getNumber(x, y) / 255};
pixels.put(id, rgb);
//计算当前方差
return getDist();
}
private double getDist() {//计算当前均方误差
double[] r = new double[pixels.size()];
double[] g = new double[pixels.size()];
double[] b = new double[pixels.size()];
for (Map.Entry<Integer, double[]> entry : pixels.entrySet()) {
double[] rgb = entry.getValue();
int key = entry.getKey();
r[key] = rgb[0];
g[key] = rgb[1];
b[key] = rgb[2];
}
return dc(r) + dc(g) + dc(b);
}
}

File diff suppressed because it is too large Load Diff

@ -1,8 +1,5 @@
package coverTest.regionCut; package coverTest.regionCut;
import java.util.ArrayList;
import java.util.List;
/** /**
* @param * @param
* @DATA * @DATA
@ -10,6 +7,11 @@ import java.util.List;
* @Description * @Description
*/ */
public class RegionCutBody { public class RegionCutBody {
private List<Integer> pixels = new ArrayList<>(); private double feature;//特征值
private int x;
private int y;
private int id;
private int minId;//最近id
private int channel;//渠道
} }

@ -17,9 +17,6 @@ import java.util.Arrays;
*/ */
public class RegionFeature extends Frequency { public class RegionFeature extends Frequency {
private Matrix matrix; private Matrix matrix;
private Matrix matrixR;
private Matrix matrixG;
private Matrix matrixB;
private Matrix kernel = Kernel.Big; private Matrix kernel = Kernel.Big;
private int fatherX; private int fatherX;
private int fatherY; private int fatherY;
@ -29,9 +26,6 @@ public class RegionFeature extends Frequency {
this.matrix = threeChannelMatrix.getMatrixRGB(); this.matrix = threeChannelMatrix.getMatrixRGB();
this.fatherX = fatherX; this.fatherX = fatherX;
this.fatherY = fatherY; this.fatherY = fatherY;
matrixR = threeChannelMatrix.getMatrixR();
matrixG = threeChannelMatrix.getMatrixG();
matrixB = threeChannelMatrix.getMatrixB();
} }
private double getMatrixVar(Matrix matrix) throws Exception { private double getMatrixVar(Matrix matrix) throws Exception {

Loading…
Cancel
Save