分水岭修改

pull/25/head
thenk008 5 years ago
parent a738814f23
commit e0c541467d

@ -9,7 +9,9 @@ public class Kernel {
private static final String All_Number = "[1,-2,1]#[-2,4,-2]#[1,-2,1]#";//角卷积
private static final String All_Number2 = "[-1,0,-1]#[0,4,0]#[-1,0,-1]#";
public static final int Region_Nub = 30;//一张图分为多少份
public static final double th = 0.7;//分水岭灰度阈值
public static final int Region_Dif = 3;//区域内的最大间隔
public static final double Region_Th = 0.2;//区域分割阈值
public static final double th = 0.5;//分水岭灰度阈值
public static final double rgbN = 441.6729559300637;//RGB范数归一化最大值
public static Matrix Vertical;
public static Matrix Horizontal;

@ -14,42 +14,23 @@ public class RegionBody {
private int minY = -1;
private int maxX;
private int maxY;
private List<Integer> pointList = new ArrayList<>();
private Matrix regionMap;//分区图
public List<Integer> getPointList() {
return pointList;
}
public RegionBody(Matrix regionMap) {
this.regionMap = regionMap;
public void setX(int x) {
if (x < minX || minX == -1) {
minX = x;
}
public void merge(int type, RegionBody regionBody) throws Exception {//区域合并
List<Integer> points = regionBody.getPointList();
for (int pixel : points) {
int x = pixel >> 12;
int y = pixel & 0xfff;
setPoint(x, y, type);
if (x > maxX) {
maxX = x;
}
}
public void setPoint(int x, int y, int type) throws Exception {
if (x < minX || minX == -1) {
minX = x;
}
public void setY(int y) {
if (y < minY || minY == -1) {
minY = y;
}
if (x > maxX) {
maxX = x;
}
if (y > maxY) {
maxY = y;
}
int pixel = x << 12 | y;
pointList.add(pixel);
regionMap.setNub(x, y, type);
}
public int getMinX() {

@ -2,6 +2,7 @@ package org.wlld.imageRecognition.segmentation;
import org.wlld.MatrixTools.Matrix;
import org.wlld.config.Kernel;
import org.wlld.tools.ArithUtil;
import java.util.*;
@ -13,24 +14,24 @@ import java.util.*;
public class Watershed {
private Matrix matrix;//RGB范数图像
private Matrix rainfallMap;//降雨图
private Matrix rainDensityMap;//降雨密度图
private Matrix regionMap;//分区图
private int xSize;//单元高度
private int ySize;//单元宽度
private double th = Kernel.th;//灰度阈值
private double regionTh = Kernel.Region_Th;
private int regionSize = Kernel.Region_Dif;
private Map<Integer, RegionBody> regionBodyMap = new HashMap<>();
private int regionNub = Kernel.Region_Nub;//一张图分多少份
private int xMax;
private int yMax;
private int id = 1;
private int rxMax;
private int ryMax;
private int id = 0;
public Watershed(Matrix matrix) throws Exception {
if (matrix != null) {
this.matrix = matrix;
rainfallMap = new Matrix(matrix.getX(), matrix.getY());
rainDensityMap = new Matrix(matrix.getX() / regionNub, matrix.getY() / regionNub);
regionMap = new Matrix(matrix.getX() / regionNub, matrix.getY() / regionNub);
xMax = rainfallMap.getX() - 1;
yMax = rainfallMap.getY() - 1;
@ -194,67 +195,88 @@ public class Watershed {
ySize = y / regionNub;
System.out.println("xSize==" + xSize + ",ySize==" + ySize);
sigmaPixel();
// int nub = 0;
// System.out.println("region size==" + regionBodyMap.size());
// for (Map.Entry<Integer, RegionBody> entry : regionBodyMap.entrySet()) {
// RegionBody regionBody = entry.getValue();
// int maxX = regionBody.getMaxX();
// int maxY = regionBody.getMaxY();
// int minX = regionBody.getMinX();
// int minY = regionBody.getMinY();
// System.out.println("minX==" + minX + ",minY==" + minY + ",maxX==" + maxX + ",maxY==" + maxY);
// }
// System.out.println("nub===" + nub);
}
private void setRegion(int x, int y, int type) throws Exception {
int i = 0;
int j = 0;
for (int t = 0; t < 8; t++) {
switch (t) {
case 0:
i = x - 1;
j = y;
break;
case 1:
i = x - 1;
j = y - 1;
break;
case 2:
i = x - 1;
j = y + 1;
break;
case 3:
i = x;
j = y - 1;
break;
case 4:
i = x;
j = y + 1;
break;
case 5:
i = x + 1;
j = y - 1;
break;
case 6:
i = x + 1;
j = y;
break;
case 7:
i = x + 1;
j = y + 1;
break;
private void setType(int type, int x, int y) throws Exception {
for (int i = x; i < x + 3; i++) {
for (int j = y; j < y + 3; j++) {
if (regionMap.getNumber(i, j) != 0) {
regionMap.setNub(i, j, type);
}
if (i > -1 && j > -1 && i <= rxMax && j <= ryMax && regionMap.getNumber(i, j) == 1) {
regionBodyMap.get(type).setPoint(i, j, type);
}
}
}
private void getPosition() throws Exception {
int x = rainDensityMap.getX();
int y = rainDensityMap.getY();
private void mergeRegion() throws Exception {//区域合并
int x = regionMap.getX() - 2;
int y = regionMap.getY() - 2;
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
double nub = rainDensityMap.getNumber(i, j);
if (nub == 1) {
Matrix matrix = regionMap.getSonOfMatrix(i, j, regionSize, regionSize);
int type = 0;
int state = 0;
for (int k = 0; k < matrix.getX(); k++) {
for (int l = 0; l < matrix.getY(); l++) {
int nub = (int) matrix.getNumber(k, l);
if (nub > 1 && type == 0) {//存在大于1的数
type = nub;
state = state | (1 << 1);
} else if (nub == 1) {
state = state | 1;
}
}
}
if ((state & 2) != 0) {//存在大于1的数
setType(type, i, j);
} else if ((state & 1) != 0) {//不存在大于1的数但是存在1,生成新的ID
id++;
RegionBody regionBody = new RegionBody(regionMap);
regionBody.setPoint(x, y, id);
regionBodyMap.put(id, regionBody);
setRegion(i, j, id);
regionBodyMap.put(id, new RegionBody());
setType(id, i, j);
}
}
}
}
private void createRegion() throws Exception {
int x = regionMap.getX();
int y = regionMap.getY();
for (int i = 0; i < x; i++) {
Map<Integer, Integer> map = new HashMap<>();
for (int j = 0; j < y; j++) {
int type = (int) regionMap.getNumber(i, j);
if (type > 1) {
if (map.containsKey(type)) {
map.put(type, map.get(type) + 1);
} else {
map.put(type, 1);
}
}
}
///
for (Map.Entry<Integer, RegionBody> entry : regionBodyMap.entrySet()) {
int type = entry.getKey();
RegionBody regionBody = entry.getValue();
if (map.containsKey(type)) {//如果这个类型存在
int nub = map.get(type);
double point = ArithUtil.div(nub, ryMax);
if (point > regionTh) {
//regionBody.setX();
}
}
}
}
}
private void sigmaPixel() throws Exception {//生成降雨密度图
@ -274,11 +296,12 @@ public class Watershed {
}
double cover = (double) sigma / (double) size;//降雨率产生剧烈波动时则出现坐标
if (cover > th) {//降雨密度图
rainDensityMap.setNub(i / regionNub, j / regionNub, 1);
regionMap.setNub(i / regionNub, j / regionNub, 1);
}
}
}
getPosition();
mergeRegion();
// System.out.println(regionMap.getString());
}
private int getMinIndex(double[] array, double mySelf) {//获取最小值

@ -21,7 +21,7 @@ public class FoodTest {
public static void rain() throws Exception {//降雨
Picture picture = new Picture();
Matrix matrix = picture.getImageMatrixByLocal("D:\\share\\cai/d1.jpg");
Matrix matrix = picture.getImageMatrixByLocal("D:\\share/c.png");
Watershed watershed = new Watershed(matrix);
watershed.rainfall();
}

Loading…
Cancel
Save