添加多种类干食切图

pull/59/head
lidapeng 4 years ago
parent 690dae0028
commit 2254885c57

@ -0,0 +1,147 @@
package org.wlld.imageRecognition;
import org.wlld.MatrixTools.Matrix;
import org.wlld.imageRecognition.border.GMClustering;
import org.wlld.imageRecognition.modelEntity.GMBody;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @param
* @DATA
* @Author LiDaPeng
* @Description
*/
public class CutFood {
private TempleConfig templeConfig;
private Map<Integer, GMClustering> meanMap = new HashMap<>();
private Matrix regionMap;
public CutFood(TempleConfig templeConfig) {
this.templeConfig = templeConfig;
}
private void mean(ThreeChannelMatrix threeChannelMatrix, GMClustering mean) throws Exception {
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[] rgb = new double[]{matrixR.getNumber(i, j) / 255, matrixG.getNumber(i, j) / 255
, matrixB.getNumber(i, j) / 255};
mean.setColor(rgb);
}
}
mean.start(true);
}
private double getAvg(Matrix matrix) throws Exception {
double sigma = 0;
int x = matrix.getX();
int y = matrix.getY();
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
sigma = sigma + matrix.getNumber(i, j);
}
}
return sigma / (x * y);
}
public void createRegion(ThreeChannelMatrix threeChannelMatrix) throws Exception {
int size = templeConfig.getFood().getRegionSize();
Matrix matrixR = threeChannelMatrix.getMatrixR();
Matrix matrixG = threeChannelMatrix.getMatrixG();
Matrix matrixB = threeChannelMatrix.getMatrixB();
int x = matrixR.getX();
int y = matrixR.getY();
double s = Math.pow(size, 2);
regionMap = new Matrix(x / size, y / size);
for (int i = 0; i <= x - size; i += size) {
for (int j = 0; j <= y - size; j += size) {
double r = getAvg(matrixR.getSonOfMatrix(i, j, size, size));
double g = getAvg(matrixG.getSonOfMatrix(i, j, size, size));
double b = getAvg(matrixB.getSonOfMatrix(i, j, size, size));
double[] rgb = new double[]{r / 255, g / 255, b / 255};
int index = getType(rgb);//该区域所属类别
regionMap.setNub(i / size, j / size, index);
}
}
int xr = regionMap.getX();
int yr = regionMap.getY();
List<GMBody> gmBodies = new ArrayList<>();
for (int i = 0; i < xr; i++) {
for (int j = 0; j < yr; j++) {
int type = (int) regionMap.getNumber(i, j);
if (!insertBodies(gmBodies, i, j, type)) {//需要创建一个新的
gmBodies.add(new GMBody(type, i, j));
}
}
}
List<GMBody> gmBodies2 = new ArrayList<>();
for (int i = 0; i < gmBodies.size(); i++) {//过滤侯选区
GMBody gmBody = gmBodies.get(i);
double regionSize = gmBody.getPixelNub() * s;
int type = gmBody.getType();
if (type != 1) {//背景直接过滤
int oneSize = meanMap.get(type).getRegionSize();
if (regionSize > oneSize * 0.8) {
gmBodies2.add(gmBody);
}
}
}
for (GMBody gmBody : gmBodies2) {
int type = gmBody.getType();
double regionSize = gmBody.getPixelNub() * s;
int oneSize = meanMap.get(type).getRegionSize();
double nub = regionSize / (double) oneSize;
System.out.println("type==" + type + ",nub==" + nub + ",onSize==" + oneSize + ",gmNub=="
+ gmBody.getPixelNub());
}
}
private boolean insertBodies(List<GMBody> gmBodies, int x, int y, int type) {
boolean isInsert = false;
for (GMBody gmBody : gmBodies) {
if (gmBody.insertRgb(x, y, type)) {
isInsert = true;
break;
}
}
return isInsert;
}
private int getType(double[] rgb) throws Exception {
int index = 0;
double max = 0;
for (Map.Entry<Integer, GMClustering> entry : meanMap.entrySet()) {
GMClustering gmClustering = entry.getValue();
double probability = gmClustering.getProbabilityDensity(rgb);
if (probability > max) {
max = probability;
index = entry.getKey();
}
}
if (max < 2) {
index = 1;
}
return index;
}
public void study(int type, ThreeChannelMatrix threeChannelMatrix) throws Exception {
Matrix matrixR = threeChannelMatrix.getMatrixR();
int x = matrixR.getX();
int y = matrixR.getY();
GMClustering mean = new GMClustering(templeConfig.getFeatureNub(), templeConfig);
mean.setRegionSize(x * y);
meanMap.put(type, mean);
mean(threeChannelMatrix, mean);
//记录非背景的单物体面积
}
}

@ -131,10 +131,9 @@ public class Operation {//进行计算
int minY = regionBody.getMinY();
int maxX = regionBody.getMaxX();
int maxY = regionBody.getMaxY();
System.out.println("异常minX==" + minX + ",minY==" + minY + ",maxX==" + maxX + ",maxY==" + maxY + ",tag==" + tag
+ "url==" + url);
System.out.println("异常minX==" + minX + ",minY==" + minY + ",maxX==" + maxX + ",maxY==" + maxY + ",tag==" + tag);
}
throw new Exception("Parameter exception region size==" + regionBodies.size());
throw new Exception("Parameter exception region size==" + regionBodies.size() + ",url" + url);
}
}

@ -202,7 +202,7 @@ public class RGBNorm {
}
}
public double getGMProbability(double[] feature) throws Exception {//计算正态分布概率
public double getGMProbability(double[] feature) throws Exception {//计算正态分布概率密度
double zSigma = 0;
int size = feature.length;
for (int i = 0; i < size; i++) {

@ -4,7 +4,6 @@ import org.wlld.imageRecognition.MeanClustering;
import org.wlld.imageRecognition.RGBNorm;
import org.wlld.imageRecognition.TempleConfig;
import java.util.Arrays;
/**
* @param
@ -13,11 +12,28 @@ import java.util.Arrays;
* @Description
*/
public class GMClustering extends MeanClustering {
private int regionSize;//单区域面积
public int getRegionSize() {
return regionSize;
}
public void setRegionSize(int regionSize) {
this.regionSize = regionSize;
}
public GMClustering(int speciesQuantity, TempleConfig templeConfig) throws Exception {
super(speciesQuantity, templeConfig);
}
public double getProbabilityDensity(double[] feature) throws Exception {//获取总概率密度
double sigma = 0;
for (RGBNorm rgbNorm : matrices) {
sigma = sigma + rgbNorm.getGMProbability(feature);
}
return sigma;
}
@Override
public void start(boolean isRegression) throws Exception {
super.start(isRegression);

@ -0,0 +1,55 @@
package org.wlld.imageRecognition.modelEntity;
import java.util.ArrayList;
import java.util.List;
/**
* @param
* @DATA
* @Author LiDaPeng
* @Description
*/
public class GMBody {
private int type;//类别
private List<Integer> pixels = new ArrayList<>();
private int pixelNub = 0;
public GMBody(int type, int x, int y) {
this.type = type;
pixelNub++;
pixels.add((x << 12) | y);
}
public int getPixelNub() {
return pixelNub;
}
public int getType() {
return type;
}
private boolean isAdjacent(int x, int y, int i, int j) {
boolean adjacent = false;
if (Math.abs(x - i) == 1 || Math.abs(y - j) == 1) {
adjacent = true;
}
return adjacent;
}
public boolean insertRgb(int x, int y, int type) {
boolean isRight = false;
if (this.type == type) {
for (int pixel : pixels) {
int i = (pixel >> 12) & 0xfff;
int j = pixel & 0xfff;
if (isAdjacent(x, y, i, j)) {//相邻
isRight = true;
pixelNub++;
pixels.add((x << 12) | y);
break;
}
}
}
return isRight;
}
}

@ -75,7 +75,7 @@ public class Watershed {
private boolean isTray(int x, int y) throws Exception {
boolean isTray = false;
if (trayBody != null && trayBody.size() > 0) {
if (trayBody.size() > 0) {
double[] rgb = new double[]{matrixR.getNumber(x, y) / 255, matrixG.getNumber(x, y) / 255,
matrixB.getNumber(x, y) / 255};
for (RgbRegression rgbRegression : trayBody) {
@ -279,14 +279,16 @@ public class Watershed {
regionBodies.add(regionBody);
}
}
for (RegionBody regionBody : regionBodies) {
regionBodies = iou(regionBodies);
for (int i = 0; i < regionBodies.size(); i++) {
RegionBody regionBody = regionBodies.get(i);
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);
System.out.println("minX==" + minX + ",maxX==" + maxX + ",minY==" + minY + ",maxY==" + maxY);
}
return iou(regionBodies);
return regionBodies;
}
private List<RegionBody> iou(List<RegionBody> regionBodies) {
@ -361,7 +363,7 @@ public class Watershed {
double left = this.width / edgeSize;//左边缘界限
double bottom = this.height - top;//下边缘界限
double right = this.width - left;//右边缘界限
isCenter = maxX > top && maxY > left && minX < bottom && minY < right;
isCenter = minX > top && minY > left && minX < bottom && minY < right;
}
if (width >= specification.getMinWidth() && height >= specification.getMinHeight()
&& width <= specification.getMaxWidth() && height <= specification.getMaxHeight()
@ -494,7 +496,7 @@ public class Watershed {
int minIdx = 0;
for (int i = 0; i < array.length; i++) {
double nub = array[i];
if (nub > -1 && nub < mySelf) {
if (nub > -1 && nub < mySelf && nub < 280) {
minIdx = minIdx | (1 << i);
}
}

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save