增加粒子群特征筛选

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

@ -56,6 +56,7 @@ public class Matrix {
private void setState(int x, int y) {
if (x == 1 && y == 1) {
isZero = true;
isVector = true;
} else if (x == 1 || y == 1) {
isVector = true;
if (x == 1) {

@ -46,7 +46,7 @@ public class MatrixOperation {
//多元线性回归
public static Matrix getLinearRegression(Matrix parameter, Matrix out) throws Exception {
if (parameter.getX() == out.getX() && out.isVector() && !out.isRowVector()) {
if (parameter.getX() == out.getX() && out.isVector()) {
//将参数矩阵转置
Matrix matrix1 = transPosition(parameter);
//转置的参数矩阵乘以参数矩阵

@ -6,6 +6,10 @@ import org.wlld.config.Kernel;
import org.wlld.imageRecognition.border.Border;
import org.wlld.imageRecognition.border.Frame;
import org.wlld.imageRecognition.border.FrameBody;
import org.wlld.imageRecognition.segmentation.ColorFunction;
import org.wlld.imageRecognition.segmentation.DimensionMapping;
import org.wlld.param.Food;
import org.wlld.pso.PSO;
import org.wlld.tools.ArithUtil;
import org.wlld.tools.Frequency;
@ -94,113 +98,64 @@ public class Convolution extends Frequency {
return features;
}
public void filtering(ThreeChannelMatrix threeChannelMatrix) throws Exception {//平滑滤波
public List<Double> getCenterColor(ThreeChannelMatrix threeChannelMatrix, TempleConfig templeConfig,
boolean isStudy, int tag) throws Exception {
Matrix matrixR = threeChannelMatrix.getMatrixR();
Matrix matrixG = threeChannelMatrix.getMatrixG();
Matrix matrixB = threeChannelMatrix.getMatrixB();
int x = matrixR.getX();
int y = matrixR.getY();
double nub = x * y;
double sigmaR = 0;
double sigmaG = 0;
double sigmaB = 0;
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
sigmaR = matrixR.getNumber(i, j) + sigmaR;
sigmaG = matrixG.getNumber(i, j) + sigmaG;
sigmaB = matrixB.getNumber(i, j) + sigmaB;
}
}
double r = sigmaR / nub;
double g = sigmaG / nub;
double b = sigmaB / nub;
MatrixOperation.mathDiv(matrixR, r);
MatrixOperation.mathDiv(matrixG, g);
MatrixOperation.mathDiv(matrixB, b);
}
int maxX = matrixR.getX();
int maxY = matrixR.getY();
ColorFunction colorFunction = new ColorFunction(threeChannelMatrix);
int[] minBorder = new int[]{0, 0};
int[] maxBorder = new int[]{maxX, maxY};
//创建粒子群
PSO pso = new PSO(2, minBorder, maxBorder, 100, 100,
colorFunction, 0.2, 1, 0.5, true, 10, 1);
List<double[]> positions = pso.start(0, 0);
List<double[]> feature = new ArrayList<>();//像素特征
for (int i = 0; i < positions.size(); i++) {
double[] parameter = positions.get(i);
//获取取样坐标
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};
feature.add(rgb);
}
if (isStudy) {//进行新维度映射排序
pso = new PSO(3, null, null, 200, 50,
new DimensionMapping(feature), 0.5, 2, 2, true, 0.5, 0.001);
List<double[]> mappings = pso.start(0, 0);//最小映射集合
double[] sigmaMapping = new double[3];//最小映射
for (int i = 0; i < mappings.size(); i++) {
double[] mapping = mappings.get(i);
for (int j = 0; j < sigmaMapping.length; j++) {
sigmaMapping[j] = sigmaMapping[j] + mapping[j];
}
}
for (int j = 0; j < sigmaMapping.length; j++) {
sigmaMapping[j] = sigmaMapping[j] / mappings.size();
}
//保存类别及映射
Food food = templeConfig.getFood();
Map<Integer, double[]> allMappings = food.getMappings();
if (allMappings == null) {
allMappings = new HashMap<>();
food.setMappings(allMappings);
}
allMappings.put(tag, sigmaMapping);
//根据映射值进行排序
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;
}
} else {
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, TempleConfig templeConfig) throws Exception {
Matrix matrixR = threeChannelMatrix.getMatrixR();
Matrix matrixG = threeChannelMatrix.getMatrixG();
Matrix matrixB = threeChannelMatrix.getMatrixB();
// matrixR = late(matrixR, poolSize);
// matrixG = late(matrixG, poolSize);
// matrixB = late(matrixB, poolSize);
RGBSort rgbSort = new RGBSort();
int x = matrixR.getX();
int y = matrixR.getY();
MeanClustering meanClustering = new MeanClustering(sqNub, templeConfig, true);
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
double[] color = new double[]{matrixR.getNumber(i, j), matrixG.getNumber(i, j), matrixB.getNumber(i, j)};
meanClustering.setColor(color);
}
}
meanClustering.start(false);
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();
// RgbRegression rgbRegression = rgbNorms.get(i).getRgbRegression();
//double[] rgb = new double[]{rgbRegression.getWr(), rgbRegression.getWg(), rgbRegression.getB()};
for (int j = 0; j < 3; j++) {
features.add(rgb[j]);
}
}
//测试卷积
//dispersed(matrixR, matrixG, matrixB, rgbNorms);
//System.out.println("feature==" + feature);
return features;
return null;
}
public List<Double> getCenterTexture(ThreeChannelMatrix threeChannelMatrix, int size, int poolSize, TempleConfig templeConfig
, int sqNub, int tag) throws Exception {
RGBSort rgbSort = new RGBSort();
MeanClustering meanClustering = new MeanClustering(sqNub, templeConfig, true);
Matrix matrixR = threeChannelMatrix.getMatrixR();
Matrix matrixG = threeChannelMatrix.getMatrixG();
@ -208,13 +163,6 @@ public class Convolution extends Frequency {
Matrix matrixRGB = threeChannelMatrix.getMatrixRGB();
int xn = matrixR.getX();
int yn = matrixR.getY();
// for (int i = 0; i < xn; i++) {
// for (int j = 0; j < yn; j++) {
// double[] rgb = new double[]{matrixR.getNumber(i, j), matrixG.getNumber(i, j)
// , matrixB.getNumber(i, j)};
// meanClustering.setColor(rgb);
// }
// }
//局部特征选区筛选
int nub = size * size;
int twoNub = nub * 2;

@ -63,7 +63,6 @@ public class Operation {//进行计算
Matrix matrixR = threeChannelMatrix.getMatrixR();
Matrix matrixG = threeChannelMatrix.getMatrixG();
Matrix matrixB = threeChannelMatrix.getMatrixB();
//Matrix matrixRGB = threeChannelMatrix.getMatrixRGB();
Random random = new Random();
int x = matrixR.getX();
int y = matrixR.getY();
@ -77,21 +76,9 @@ public class Operation {//进行计算
rgbRegression.insertRGB(rgb);
}
rgbRegression.regression();
// double[] rgb = new double[]{164 / 255, 189 / 255, 193 / 255};
// double dis = rgbRegression.getDisError(rgb);
// System.out.println("dis==" + dis);
templeConfig.getFood().getTrayBody().add(rgbRegression);
}
private void cutPic(ThreeChannelMatrix threeChannelMatrix, int x, int y, int xSize, int ySize) {
Matrix matrixR = threeChannelMatrix.getMatrixR();
Matrix matrixG = threeChannelMatrix.getMatrixG();
Matrix matrixB = threeChannelMatrix.getMatrixB();
threeChannelMatrix.setMatrixR(matrixR.getSonOfMatrix(x, y, xSize, ySize));
threeChannelMatrix.setMatrixG(matrixG.getSonOfMatrix(x, y, xSize, ySize));
threeChannelMatrix.setMatrixB(matrixB.getSonOfMatrix(x, y, xSize, ySize));
}
public RegionBody colorStudy(ThreeChannelMatrix threeChannelMatrix, int tag, List<Specifications> specificationsList
, String url) throws Exception {
Watershed watershed = new Watershed(threeChannelMatrix, specificationsList, templeConfig);

@ -0,0 +1,35 @@
package org.wlld.imageRecognition.modelEntity;
/**
* @param
* @DATA
* @Author LiDaPeng
* @Description
*/
public class MappingBody {
private double[] feature;//特征
private double mappingNub;//映射好的值
public MappingBody(double[] mapping, double[] feature) {
this.feature = feature;
double sigma = 0;
for (int i = 0; i < mapping.length; i++) {
sigma = sigma + Math.pow(mapping[i], 2);
}
sigma = Math.sqrt(sigma);//映射维度的莫
double s = 0;
for (int j = 0; j < feature.length; j++) {
s = s + feature[j] * mapping[j];
}
mappingNub = s / sigma;
}
public double[] getFeature() {
return feature;
}
public double getMappingNub() {
return mappingNub;
}
}

@ -0,0 +1,72 @@
package org.wlld.imageRecognition.segmentation;
import org.wlld.MatrixTools.Matrix;
import org.wlld.i.PsoFunction;
import org.wlld.imageRecognition.ThreeChannelMatrix;
import org.wlld.tools.Frequency;
import java.util.HashMap;
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();
//return getRegression();
}
private double getRegression() throws Exception {
RgbRegression rgbRegression = new RgbRegression(pixels.size());
for (Map.Entry<Integer, double[]> entry : pixels.entrySet()) {
double[] rgb = entry.getValue();
rgbRegression.insertRGB(rgb);
}
double sigma = 0;
if (rgbRegression.regression()) {
for (Map.Entry<Integer, double[]> entry : pixels.entrySet()) {
double[] rgb = entry.getValue();
sigma = sigma + rgbRegression.getDisError(rgb);
}
sigma = sigma / pixels.size();
}
return sigma;
}
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);
}
}

@ -0,0 +1,41 @@
package org.wlld.imageRecognition.segmentation;
import org.wlld.i.PsoFunction;
import org.wlld.tools.Frequency;
import java.util.List;
/**
* @param
* @DATA
* @Author LiDaPeng
* @Description
*/
public class DimensionMapping extends Frequency implements PsoFunction {
private List<double[]> features;
private int size;
public DimensionMapping(List<double[]> features) {
this.features = features;
size = features.size();
}
@Override
public double getResult(double[] parameter, int id) throws Exception {
double sigma = 0;
for (int i = 0; i < parameter.length; i++) {
sigma = sigma + Math.pow(parameter[i], 2);
}
sigma = Math.sqrt(sigma);
double[] mapping = new double[size];//在新维度的映射集合
for (int i = 0; i < size; i++) {
double[] feature = features.get(i);
double s = 0;
for (int j = 0; j < feature.length; j++) {
s = s + feature[j] * parameter[j];
}
mapping[i] = s / sigma;
}
return variance(mapping);
}
}

@ -5,6 +5,7 @@ import org.wlld.imageRecognition.segmentation.RgbRegression;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @param
@ -14,7 +15,6 @@ import java.util.List;
*/
public class Food {
private int shrink = 60;//收缩参数
private int times = 10;//聚类增强次数
private double rowMark = 0.12;//行痕迹过滤
private double columnMark = 0.25;//列痕迹过滤
private List<RgbRegression> trayBody = new ArrayList<>();//托盘实体参数
@ -22,9 +22,17 @@ public class Food {
private double trayTh = 0.1;//托盘回归阈值
private int regionSize = 5;//纹理区域大小
private int step = 1;//特征取样步长
private double dispersedTh = 0.3;//选区筛选离散阈值
private int speciesNub = 24;//种类数
private KNerveManger kNerveManger;
private Map<Integer, double[]> mappings;//类别映射集合
public Map<Integer, double[]> getMappings() {
return mappings;
}
public void setMappings(Map<Integer, double[]> mappings) {
this.mappings = mappings;
}
public KNerveManger getkNerveManger() {
return kNerveManger;
@ -50,13 +58,6 @@ public class Food {
this.step = step;
}
public double getDispersedTh() {
return dispersedTh;
}
public void setDispersedTh(double dispersedTh) {
this.dispersedTh = dispersedTh;
}
public int getRegionSize() {
return regionSize;
@ -113,12 +114,4 @@ public class Food {
public void setShrink(int shrink) {
this.shrink = shrink;
}
public int getTimes() {
return times;
}
public void setTimes(int times) {
this.times = times;
}
}

@ -32,11 +32,13 @@ public class PSO {
private Random random = new Random();
private int[] minBorder, maxBorder;
private double maxSpeed;
private double initSpeed;//初始速度
public PSO(int dimensionNub, int[] minBorder, int[] maxBorder,
int times, int particleNub, PsoFunction psoFunction,
double inertialFactor, double selfStudyFactor, double socialStudyFactor
, boolean isMax, double maxSpeed) {
, boolean isMax, double maxSpeed, double initSpeed) {
this.initSpeed = initSpeed;
this.times = times;
this.psoFunction = psoFunction;
this.isMax = isMax;
@ -64,17 +66,23 @@ public class PSO {
this.allPar = allPar;
}
public void start(int fatherX, int fatherY) throws Exception {//开始进行迭代
public List<double[]> 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);
}
}
List<double[]> feature = new ArrayList<>();
for (int i = 0; i < size; i++) {
feature.add(allPar.get(i).getParameter());
}
return feature;
//粒子群移动结束
draw("/Users/lidapeng/Desktop/test/testOne/e2.jpg", fatherX, fatherY);
// 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);
@ -94,7 +102,7 @@ public class PSO {
Rectangle2D rect = new Rectangle2D.Double(y, x, 1, 1);//声明并创建矩形对象,矩形的左上角是(2030)宽是300高是40
g2.draw(rect);
}
String savePath = "/Users/lidapeng/Desktop/test/testTwo/a.jpg";
String savePath = "/Users/lidapeng/Desktop/test/testTwo/d.jpg";
ImageIO.write(bi, "JPEG", new FileOutputStream(savePath));
}
@ -153,12 +161,14 @@ public class PSO {
bestData[i].speed = speed;
//更新该粒子该维度新的位置
double position = selfPosition + speed;
if (minBorder != null) {
if (position < minBorder[i]) {
position = minBorder[i];
}
if (position > maxBorder[i]) {
position = maxBorder[i];
}
}
bestData[i].selfPosition = position;
}
}
@ -178,23 +188,29 @@ public class PSO {
protected Particle(int dimensionNub) {//初始化随机位置
bestDataArray = new BestData[dimensionNub];
for (int i = 0; i < dimensionNub; i++) {
double position;
if (minBorder != null && maxBorder != null) {
int min = minBorder[i];
int max = maxBorder[i];
int region = max - min + 1;
int position = random.nextInt(region) + min;//初始化该维度的位置
bestDataArray[i] = new BestData(position);
position = random.nextInt(region) + min;//初始化该维度的位置
} else {
position = random.nextDouble();
}
bestDataArray[i] = new BestData(position, initSpeed);
}
}
}
class BestData {//数据保存
private BestData(double selfPosition) {
private BestData(double selfPosition, double initSpeed) {
this.selfBestPosition = selfPosition;
this.selfPosition = selfPosition;
speed = initSpeed;
}
private double speed = 1;//该粒子当前维度的速度
private double speed;//该粒子当前维度的速度
private double selfBestPosition;//当前维度自身最优的历史位置/自己最优位置的值
private double selfPosition;//当前维度自己现在的位置/也就是当前维度自己的值
}

@ -24,9 +24,9 @@ public class DataObservation {
public static void main(String[] args) throws Exception {
//372,330,右 最大值 147.44
//377 ,330右 最大值 69.6
int xp = 100;
int yp = 720;//290
observation2("/Users/lidapeng/Desktop/test/testOne/e2.jpg", xp, yp);
int xp = 100;//100 2
int yp = 720;//720 2
observation2("/Users/lidapeng/Desktop/test/testOne/e2.jpg", xp, yp);//2
}
public static void observation2(String url, int xp, int yp) throws Exception {

@ -75,7 +75,6 @@ public class FoodTest {
templeConfig.setFeatureNub(5);//聚类特征数量
//菜品识别实体类
food.setShrink(5);//缩紧像素
food.setTimes(1);//聚类数据增强
food.setRegionSize(5);
KNerveManger kNerveManger = new KNerveManger(12, 24, 6000);
food.setkNerveManger(kNerveManger);
@ -83,7 +82,6 @@ public class FoodTest {
food.setColumnMark(0.15);//0.25
food.setRegressionNub(20000);
food.setTrayTh(0.08);
food.setDispersedTh(0.5);
templeConfig.setClassifier(Classifier.KNN);
templeConfig.init(StudyPattern.Cover_Pattern, true, 400, 400, 3);
if (modelParameter != null) {

@ -3,6 +3,7 @@ package coverTest.regionCut;
import org.wlld.MatrixTools.Matrix;
import org.wlld.i.PsoFunction;
import org.wlld.imageRecognition.ThreeChannelMatrix;
import org.wlld.imageRecognition.segmentation.RgbRegression;
import org.wlld.tools.Frequency;
import java.util.ArrayList;
@ -37,6 +38,24 @@ public class ColorFunction extends Frequency implements PsoFunction {
pixels.put(id, rgb);
//计算当前方差
return getDist();
//return getRegression();
}
private double getRegression() throws Exception {
RgbRegression rgbRegression = new RgbRegression(pixels.size());
for (Map.Entry<Integer, double[]> entry : pixels.entrySet()) {
double[] rgb = entry.getValue();
rgbRegression.insertRGB(rgb);
}
double sigma = 0;
if (rgbRegression.regression()) {
for (Map.Entry<Integer, double[]> entry : pixels.entrySet()) {
double[] rgb = entry.getValue();
sigma = sigma + rgbRegression.getDisError(rgb);
}
sigma = sigma / pixels.size();
}
return sigma;
}
private double getDist() {//计算当前均方误差

@ -33,9 +33,12 @@ public class RegionCut {
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);
long a = System.currentTimeMillis();
PSO pso = new PSO(2, minBorder, maxBorder, 100, 100,
colorFunction, 0.2, 1, 0.5, true, 10, 1);
pso.start(fatherX, fatherY);
long b = System.currentTimeMillis() - a;
System.out.println("时间:" + b);
// Matrix matrixR = threeChannelMatrix.getMatrixR();
// Matrix matrixG = threeChannelMatrix.getMatrixG();
// Matrix matrixB = threeChannelMatrix.getMatrixB();

Loading…
Cancel
Save