parent
1a64279d4c
commit
b955bb673f
@ -1,16 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
|
||||
<output url="file://$MODULE_DIR$/target/classes" />
|
||||
<output-test url="file://$MODULE_DIR$/target/test-classes" />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: com.alibaba:fastjson:1.2.51" level="project" />
|
||||
</component>
|
||||
</module>
|
@ -0,0 +1,96 @@
|
||||
package org.wlld.imageRecognition;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
//K均值聚类
|
||||
public class MeanClustering {
|
||||
private List<double[]> matrixList = new ArrayList<>();//聚类集合
|
||||
private int length;//向量长度(模型需要返回)
|
||||
private int speciesQuantity;//种类数量(模型需要返回)
|
||||
private List<RGBNorm> matrices = new ArrayList<>();//均值K模型(模型需要返回)
|
||||
|
||||
public List<RGBNorm> getMatrices() {
|
||||
return matrices;
|
||||
}
|
||||
|
||||
public MeanClustering(int speciesQuantity) {
|
||||
this.speciesQuantity = speciesQuantity;//聚类的数量
|
||||
}
|
||||
|
||||
public void setColor(double[] color) throws Exception {
|
||||
if (matrixList.size() == 0) {
|
||||
matrixList.add(color);
|
||||
length = color.length;
|
||||
} else {
|
||||
if (length == color.length) {
|
||||
matrixList.add(color);
|
||||
} else {
|
||||
throw new Exception("vector length is different");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void averageMatrix() {
|
||||
for (double[] rgb : matrixList) {//遍历当前集合
|
||||
double min = -1;
|
||||
int id = 0;
|
||||
for (int i = 0; i < speciesQuantity; i++) {
|
||||
RGBNorm rgbNorm = matrices.get(i);
|
||||
double dist = rgbNorm.getEDist(rgb);
|
||||
if (min == -1 || dist < min) {
|
||||
min = dist;
|
||||
id = i;
|
||||
}
|
||||
}
|
||||
//进簇
|
||||
RGBNorm rgbNorm = matrices.get(id);
|
||||
rgbNorm.setColor(rgb);
|
||||
}
|
||||
//重新计算均值
|
||||
for (RGBNorm rgbNorm : matrices) {
|
||||
rgbNorm.norm();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isNext() {
|
||||
boolean isNext = false;
|
||||
for (RGBNorm rgbNorm : matrices) {
|
||||
isNext = rgbNorm.compare();
|
||||
if (isNext) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return isNext;
|
||||
}
|
||||
|
||||
private void clear() {
|
||||
for (RGBNorm rgbNorm : matrices) {
|
||||
rgbNorm.clear();
|
||||
}
|
||||
}
|
||||
|
||||
public void start() throws Exception {//开始聚类
|
||||
if (matrixList.size() > 1) {
|
||||
Random random = new Random();
|
||||
for (int i = 0; i < speciesQuantity; i++) {//初始化均值向量
|
||||
int index = random.nextInt(matrixList.size());
|
||||
double[] rgb = matrixList.get(index);
|
||||
RGBNorm rgbNorm = new RGBNorm(rgb);
|
||||
//要进行深度克隆
|
||||
matrices.add(rgbNorm);
|
||||
}
|
||||
//进行两者的比较
|
||||
boolean isNext;
|
||||
do {
|
||||
averageMatrix();
|
||||
isNext = isNext();
|
||||
if (isNext) {
|
||||
clear();
|
||||
}
|
||||
}
|
||||
while (isNext);
|
||||
} else {
|
||||
throw new Exception("matrixList number less than 2");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
package org.wlld.imageRecognition;
|
||||
|
||||
import org.wlld.tools.ArithUtil;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class RGBNorm {
|
||||
private double[] rgbAll = new double[3];
|
||||
private double norm;
|
||||
private int nub;
|
||||
private double[] rgb = new double[3];
|
||||
private double[] rgbUp;
|
||||
|
||||
RGBNorm(double[] rgb) {
|
||||
this.rgbUp = rgb;
|
||||
}
|
||||
|
||||
public void syn() {
|
||||
rgbUp = rgb;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
rgbAll = new double[3];
|
||||
nub = 0;
|
||||
for (int i = 0; i < rgb.length; i++) {
|
||||
rgbUp[i] = rgb[i];
|
||||
}
|
||||
//System.out.println("clear==" + Arrays.toString(rgbUp));
|
||||
}
|
||||
|
||||
public int getNub() {
|
||||
return nub;
|
||||
}
|
||||
|
||||
public boolean compare() {
|
||||
boolean isNext = false;
|
||||
for (int i = 0; i < rgb.length; i++) {
|
||||
if (rgb[i] != rgbUp[i]) {
|
||||
isNext = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return isNext;
|
||||
}
|
||||
|
||||
public double getEDist(double[] x) {
|
||||
double[] y = new double[x.length];
|
||||
for (int i = 0; i < y.length; i++) {
|
||||
y[i] = x[i] - rgbUp[i];
|
||||
}
|
||||
double sigma = 0;
|
||||
for (int i = 0; i < y.length; i++) {
|
||||
sigma = sigma + Math.pow(y[i], 2);
|
||||
}
|
||||
return Math.sqrt(sigma);
|
||||
}
|
||||
|
||||
public void setColor(double[] rgb) {
|
||||
for (int i = 0; i < rgb.length; i++) {
|
||||
rgbAll[i] = rgbAll[i] + rgb[i];
|
||||
}
|
||||
nub++;
|
||||
}
|
||||
|
||||
public void norm() {//范长计算
|
||||
double sigma = 0;
|
||||
if (nub > 0) {
|
||||
for (int i = 0; i < rgb.length; i++) {
|
||||
double rgbc = ArithUtil.div(rgbAll[i], nub);
|
||||
rgb[i] = rgbc;
|
||||
sigma = sigma + Math.pow(rgbc, 2);
|
||||
}
|
||||
norm = Math.sqrt(sigma);
|
||||
}
|
||||
}
|
||||
|
||||
public double getNorm() {
|
||||
return norm;
|
||||
}
|
||||
|
||||
public double[] getRgb() {
|
||||
return rgb;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue