parent
d8d6a3197c
commit
f1cf3de0f2
@ -0,0 +1,124 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Palette2">
|
||||
<group name="Swing">
|
||||
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
|
||||
</item>
|
||||
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
|
||||
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
|
||||
<initial-values>
|
||||
<property name="text" value="Button" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="RadioButton" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="CheckBox" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="Label" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||
<preferred-size width="200" height="200" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||
<preferred-size width="200" height="200" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
|
||||
<preferred-size width="-1" height="20" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
|
||||
</item>
|
||||
</group>
|
||||
</component>
|
||||
</project>
|
@ -0,0 +1,131 @@
|
||||
package org.wlld.imageRecognition.border;
|
||||
|
||||
import org.wlld.MatrixTools.Matrix;
|
||||
import org.wlld.MatrixTools.MatrixOperation;
|
||||
import org.wlld.tools.ArithUtil;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author lidapeng
|
||||
* @description //k均值聚类工具
|
||||
* @date 10:14 上午 2020/2/4
|
||||
*/
|
||||
public class KClustering {
|
||||
private List<MatrixBody> matrixList = new ArrayList<>();//聚类集合
|
||||
private int length;//向量长度
|
||||
private int speciesQuantity;//种类数量
|
||||
private Matrix[] matrices = new Matrix[speciesQuantity];//均值K
|
||||
private Map<Integer, List<MatrixBody>> clusterMap = new HashMap<>();//簇
|
||||
|
||||
public Matrix[] getMatrices() {
|
||||
return matrices;
|
||||
}
|
||||
|
||||
public Map<Integer, List<MatrixBody>> getClusterMap() {
|
||||
return clusterMap;
|
||||
}
|
||||
|
||||
public KClustering(int speciesQuantity) {
|
||||
this.speciesQuantity = speciesQuantity;
|
||||
for (int i = 0; i < speciesQuantity; i++) {
|
||||
clusterMap.put(i, new ArrayList<>());
|
||||
}
|
||||
}
|
||||
|
||||
public void setMatrixList(MatrixBody matrixBody) throws Exception {
|
||||
if (matrixBody.getMatrix().isVector() && matrixBody.getMatrix().isRowVector()) {
|
||||
Matrix matrix = matrixBody.getMatrix();
|
||||
if (matrixList.size() == 0) {
|
||||
matrixList.add(matrixBody);
|
||||
length = matrix.getY();
|
||||
} else {
|
||||
if (length == matrix.getY()) {
|
||||
matrixList.add(matrixBody);
|
||||
} else {
|
||||
throw new Exception("vector length is different");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new Exception("this matrix is not vector or rowVector");
|
||||
}
|
||||
}
|
||||
|
||||
private Matrix[] averageMatrix() throws Exception {
|
||||
Matrix[] matrices2 = new Matrix[speciesQuantity];//待比较均值K
|
||||
for (MatrixBody matrixBody : matrixList) {//遍历当前集合
|
||||
Matrix matrix = matrixBody.getMatrix();
|
||||
double min = 0;
|
||||
int id = 0;
|
||||
for (int i = 0; i < matrices.length; i++) {
|
||||
double dist = MatrixOperation.getEDist(matrix, matrices[i]);
|
||||
if (min == 0 || dist < min) {
|
||||
min = dist;
|
||||
id = i;
|
||||
}
|
||||
}
|
||||
List<MatrixBody> matrixList1 = clusterMap.get(id);
|
||||
matrixList1.add(matrixBody);
|
||||
}
|
||||
//重新计算均值
|
||||
for (Map.Entry<Integer, List<MatrixBody>> entry : clusterMap.entrySet()) {
|
||||
Matrix matrix = average(entry.getValue());
|
||||
matrices2[entry.getKey()] = matrix;
|
||||
}
|
||||
return matrices2;
|
||||
}
|
||||
|
||||
private void clear() {
|
||||
for (Map.Entry<Integer, List<MatrixBody>> entry : clusterMap.entrySet()) {
|
||||
entry.getValue().clear();
|
||||
}
|
||||
}
|
||||
|
||||
private Matrix average(List<MatrixBody> matrixList) throws Exception {//进行矩阵均值计算
|
||||
double nub = ArithUtil.div(1, matrixList.size());
|
||||
Matrix matrix = new Matrix(0, length);
|
||||
for (MatrixBody matrixBody1 : matrixList) {
|
||||
matrix = MatrixOperation.add(matrix, matrixBody1.getMatrix());
|
||||
}
|
||||
MatrixOperation.mathMul(matrix, nub);
|
||||
return matrix;
|
||||
}
|
||||
|
||||
public void start() throws Exception {//开始聚类
|
||||
if (matrixList.size() > 1) {
|
||||
Random random = new Random();
|
||||
for (int i = 0; i < matrices.length; i++) {//初始化均值向量
|
||||
int index = random.nextInt(matrixList.size());
|
||||
matrices[i] = matrixList.get(index).getMatrix();
|
||||
}
|
||||
//进行两者的比较
|
||||
boolean isEqual = false;
|
||||
do {
|
||||
Matrix[] matrices2 = averageMatrix();
|
||||
isEqual = equals(matrices, matrices2);
|
||||
if (!isEqual) {
|
||||
matrices = matrices2;
|
||||
clear();
|
||||
}
|
||||
}
|
||||
while (isEqual);
|
||||
} else {
|
||||
throw new Exception("matrixList number less than 2");
|
||||
}
|
||||
}
|
||||
|
||||
public boolean equals(Matrix[] matrices1, Matrix[] matrices2) throws Exception {
|
||||
boolean isEquals = true;
|
||||
for (int i = 0; i < matrices1.length; i++) {
|
||||
Matrix matrix1 = matrices1[i];
|
||||
Matrix matrix2 = matrices2[i];
|
||||
for (int j = 0; j < length; j++) {
|
||||
if (matrix1.getNumber(0, j) != matrix2.getNumber(0, j)) {
|
||||
isEquals = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return isEquals;
|
||||
}
|
||||
}
|
@ -0,0 +1,108 @@
|
||||
package org.wlld.imageRecognition.border;
|
||||
|
||||
import org.wlld.MatrixTools.Matrix;
|
||||
import org.wlld.MatrixTools.MatrixOperation;
|
||||
import org.wlld.tools.ArithUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* @author lidapeng
|
||||
* @description LVQ 向量量化
|
||||
* @date 5:36 下午 2020/2/4
|
||||
*/
|
||||
public class LVQ {
|
||||
private int typeNub;//原型聚类个数,即分类个数
|
||||
private MatrixBody[] model = new MatrixBody[typeNub];//原型向量
|
||||
private List<MatrixBody> matrixList = new ArrayList<>();
|
||||
private double studyPoint = 0.1;//量化学习率
|
||||
private int length;//向量长度
|
||||
|
||||
public LVQ(int typeNub) {
|
||||
this.typeNub = typeNub;
|
||||
}
|
||||
|
||||
public MatrixBody[] getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public void insertMatrixBody(MatrixBody matrixBody) throws Exception {
|
||||
if (matrixBody.getMatrix().isVector() && matrixBody.getMatrix().isRowVector()) {
|
||||
Matrix matrix = matrixBody.getMatrix();
|
||||
if (matrixList.size() == 0) {
|
||||
matrixList.add(matrixBody);
|
||||
length = matrix.getY();
|
||||
} else {
|
||||
if (length == matrix.getY()) {
|
||||
matrixList.add(matrixBody);
|
||||
} else {
|
||||
throw new Exception("vector length is different");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new Exception("this matrix is not vector or rowVector");
|
||||
}
|
||||
}
|
||||
|
||||
private double study() throws Exception {
|
||||
double error = 0;
|
||||
for (MatrixBody matrixBody : matrixList) {
|
||||
Matrix matrix = matrixBody.getMatrix();//特征向量
|
||||
long type = matrixBody.getId();//类别
|
||||
for (int i = 0; i < typeNub; i++) {
|
||||
MatrixBody modelBody = model[i];
|
||||
Matrix modelMatrix = modelBody.getMatrix();
|
||||
long id = modelBody.getId();
|
||||
boolean isRight = id == type;//类别是否相同
|
||||
//对矩阵进行修正
|
||||
Matrix matrix1 = op(matrix, modelMatrix, isRight);
|
||||
//修正矩阵与原矩阵的范数差
|
||||
double dist = vectorEqual(modelMatrix, matrix1);
|
||||
//将修正后的向量进行赋值
|
||||
modelBody.setMatrix(matrix1);
|
||||
//统计变化值
|
||||
error = ArithUtil.add(error, dist);
|
||||
}
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
//比较两个向量之间的范数差
|
||||
private double vectorEqual(Matrix matrix1, Matrix matrix2) throws Exception {
|
||||
Matrix matrix = MatrixOperation.sub(matrix1, matrix2);
|
||||
return MatrixOperation.getNorm(matrix);
|
||||
}
|
||||
|
||||
private Matrix op(Matrix matrix, Matrix modelMatrix, boolean isRight) throws Exception {
|
||||
Matrix matrix1 = MatrixOperation.sub(matrix, modelMatrix);
|
||||
MatrixOperation.mathMul(matrix1, studyPoint);
|
||||
Matrix matrix2;
|
||||
if (isRight) {
|
||||
matrix2 = MatrixOperation.add(modelMatrix, matrix1);
|
||||
} else {
|
||||
matrix2 = MatrixOperation.sub(modelMatrix, matrix1);
|
||||
}
|
||||
return matrix2;
|
||||
}
|
||||
|
||||
public void start() throws Exception {//开始向量量化聚类
|
||||
Random random = new Random();
|
||||
for (int i = 0; i < typeNub; i++) {
|
||||
MatrixBody matrixBody = new MatrixBody();
|
||||
Matrix matrix = new Matrix(1, length);
|
||||
matrixBody.setMatrix(matrix);
|
||||
matrixBody.setId(i);
|
||||
for (int j = 0; j < length; j++) {
|
||||
matrix.setNub(0, j, random.nextInt(10));
|
||||
}
|
||||
model[i] = matrixBody;
|
||||
}
|
||||
//初始化完成
|
||||
for (int k = 0; k < 1000; k++) {
|
||||
double error = study();
|
||||
System.out.println("error==" + error);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package org.wlld.imageRecognition.border;
|
||||
|
||||
import org.wlld.MatrixTools.Matrix;
|
||||
|
||||
/**
|
||||
* @author lidapeng
|
||||
* @description
|
||||
* @date 3:33 下午 2020/2/4
|
||||
*/
|
||||
public class MatrixBody {//给矩阵绑定一个id
|
||||
private int id;//唯一ID
|
||||
private Matrix matrix;//矩阵
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Matrix getMatrix() {
|
||||
return matrix;
|
||||
}
|
||||
|
||||
public void setMatrix(Matrix matrix) {
|
||||
this.matrix = matrix;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue