parent
877694fae2
commit
003804ec22
@ -0,0 +1,37 @@
|
||||
package org.wlld.imageRecognition.modelEntity;
|
||||
|
||||
import org.wlld.MatrixTools.Matrix;
|
||||
import org.wlld.i.OutBack;
|
||||
|
||||
/**
|
||||
* @param
|
||||
* @DATA
|
||||
* @Author LiDaPeng
|
||||
* @Description
|
||||
*/
|
||||
public class RgbBack implements OutBack {
|
||||
private int id = 0;
|
||||
private double out = 0;
|
||||
|
||||
public void clear() {
|
||||
out = 0;
|
||||
id = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getBack(double out, int id, long eventId) {
|
||||
if (out > this.out) {
|
||||
this.out = out;
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getBackMatrix(Matrix matrix, long eventId) {
|
||||
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
@ -0,0 +1,117 @@
|
||||
package org.wlld.imageRecognition.segmentation;
|
||||
|
||||
import org.wlld.config.RZ;
|
||||
import org.wlld.function.Sigmod;
|
||||
import org.wlld.function.Tanh;
|
||||
import org.wlld.imageRecognition.modelEntity.RgbBack;
|
||||
import org.wlld.nerveCenter.NerveManager;
|
||||
import org.wlld.nerveEntity.SensoryNerve;
|
||||
|
||||
import java.awt.image.Kernel;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @param
|
||||
* @DATA
|
||||
* @Author LiDaPeng
|
||||
* @Description
|
||||
*/
|
||||
public class KNerveManger {
|
||||
private Map<Integer, List<double[]>> featureMap = new HashMap<>();
|
||||
private int sensoryNerveNub;//输出神经元个数
|
||||
private int speciesNub;//种类数
|
||||
private NerveManager nerveManager;
|
||||
private int times;
|
||||
private RgbBack rgbBack = new RgbBack();
|
||||
|
||||
public KNerveManger(int sensoryNerveNub, int speciesNub, int times) throws Exception {
|
||||
this.sensoryNerveNub = sensoryNerveNub;
|
||||
this.speciesNub = speciesNub;
|
||||
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);
|
||||
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);
|
||||
}
|
||||
return tagging;
|
||||
}
|
||||
|
||||
public void look(List<double[]> data) throws Exception {
|
||||
int size = data.size();
|
||||
Map<Integer, Integer> map = new HashMap<>();
|
||||
for (int i = 0; i < size; i++) {
|
||||
rgbBack.clear();
|
||||
post(data.get(i), null, false);
|
||||
int type = rgbBack.getId();
|
||||
if (map.containsKey(type)) {
|
||||
map.put(type, map.get(type) + 1);
|
||||
} else {
|
||||
map.put(type, 1);
|
||||
}
|
||||
}
|
||||
double max = 0;
|
||||
int type = 0;
|
||||
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
|
||||
int nub = entry.getValue();
|
||||
if (nub > max) {
|
||||
max = nub;
|
||||
type = entry.getKey();
|
||||
}
|
||||
}
|
||||
double point = max / size;
|
||||
System.out.println("类型是:" + type + ",总票数:" + size + ",得票率:" + point);
|
||||
System.out.println("=================================完成");
|
||||
}
|
||||
|
||||
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 (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 {
|
||||
List<SensoryNerve> sensoryNerveList = nerveManager.getSensoryNerves();
|
||||
int size = sensoryNerveList.size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
sensoryNerveList.get(i).postMessage(1, data[i], isStudy, tagging, rgbBack);
|
||||
}
|
||||
}
|
||||
|
||||
public void setFeature(int type, List<double[]> feature) {
|
||||
if (type > 0) {
|
||||
if (featureMap.containsKey(type)) {
|
||||
featureMap.get(type).addAll(feature);
|
||||
} else {
|
||||
featureMap.put(type, feature);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
package org.wlld.imageRecognition.segmentation;
|
||||
|
||||
import org.wlld.MatrixTools.Matrix;
|
||||
import org.wlld.imageRecognition.MeanClustering;
|
||||
import org.wlld.imageRecognition.RGBNorm;
|
||||
import org.wlld.imageRecognition.TempleConfig;
|
||||
import org.wlld.imageRecognition.ThreeChannelMatrix;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @param
|
||||
* @DATA
|
||||
* @Author LiDaPeng
|
||||
* @Description
|
||||
*/
|
||||
public class WFilter {
|
||||
private List<RGBNorm> rgbNorms;
|
||||
|
||||
public void filter(ThreeChannelMatrix threeChannelMatrix, TempleConfig templeConfig,
|
||||
int speciesQuantity) throws Exception {
|
||||
MeanClustering meanClustering = new MeanClustering(speciesQuantity, templeConfig);
|
||||
Matrix matrixR = threeChannelMatrix.getMatrixR();
|
||||
Matrix matrixG = threeChannelMatrix.getMatrixG();
|
||||
Matrix matrixB = threeChannelMatrix.getMatrixB();
|
||||
int x = matrixR.getX();
|
||||
int y = matrixR.getY();
|
||||
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(true);
|
||||
rgbNorms = meanClustering.getMatrices();
|
||||
|
||||
}
|
||||
|
||||
private void getDist() {
|
||||
double min = -1;
|
||||
for (RGBNorm rgbNorm : rgbNorms) {
|
||||
double[] rgb = rgbNorm.getRgb();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private double dist(double[] a, double[] b) {
|
||||
double sigma = 0;
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
double sub = Math.pow(a[i] - b[i], 2);
|
||||
sigma = sigma + sub;
|
||||
}
|
||||
return sigma / a.length;
|
||||
}
|
||||
}
|
@ -0,0 +1,101 @@
|
||||
package coverTest;
|
||||
|
||||
import coverTest.regionCut.RegionCut;
|
||||
import coverTest.regionCut.RegionFeature;
|
||||
import org.wlld.MatrixTools.Matrix;
|
||||
import org.wlld.imageRecognition.Convolution;
|
||||
import org.wlld.imageRecognition.Picture;
|
||||
import org.wlld.imageRecognition.ThreeChannelMatrix;
|
||||
import org.wlld.imageRecognition.segmentation.RgbRegression;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @param
|
||||
* @DATA
|
||||
* @Author LiDaPeng
|
||||
* @Description数据观察
|
||||
*/
|
||||
public class DataObservation {
|
||||
private static Convolution convolution = new Convolution();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
public static void observation(String url, int xp, int yp, int size) throws Exception {
|
||||
Picture picture = new Picture();
|
||||
ThreeChannelMatrix threeChannelMatrix = picture.getThreeMatrix(url);
|
||||
ThreeChannelMatrix myThreeChannelMatrix = convolution.getRegionMatrix(threeChannelMatrix, xp, yp, size, size);
|
||||
//右
|
||||
ThreeChannelMatrix threeChannelMatrix2 = convolution.getRegionMatrix(threeChannelMatrix, xp, yp + size, size, size);
|
||||
getDist(myThreeChannelMatrix, threeChannelMatrix2, "右");
|
||||
//左
|
||||
ThreeChannelMatrix threeChannelMatrix3 = convolution.getRegionMatrix(threeChannelMatrix, xp, yp - size, size, size);
|
||||
getDist(myThreeChannelMatrix, threeChannelMatrix3, "左");
|
||||
//上
|
||||
ThreeChannelMatrix threeChannelMatrix4 = convolution.getRegionMatrix(threeChannelMatrix, xp - size, yp, size, size);
|
||||
getDist(myThreeChannelMatrix, threeChannelMatrix4, "上");
|
||||
//下
|
||||
ThreeChannelMatrix threeChannelMatrix5 = convolution.getRegionMatrix(threeChannelMatrix, xp + size, yp, size, size);
|
||||
getDist(myThreeChannelMatrix, threeChannelMatrix5, "下");
|
||||
//左上
|
||||
ThreeChannelMatrix threeChannelMatrix6 = convolution.getRegionMatrix(threeChannelMatrix, xp - size, yp - size, size, size);
|
||||
getDist(myThreeChannelMatrix, threeChannelMatrix6, "左上");
|
||||
//左下
|
||||
ThreeChannelMatrix threeChannelMatrix7 = convolution.getRegionMatrix(threeChannelMatrix, xp + size, yp - size, size, size);
|
||||
getDist(myThreeChannelMatrix, threeChannelMatrix7, "左下");
|
||||
//右上
|
||||
ThreeChannelMatrix threeChannelMatrix8 = convolution.getRegionMatrix(threeChannelMatrix, xp - size, yp + size, size, size);
|
||||
getDist(myThreeChannelMatrix, threeChannelMatrix8, "右上");
|
||||
//右下
|
||||
ThreeChannelMatrix threeChannelMatrix9 = convolution.getRegionMatrix(threeChannelMatrix, xp + size, yp + size, size, size);
|
||||
getDist(myThreeChannelMatrix, threeChannelMatrix9, "右下");
|
||||
//getDist("/Users/lidapeng/Desktop/test/testOne/a1.jpg", 468, 713, rgbRegression, "测");
|
||||
|
||||
}
|
||||
|
||||
private static void getDist(ThreeChannelMatrix threeChannelMatrix1, ThreeChannelMatrix threeChannelMatrix2, String name) throws Exception {
|
||||
Matrix matrixR1 = threeChannelMatrix1.getMatrixR();
|
||||
Matrix matrixG1 = threeChannelMatrix1.getMatrixG();
|
||||
Matrix matrixB1 = threeChannelMatrix1.getMatrixB();
|
||||
Matrix matrixR2 = threeChannelMatrix2.getMatrixR();
|
||||
Matrix matrixG2 = threeChannelMatrix2.getMatrixG();
|
||||
Matrix matrixB2 = threeChannelMatrix2.getMatrixB();
|
||||
int x = matrixR1.getX();
|
||||
int y = matrixR1.getY();
|
||||
int 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++) {
|
||||
double subR = Math.pow(matrixR1.getNumber(i, j) - matrixR2.getNumber(i, j), 2);
|
||||
double subG = Math.pow(matrixG1.getNumber(i, j) - matrixG2.getNumber(i, j), 2);
|
||||
double subB = Math.pow(matrixB1.getNumber(i, j) - matrixB2.getNumber(i, j), 2);
|
||||
sigmaR = subR + sigmaR;
|
||||
sigmaG = subG + sigmaG;
|
||||
sigmaB = subB + sigmaB;
|
||||
}
|
||||
}
|
||||
sigmaR = sigmaR / nub;
|
||||
sigmaG = sigmaG / nub;
|
||||
sigmaB = sigmaB / nub;
|
||||
double sigma = sigmaR + sigmaG + sigmaB;
|
||||
System.out.println(name + ":" + sigma);
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package coverTest;
|
||||
|
||||
/**
|
||||
* @param
|
||||
* @DATA
|
||||
* @Author LiDaPeng
|
||||
* @Description
|
||||
*/
|
||||
public class RGBBody {
|
||||
private double r;
|
||||
private double g;
|
||||
private double b;
|
||||
private double rgb;
|
||||
|
||||
public double getR() {
|
||||
return r;
|
||||
}
|
||||
|
||||
public void setR(double r) {
|
||||
this.r = r;
|
||||
}
|
||||
|
||||
public double getG() {
|
||||
return g;
|
||||
}
|
||||
|
||||
public void setG(double g) {
|
||||
this.g = g;
|
||||
}
|
||||
|
||||
public double getB() {
|
||||
return b;
|
||||
}
|
||||
|
||||
public void setB(double b) {
|
||||
this.b = b;
|
||||
}
|
||||
|
||||
public double getRgb() {
|
||||
return rgb;
|
||||
}
|
||||
|
||||
public void setRgb(double rgb) {
|
||||
this.rgb = rgb;
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,15 @@
|
||||
package coverTest.regionCut;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @param
|
||||
* @DATA
|
||||
* @Author LiDaPeng
|
||||
* @Description
|
||||
*/
|
||||
public class RegionCutBody {
|
||||
private List<Integer> pixels = new ArrayList<>();
|
||||
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
package coverTest.regionCut;
|
||||
|
||||
import org.wlld.MatrixTools.Matrix;
|
||||
import org.wlld.MatrixTools.MatrixOperation;
|
||||
import org.wlld.config.Kernel;
|
||||
import org.wlld.imageRecognition.ThreeChannelMatrix;
|
||||
import org.wlld.imageRecognition.segmentation.RgbRegression;
|
||||
import org.wlld.tools.Frequency;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @param
|
||||
* @DATA
|
||||
* @Author LiDaPeng
|
||||
* @Description 利用边缘算子提取特征
|
||||
*/
|
||||
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;
|
||||
private RgbRegression rgbRegression = new RgbRegression(25);
|
||||
|
||||
public RegionFeature(ThreeChannelMatrix threeChannelMatrix, int fatherX, int fatherY) {
|
||||
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 {
|
||||
int x = matrix.getX();
|
||||
int y = matrix.getY();
|
||||
double[] data = new double[x * y];
|
||||
for (int i = 0; i < x; i++) {
|
||||
for (int j = 0; j < y; j++) {
|
||||
int index = i * y + j;
|
||||
data[index] = matrix.getNumber(i, j);
|
||||
}
|
||||
}
|
||||
//System.out.println(Arrays.toString(data));
|
||||
return variance(data);
|
||||
}
|
||||
|
||||
public void start() throws Exception {
|
||||
int x = matrix.getX();
|
||||
int y = matrix.getY();
|
||||
int size = kernel.getX();
|
||||
for (int i = 0; i <= x - size; i += 2) {
|
||||
int xSize = fatherX + i;
|
||||
System.out.println("==================================" + xSize);
|
||||
for (int j = 0; j <= y - size; j += 2) {
|
||||
// double conNub = Math.abs(convolution(i, j));//卷积值
|
||||
double var = getMatrixVar(matrix.getSonOfMatrix(i, j, size, size));
|
||||
int ySize = fatherY + j;
|
||||
System.out.println("x:" + xSize + ",y:" + ySize + ",var:" + var);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private double convolution(int x, int y) throws Exception {//计算卷积
|
||||
double allNub = 0;
|
||||
int xr;
|
||||
int yr;
|
||||
int kxMax = kernel.getX();
|
||||
int kyMax = kernel.getY();
|
||||
for (int i = 0; i < kxMax; i++) {
|
||||
xr = i + x;
|
||||
for (int j = 0; j < kyMax; j++) {
|
||||
yr = j + y;
|
||||
allNub = matrix.getNumber(xr, yr) * kernel.getNumber(i, j) + allNub;
|
||||
}
|
||||
}
|
||||
return allNub;
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue