增加粒子群

This commit is contained in:
lidapeng 2020-10-20 16:11:57 +08:00
parent fd0353b3a3
commit 824874c078
17 changed files with 460 additions and 370 deletions

View File

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

View File

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

View File

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

View File

@ -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() {
int times = 2000;
int index = 0;
List<double[]> features = new ArrayList<>();
List<List<double[]>> lists = new ArrayList<>();
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);
}
for (int j = 0; j < times; j++) {
@ -137,7 +168,7 @@ public class MeanClustering {
return features;
}
public List<double[]> start(boolean isRegression) throws Exception {//开始聚类
public void start(boolean isRegression) throws Exception {//开始聚类
if (matrixList.size() > 1) {
Random random = new Random();
for (int i = 0; i < speciesQuantity; i++) {//初始化均值向量
@ -149,10 +180,10 @@ public class MeanClustering {
}
//进行两者的比较
boolean isNext;
for (int i = 0; i < 40; i++) {
for (int i = 0; i < 50; i++) {
averageMatrix();
isNext = isNext();
if (isNext && i < 39) {
if (isNext && i < 49) {
clear();
} else {
break;
@ -160,15 +191,10 @@ public class MeanClustering {
}
RGBSort rgbSort = new RGBSort();
Collections.sort(matrices, rgbSort);
for (RGBNorm rgbNorm : matrices) {
rgbNorm.finish();
}
// if (isRegression) {
// return startRegression();
// } else {
// return null;
// for (RGBNorm rgbNorm : matrices) {
// rgbNorm.finish();
// }
return startBp();
// return startBp();
} else {
throw new Exception("matrixList number less than 2");
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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;//当前维度自己现在的位置/也就是当前维度自己的值
}
}

View File

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

View File

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

View File

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

View File

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

View File

@ -1,12 +1,10 @@
package coverTest.regionCut;
import org.wlld.Ma;
import org.wlld.MatrixTools.Matrix;
import org.wlld.config.Kernel;
import org.wlld.imageRecognition.ThreeChannelMatrix;
import org.wlld.pso.PSO;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
/**
* @param
@ -15,8 +13,7 @@ import java.util.Map;
* @Description 分区切割
*/
public class RegionCut {
private Matrix matrixH;
private Matrix regionMatrix;//分区地图
private ThreeChannelMatrix threeChannelMatrix;
private int fatherX;
private int fatherY;
private int size;
@ -24,261 +21,43 @@ public class RegionCut {
private Map<Integer, Double> minMap = new HashMap<>();//保存最小值
private Map<Integer, Double> maxMap = new HashMap<>();//保存最大值
public RegionCut(Matrix matrixH, int fatherX, int fatherY, int size) {
this.matrixH = matrixH;
public RegionCut(ThreeChannelMatrix threeChannelMatrix, int fatherX, int fatherY, int size) {
this.fatherX = fatherX;
this.fatherY = fatherY;
this.size = size;
regionMatrix = new Matrix(matrixH.getX(), matrixH.getY());
this.threeChannelMatrix = threeChannelMatrix;
}
private void setLimit(int id, double pixel) {
double min = minMap.get(id);
double max = maxMap.get(id);
if (pixel > max) {
maxMap.put(id, pixel);
}
if (pixel < min) {
minMap.put(id, pixel);
}
}
private void firstCut() throws Exception {//进行第一次切割
int x = matrixH.getX();
int y = matrixH.getY();
int size = x * y;
System.out.println("像素数量:" + size);
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
double regionId = regionMatrix.getNumber(i, j);
if (regionId < 0.5) {//该像素没有被连接
boolean isStop;
double self = matrixH.getNumber(i, j);//灰度值
regionMatrix.setNub(i, j, id);
minMap.put(id, self);
maxMap.put(id, self);
//System.out.println(regionMatrix.getString());
int xi = i;
int yj = j;
do {
double mySelf = matrixH.getNumber(xi, yj);//灰度值
int pixel = pixelLine(xi, yj, mySelf);
int column = pixel & 0xfff;
int row = (pixel >> 12) & 0xfff;
double type = regionMatrix.getNumber(row, column);
if (type < 0.5) {//可以连接
regionMatrix.setNub(row, column, id);//进行连接
setLimit(id, mySelf);
double mySelfSon = matrixH.getNumber(row, column);//灰度值
int pixelOther = pixelLine(row, column, mySelfSon);
int column2 = pixelOther & 0xfff;
int row2 = (pixelOther >> 12) & 0xfff;
isStop = row2 == xi && column2 == yj;
xi = row;
yj = column;
} else {//已经被连接了跳出
isStop = true;
}
} while (!isStop);
id++;
}
}
}
System.out.println("第一次选区数量:" + id);
ColorFunction colorFunction = new ColorFunction(threeChannelMatrix);
int[] minBorder = new int[]{0, 0};
int[] maxBorder = new int[]{299, 299};
PSO pso = new PSO(2, minBorder, maxBorder, 400, 200,
colorFunction, 0.1, 0.1, 0.1, true, 10);
pso.start(fatherX, fatherY);
// Matrix matrixR = threeChannelMatrix.getMatrixR();
// Matrix matrixG = threeChannelMatrix.getMatrixG();
// Matrix matrixB = threeChannelMatrix.getMatrixB();
// Matrix matrixRGB = threeChannelMatrix.getMatrixRGB();
// int x = matrixR.getX();
// int y = matrixR.getY();
// int index = 0;
// for (int i = 0; i <= x - size; i += size) {
// for (int j = 0; j <= y - size; j += size) {
// Matrix matrixRs = matrixR.getSonOfMatrix(i, j, size, size);
// Matrix matrixGs = matrixG.getSonOfMatrix(i, j, size, size);
// Matrix matrixBs = matrixB.getSonOfMatrix(i, j, size, size);
// index++;
// }
// }
}
public void secondCut() throws Exception {//二切
int x = matrixH.getX();
int y = matrixH.getY();
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
double key = regionMatrix.getNumber(i, j);//与周围八方向比较看是否有异类
getOther(i, j, (int) key, matrixH.getNumber(i, j));
}
}
}
private void updateType(int type, int toType) throws Exception {
int x = regionMatrix.getX();
int y = regionMatrix.getY();
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
if (regionMatrix.getNumber(i, j) == type) {
regionMatrix.setNub(i, j, toType);
}
}
}
}
private void getOther(int x, int y, int key, double self) throws Exception {
double[] pixels = getPixels(x, y, false);
for (int i = 0; i < pixels.length; i++) {
int pix = (int) pixels[i];
if (pix > 0 && pix != key) {//接壤的非同类
double min = minMap.get(pix);
double max = maxMap.get(pix);
double maxDist = max - min;
int row = x;
int column = y;
switch (i) {
case 0://
row = x - 1;
break;
case 1://
column = y - 1;
break;
case 2://
row = x + 1;
break;
case 3://
column = y + 1;
break;
case 4://左上
column = y - 1;
row = x - 1;
break;
case 5://左下
column = y - 1;
row = x + 1;
break;
case 6://右下
column = y + 1;
row = x + 1;
break;
case 7://右上
column = y + 1;
row = x - 1;
break;
}
double dist = Math.abs(matrixH.getNumber(row, column) - self);
if (dist < maxDist * 0.2) {//两个选区可以合并
setLimit(key, min);
setLimit(key, max);
updateType(pix, key);
id--;
}
break;
}
}
}
public void start() throws Exception {
firstCut();//初切
System.out.println("区域数量1" + id);
for (int i = 0; i < 1; i++) {
secondCut();//二切
}
System.out.println("区域数量2" + id);
System.out.println(regionMatrix.getString());
firstCut();
}
private int pixelLine(int x, int y, double self) throws Exception {
double[] pixels = getPixels(x, y, true);
int minIndex = getMinIndex(pixels, self);
int row = x;
int column = y;
switch (minIndex) {
case 0://
row = x - 1;
break;
case 1://
column = y - 1;
break;
case 2://
row = x + 1;
break;
case 3://
column = y + 1;
break;
case 4://左上
column = y - 1;
row = x - 1;
break;
case 5://左下
column = y - 1;
row = x + 1;
break;
case 6://右下
column = y + 1;
row = x + 1;
break;
case 7://右上
column = y + 1;
row = x - 1;
break;
}
return row << 12 | column;
}
private double[] getPixels(int x, int y, boolean isFirst) throws Exception {
double left = 1, leftTop = 1, leftBottom = 1, right = 1, rightTop = 1, rightBottom = 1, top = 1, bottom = 1;
Matrix matrix;
if (isFirst) {
matrix = matrixH;
} else {
matrix = regionMatrix;
}
if (x == 0) {
top = -1;
leftTop = -1;
rightTop = -1;
}
if (y == 0) {
leftTop = -1;
left = -1;
leftBottom = -1;
}
if (x == size - 1) {
leftBottom = -1;
bottom = -1;
rightBottom = -1;
}
if (y == size - 1) {
rightTop = -1;
right = -1;
rightBottom = -1;
}
if (top > 0) {
top = matrix.getNumber(x - 1, y);
}
if (left > 0) {
left = matrix.getNumber(x, y - 1);
}
if (right > 0) {
right = matrix.getNumber(x, y + 1);
}
if (bottom > 0) {
bottom = matrix.getNumber(x + 1, y);
}
if (leftTop > 0) {
leftTop = matrix.getNumber(x - 1, y - 1);
}
if (leftBottom > 0) {
leftBottom = matrix.getNumber(x + 1, y - 1);
}
if (rightTop > 0) {
rightTop = matrix.getNumber(x - 1, y + 1);
}
if (rightBottom > 0) {
rightBottom = matrix.getNumber(x + 1, y + 1);
}
return new double[]{top, left, bottom, right, leftTop, leftBottom, rightBottom, rightTop};
}
private int getMinIndex(double[] array, double self) {//获取最小值
double min = -1;
int minIdx = 0;
for (int i = 0; i < array.length; i++) {
double nub = array[i];
if (nub > 0) {
nub = Math.abs(nub - self);
if (min < 0 || nub < min) {
min = nub;
minIdx = i;
}
}
}
return minIdx;
}
}

View File

@ -1,8 +1,5 @@
package coverTest.regionCut;
import java.util.ArrayList;
import java.util.List;
/**
* @param
* @DATA
@ -10,6 +7,11 @@ import java.util.List;
* @Description
*/
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;//渠道
}

View File

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