增加背景识别

pull/48/head
lidapeng 5 years ago
parent f7d1037ed3
commit 5f3f3986e1

@ -6,7 +6,7 @@
<groupId>com.wlld</groupId>
<artifactId>easyAi</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
<name>easyAi</name>
<!-- FIXME change it to the project's website -->

@ -44,6 +44,16 @@ public class Matrix {
matrix = new double[x][y];
this.x = x;
this.y = y;
setState(x, y);
}
/**
*
*
* @param x
* @param y
*/
private void setState(int x, int y) {
if (x == 1 && y == 1) {
isZero = true;
} else if (x == 1 || y == 1) {
@ -103,6 +113,7 @@ public class Matrix {
matrix = new double[x][y];
this.x = x;
this.y = y;
setState(x, y);
setAll(matr);
}

@ -45,17 +45,21 @@ public class MatrixOperation {
}
//多元线性回归
public static Matrix getLinearRegression(Matrix parameter, Matrix Out) throws Exception {
//将参数矩阵转置
Matrix matrix1 = transPosition(parameter);
//转置的参数矩阵乘以参数矩阵
Matrix matrix2 = mulMatrix(matrix1, parameter);
//求上一步的逆矩阵
Matrix matrix3 = getInverseMatrixs(matrix2);
//逆矩阵乘以转置矩阵
Matrix matrix4 = mulMatrix(matrix3, matrix1);
//最后乘以输出矩阵,生成权重矩阵并返回
return mulMatrix(matrix4, Out);
public static Matrix getLinearRegression(Matrix parameter, Matrix out) throws Exception {
if (parameter.getX() == out.getX() && out.isVector() && !out.isRowVector()) {
//将参数矩阵转置
Matrix matrix1 = transPosition(parameter);
//转置的参数矩阵乘以参数矩阵
Matrix matrix2 = mulMatrix(matrix1, parameter);
//求上一步的逆矩阵
Matrix matrix3 = getInverseMatrixs(matrix2);
//逆矩阵乘以转置矩阵
Matrix matrix4 = mulMatrix(matrix3, matrix1);
//最后乘以输出矩阵,生成权重矩阵并返回
return mulMatrix(matrix4, out);
} else {
throw new Exception("invalid regression matrix");
}
}
//返回两个向量之间的欧氏距离的平方
@ -402,8 +406,10 @@ public class MatrixOperation {
for (int h = 0; h < matrixColumn.getX(); h++) {
double columnNumber = matrixColumn.getNumber(h, 0);
double rowNumber = matrixRow.getNumber(0, h);
double nowNumber = ArithUtil.mul(columnNumber, rowNumber);
columnAllNumber = ArithUtil.add(columnAllNumber, nowNumber);
//double nowNumber = ArithUtil.mul(columnNumber, rowNumber);
//columnAllNumber = ArithUtil.add(columnAllNumber, nowNumber);
double nowNumber = columnNumber * rowNumber;
columnAllNumber = columnAllNumber + nowNumber;
}
matrix.setNub(i, j, columnAllNumber);
}
@ -417,7 +423,7 @@ public class MatrixOperation {
public static void mathMul(Matrix matrix, double nub) throws Exception {//矩阵数乘
for (int i = 0; i < matrix.getX(); i++) {
for (int j = 0; j < matrix.getY(); j++) {
matrix.setNub(i, j, ArithUtil.mul(matrix.getNumber(i, j), nub));
matrix.setNub(i, j, matrix.getNumber(i, j) * nub);
}
}
}

@ -7,7 +7,9 @@ import org.wlld.config.Classifier;
import org.wlld.config.StudyPattern;
import org.wlld.i.OutBack;
import org.wlld.imageRecognition.border.*;
import org.wlld.imageRecognition.modelEntity.TrayBody;
import org.wlld.imageRecognition.segmentation.RegionBody;
import org.wlld.imageRecognition.segmentation.RgbRegression;
import org.wlld.imageRecognition.segmentation.Specifications;
import org.wlld.imageRecognition.segmentation.Watershed;
import org.wlld.nerveCenter.NerveManager;
@ -58,6 +60,30 @@ public class Operation {//进行计算
return sub(matrix1);
}
public void setTray(ThreeChannelMatrix threeChannelMatrix) throws Exception {//设置托盘
Matrix matrixR = threeChannelMatrix.getMatrixR();
Matrix matrixG = threeChannelMatrix.getMatrixG();
Matrix matrixB = threeChannelMatrix.getMatrixB();
Matrix matrixRGB = threeChannelMatrix.getMatrixRGB();
Random random = new Random();
int x = matrixRGB.getX();
int y = matrixRGB.getY();
int size = 70;
RgbRegression rgbRegression = new RgbRegression(size);
for (int i = 0; i < size; i++) {
int xr = random.nextInt(x);
int yr = random.nextInt(y);
double[] rgb = new double[]{matrixR.getNumber(xr, yr), matrixG.getNumber(xr, yr),
matrixB.getNumber(xr, yr)};
rgbRegression.insertRGB(rgb);
}
rgbRegression.regression();
// double[] rgb = new double[]{164, 189, 193};
// 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();
@ -70,7 +96,7 @@ public class Operation {//进行计算
}
public void colorStudy(ThreeChannelMatrix threeChannelMatrix, int tag, List<Specifications> specificationsList) throws Exception {
Watershed watershed = new Watershed(threeChannelMatrix.getMatrixRGB(), specificationsList, templeConfig);
Watershed watershed = new Watershed(threeChannelMatrix, specificationsList, templeConfig);
List<RegionBody> regionBodies = watershed.rainfall();
if (regionBodies.size() == 1) {
RegionBody regionBody = regionBodies.get(0);
@ -139,7 +165,7 @@ public class Operation {//进行计算
}
public List<RegionBody> colorLook(ThreeChannelMatrix threeChannelMatrix, List<Specifications> specificationsList) throws Exception {
Watershed watershed = new Watershed(threeChannelMatrix.getMatrixRGB(), specificationsList, templeConfig);
Watershed watershed = new Watershed(threeChannelMatrix, specificationsList, templeConfig);
List<RegionBody> regionList = watershed.rainfall();
for (RegionBody regionBody : regionList) {
MaxPoint maxPoint = new MaxPoint();

@ -11,10 +11,8 @@ import org.wlld.function.Sigmod;
import org.wlld.function.Tanh;
import org.wlld.i.ActiveFunction;
import org.wlld.imageRecognition.border.*;
import org.wlld.imageRecognition.modelEntity.BoxList;
import org.wlld.imageRecognition.modelEntity.KBorder;
import org.wlld.imageRecognition.modelEntity.LvqModel;
import org.wlld.imageRecognition.modelEntity.MatrixModel;
import org.wlld.imageRecognition.modelEntity.*;
import org.wlld.imageRecognition.segmentation.RgbRegression;
import org.wlld.nerveCenter.NerveManager;
import org.wlld.nerveCenter.Normalization;
import org.wlld.nerveEntity.BodyList;

@ -0,0 +1,37 @@
package org.wlld.imageRecognition.modelEntity;
/**
* @param
* @DATA
* @Author LiDaPeng
* @Description
*/
public class TrayBody {
private double wr;
private double wg;
private double b;
public double getWr() {
return wr;
}
public void setWr(double wr) {
this.wr = wr;
}
public double getWg() {
return wg;
}
public void setWg(double wg) {
this.wg = wg;
}
public double getB() {
return b;
}
public void setB(double b) {
this.b = b;
}
}

@ -0,0 +1,75 @@
package org.wlld.imageRecognition.segmentation;
import org.wlld.MatrixTools.Matrix;
import org.wlld.MatrixTools.MatrixOperation;
/**
* @param
* @DATA
* @Author LiDaPeng
* @Description rgb B = wr *R + wg * G+b
*/
public class RgbRegression {
private double wr;
private double wg;
private double b;
private Matrix RG;//rg矩阵
private Matrix B;//b矩阵
private int xIndex = 0;//记录插入数量
private boolean isRegression = false;//是否进行了回归
public double getWr() {
return wr;
}
public double getWg() {
return wg;
}
public double getB() {
return b;
}
public RgbRegression(int size) {//初始化rgb矩阵
RG = new Matrix(size, 3);
B = new Matrix(size, 1);
}
public void insertRGB(double[] rgb) throws Exception {//rgb插入矩阵
if (rgb.length == 3) {
RG.setNub(xIndex, 0, rgb[0]);
RG.setNub(xIndex, 1, rgb[1]);
RG.setNub(xIndex, 2, 1.0);
B.setNub(xIndex, 0, rgb[2]);
xIndex++;
} else {
throw new Exception("rgb length is not equals three");
}
}
public void regression() throws Exception {//开始进行回归
if (xIndex > 0) {
Matrix ws = MatrixOperation.getLinearRegression(RG, B);
wr = ws.getNumber(0, 0);
wg = ws.getNumber(1, 0);
b = ws.getNumber(2, 0);
isRegression = true;
System.out.println("wr==" + wr + ",wg==" + wg + ",b==" + b);
} else {
throw new Exception("regression matrix size is zero");
}
}
public double getDisError(double[] rgb) throws Exception {//误差距离
if (isRegression) {
if (rgb.length == 3) {
double B = wr * rgb[0] + wg * rgb[1] + b;
return Math.abs(B - rgb[2]);
} else {
throw new Exception("rgb length is not equals three");
}
} else {
throw new Exception("matrix does not regression");
}
}
}

@ -3,6 +3,7 @@ package org.wlld.imageRecognition.segmentation;
import org.wlld.MatrixTools.Matrix;
import org.wlld.config.Kernel;
import org.wlld.imageRecognition.TempleConfig;
import org.wlld.imageRecognition.ThreeChannelMatrix;
import org.wlld.param.Cutting;
import org.wlld.tools.ArithUtil;
@ -15,6 +16,9 @@ import java.util.*;
*/
public class Watershed {
private Matrix matrix;//RGB范数图像
private Matrix matrixR;//r通道
private Matrix matrixG;//g通道
private Matrix matrixB;//b通道
private Matrix rainfallMap;//降雨图
private Matrix regionMap;//分区图
private int xSize;//单元高度
@ -32,29 +36,33 @@ public class Watershed {
private double maxIou;//最大iou
private double rowMark;//行过滤
private double columnMark;//列过滤
private List<Specifications> specifications;
private List<Specifications> specifications;//过滤候选区参数
private List<RgbRegression> trayBody;//托盘参数
public Watershed(Matrix matrix, List<Specifications> specifications, TempleConfig templeConfig) throws Exception {
public Watershed(ThreeChannelMatrix matrix, List<Specifications> specifications, TempleConfig templeConfig) throws Exception {
if (matrix != null && specifications != null && specifications.size() > 0) {
Cutting cutting = templeConfig.getCutting();
th = cutting.getTh();
regionNub = cutting.getRegionNub();
maxRain = cutting.getMaxRain();
this.matrix = matrix;
this.matrix = matrix.getMatrixRGB();
matrixR = matrix.getMatrixR();
matrixG = matrix.getMatrixG();
matrixB = matrix.getMatrixB();
this.specifications = specifications;
this.trayBody = templeConfig.getFood().getTrayBody();
if (templeConfig.getEdge() > 0) {
edgeSize = templeConfig.getEdge();
}
rowMark = templeConfig.getFood().getRowMark();
columnMark = templeConfig.getFood().getColumnMark();
width = matrix.getY();
height = matrix.getX();
xSize = matrix.getX() / regionNub;
ySize = matrix.getY() / regionNub;
width = this.matrix.getY();
height = this.matrix.getX();
xSize = this.matrix.getX() / regionNub;
ySize = this.matrix.getY() / regionNub;
maxIou = templeConfig.getCutting().getMaxIou();
System.out.println("设置最大iou阈值" + maxIou);
// System.out.println("xSize===" + xSize + ",ysize===" + ySize);
rainfallMap = new Matrix(matrix.getX(), matrix.getY());
rainfallMap = new Matrix(this.matrix.getX(), this.matrix.getY());
regionMap = new Matrix(regionNub, regionNub);
xMax = rainfallMap.getX() - 1;
yMax = rainfallMap.getY() - 1;
@ -63,6 +71,20 @@ public class Watershed {
}
}
private boolean isTray(int x, int y) throws Exception {
boolean isTray = false;
double[] rgb = new double[]{matrixR.getNumber(x, y), matrixG.getNumber(x, y),
matrixB.getNumber(x, y)};
for (RgbRegression rgbRegression : trayBody) {
double dist = rgbRegression.getDisError(rgb);
if (dist < 10) {
isTray = true;
break;
}
}
return isTray;
}
private double[] getPixels(int x, int y) throws Exception {
//八方向取值
double left = -1;//左边
@ -94,28 +116,60 @@ public class Watershed {
rightBottom = Kernel.rgbN;
}
if (top == -1 && rainfallMap.getNumber(x - 1, y) == 0) {
top = matrix.getNumber(x - 1, y);
if (isTray(x - 1, y)) {
top = Kernel.rgbN;
} else {
top = matrix.getNumber(x - 1, y);
}
}
if (left == -1 && rainfallMap.getNumber(x, y - 1) == 0) {
left = matrix.getNumber(x, y - 1);
if (isTray(x, y - 1)) {
left = Kernel.rgbN;
} else {
left = matrix.getNumber(x, y - 1);
}
}
if (bottom == -1 && rainfallMap.getNumber(x + 1, y) == 0) {
bottom = matrix.getNumber(x + 1, y);
if (isTray(x + 1, y)) {
bottom = Kernel.rgbN;
} else {
bottom = matrix.getNumber(x + 1, y);
}
}
if (right == -1 && rainfallMap.getNumber(x, y + 1) == 0) {
right = matrix.getNumber(x, y + 1);
if (isTray(x, y + 1)) {
right = Kernel.rgbN;
} else {
right = matrix.getNumber(x, y + 1);
}
}
if (leftTop == -1 && rainfallMap.getNumber(x - 1, y - 1) == 0) {
leftTop = matrix.getNumber(x - 1, y - 1);
if (isTray(x - 1, y - 1)) {
leftTop = Kernel.rgbN;
} else {
leftTop = matrix.getNumber(x - 1, y - 1);
}
}
if (leftBottom == -1 && rainfallMap.getNumber(x + 1, y - 1) == 0) {
leftBottom = matrix.getNumber(x + 1, y - 1);
if (isTray(x + 1, y - 1)) {
leftBottom = Kernel.rgbN;
} else {
leftBottom = matrix.getNumber(x + 1, y - 1);
}
}
if (rightTop == -1 && rainfallMap.getNumber(x - 1, y + 1) == 0) {
rightTop = matrix.getNumber(x - 1, y + 1);
if (isTray(x - 1, y + 1)) {
rightTop = Kernel.rgbN;
} else {
rightTop = matrix.getNumber(x - 1, y + 1);
}
}
if (rightBottom == -1 && rainfallMap.getNumber(x + 1, y + 1) == 0) {
rightBottom = matrix.getNumber(x + 1, y + 1);
if (isTray(x + 1, y + 1)) {
rightBottom = Kernel.rgbN;
} else {
rightBottom = matrix.getNumber(x + 1, y + 1);
}
}
return new double[]{top, left, bottom, right, leftTop, leftBottom, rightBottom, rightTop};
}
@ -220,16 +274,15 @@ public class Watershed {
regionBodies.add(regionBody);
}
}
// for (RegionBody regionBody : regionBodies) {
// int minX = regionBody.getMinX();
// int maxX = regionBody.getMaxX();
// int minY = regionBody.getMinY();
// int maxY = regionBody.getMaxY();
// System.out.println("minX==" + minX + ",minY==" + minY + ",maxX==" + maxX + ",maxY==" + maxY);
// }
return iou(regionBodies);
// return regionBodies;
for (RegionBody regionBody : regionBodies) {
int minX = regionBody.getMinX();
int maxX = regionBody.getMaxX();
int minY = regionBody.getMinY();
int maxY = regionBody.getMaxY();
System.out.println("minX==" + minX + ",minY==" + minY + ",maxX==" + maxX + ",maxY==" + maxY);
}
//return iou(regionBodies);
return regionBodies;
}
private List<RegionBody> iou(List<RegionBody> regionBodies) {
@ -283,7 +336,6 @@ public class Watershed {
double intersectS = ArithUtil.mul(intersectX, intersectY);//相交面积
double mergeS = ArithUtil.sub(s, intersectS);//相并面积
double iou = ArithUtil.div(intersectS, mergeS);
System.out.println("当前iou==" + iou);
if (iou > maxIou) {
if (s1 < s2) {//s1 是i ,大的
list.add(j);
@ -295,7 +347,6 @@ public class Watershed {
}
}
}
System.out.println("要删除的下标:" + list);
List<RegionBody> regionBodies2 = new ArrayList<>();
for (int i = 0; i < regionBodies.size(); i++) {
if (!list.contains(i)) {
@ -408,13 +459,11 @@ public class Watershed {
}
}
double cover = sigma / y;
// System.out.println(cover);
if (cover < rowMark) {
for (int k = 0; k < y; k++) {
regionMap.setNub(i, k, 0.0);
}
}
//System.out.println("ma==" + ma);
}
}
@ -439,9 +488,9 @@ public class Watershed {
}
}
}
pixFilter();
createMerge();
merge();
pixFilter();//痕迹过滤
createMerge();//提取候选区
merge();//合并候选区
//System.out.println(regionMap.getString());
}

@ -1,5 +1,7 @@
package org.wlld.param;
import org.wlld.imageRecognition.segmentation.RgbRegression;
import java.util.ArrayList;
import java.util.List;
@ -12,8 +14,17 @@ 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 double rowMark = 0.12;//行痕迹过滤
private double columnMark = 0.25;//列痕迹过滤
private List<RgbRegression> trayBody = new ArrayList<>();//托盘实体参数
public List<RgbRegression> getTrayBody() {
return trayBody;
}
public void setTrayBody(List<RgbRegression> trayBody) {
this.trayBody = trayBody;
}
public double getRowMark() {
return rowMark;

@ -78,7 +78,7 @@ public class FoodTest {
Cutting cutting = templeConfig.getCutting();
Food food = templeConfig.getFood();
//
cutting.setMaxRain(340);//切割阈值
cutting.setMaxRain(360);//切割阈值
cutting.setTh(0.8);
cutting.setRegionNub(200);
cutting.setMaxIou(1.0);
@ -91,7 +91,8 @@ public class FoodTest {
//菜品识别实体类
food.setShrink(5);//缩紧像素
food.setTimes(2);//聚类数据增强
// food.setTrayTh(50);
food.setRowMark(0.1);//0.12
food.setColumnMark(0.1);//0.25
templeConfig.setClassifier(Classifier.KNN);
templeConfig.init(StudyPattern.Cover_Pattern, true, 400, 400, 3);
if (modelParameter != null) {
@ -106,16 +107,20 @@ public class FoodTest {
Operation operation = new Operation(templeConfig);
List<Specifications> specificationsList = new ArrayList<>();
Specifications specifications = new Specifications();
specifications.setMinWidth(400);
specifications.setMinHeight(400);
specifications.setMinWidth(300);
specifications.setMinHeight(300);
specifications.setMaxWidth(900);
specifications.setMaxHeight(900);
specificationsList.add(specifications);
ThreeChannelMatrix threeChannelMatrix = picture.getThreeMatrix("/Users/lidapeng/Desktop/myDocument/d.jpg");
operation.setTray(threeChannelMatrix);
for (int i = 1; i <= 1; i++) {
ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix("/Users/lidapeng/Desktop/myDocument/c.jpg");
ThreeChannelMatrix threeChannelMatrix1 = picture.getThreeMatrix("/Users/lidapeng/Desktop/test/e1.jpg");
operation.colorStudy(threeChannelMatrix1, 1, specificationsList);
System.out.println("=======================================");
}
// minX==301,minY==430,maxX==854,maxY==920
// minX==497,minY==1090,maxX==994,maxY==1520
//test2(templeConfig);
}

@ -1,5 +1,7 @@
package org.wlld;
import org.wlld.MatrixTools.Matrix;
import org.wlld.MatrixTools.MatrixOperation;
import org.wlld.naturalLanguage.Talk;
import org.wlld.naturalLanguage.TemplateReader;
import org.wlld.naturalLanguage.WordModel;
@ -16,7 +18,74 @@ import java.util.*;
*/
public class LangTest {
public static void main(String[] args) throws Exception {
test();
reTest();
}
public static void reTest() throws Exception {
Matrix parameter = new Matrix(29, 3, "[173,198,1]#" +
"[174,199,1]#" +
"[170,195,1]#" +
"[169,194,1]#" +
"[159,181,1]#" +
"[154,175,1]#" +
"[151,175,1]#" +
"[152,177,1]#" +
"[175,200,1]#" +
"[169,194,1]#" +
"[172,200,1]#" +
"[170,195,1]#" +
"[172,197,1]#" +
"[173,198,1]#" +
"[157,177,1]#" +
"[158,178,1]#" +
"[156,176,1]#" +
"[156,177,1]#" +
"[157,177,1]#" +
"[162,187,1]#" +
"[165,188,1]#" +
"[166,189,1]#" +
"[163,188,1]#" +
"[162,187,1]#" +
"[163,186,1]#" +
"[164,184,1]#" +
"[163,186,1]#" +
"[179,202,1]#" +
"[182,207,1]#");
Matrix out = new Matrix(29, 1, "[203]#" +
"[204]#" +
"[200]#" +
"[199]#" +
"[185]#" +
"[180]#" +
"[179]#" +
"[181]#" +
"[205]#" +
"[198]#" +
"[204]#" +
"[199]#" +
"[202]#" +
"[203]#" +
"[184]#" +
"[185]#" +
"[183]#" +
"[182]#" +
"[184]#" +
"[194]#" +
"[194]#" +
"[195]#" +
"[195]#" +
"[194]#" +
"[192]#" +
"[191]#" +
"[192]#" +
"[210]#" +
"[214]#");
Matrix w = MatrixOperation.getLinearRegression(parameter, out);
System.out.println(w.getString());
double r = 162;
double g = 187;
double b = r * w.getNumber(0, 0) + g * w.getNumber(1, 0) + w.getNumber(2, 0);
System.out.println("r:" + r + ",g:" + g + ",b:" + b);
}
public static void test1() throws Exception {

Loading…
Cancel
Save