Skip to content

Commit

Permalink
remove unnecessary codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Yigang0622 committed Dec 10, 2016
1 parent e168157 commit 8558ec6
Show file tree
Hide file tree
Showing 12 changed files with 803 additions and 59 deletions.
777 changes: 777 additions & 0 deletions .idea/workspace.xml

Large diffs are not rendered by default.

Binary file added out/production/KMeans/Algo/Cluster.class
Binary file not shown.
Binary file added out/production/KMeans/Algo/Distance.class
Binary file not shown.
Binary file added out/production/KMeans/Algo/KMeans.class
Binary file not shown.
Binary file added out/production/KMeans/Algo/Main.class
Binary file not shown.
Binary file added out/production/KMeans/Algo/MyVector.class
Binary file not shown.
Binary file added out/production/KMeans/ColorPicker/FileUtil.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
33 changes: 1 addition & 32 deletions src/Algo/Cluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.ArrayList;

/**
*
* 聚类
* Created by Mike on 12/9/2016.
*/
public class Cluster {
Expand Down Expand Up @@ -59,36 +59,6 @@ public MyVector getCenterVector(){
return vector;
}

private boolean hasVector(MyVector vector){

boolean has = false;

for(MyVector mVector:vectors){
if (mVector.isSameVector(vector)){
has = true;
}
}

return has;
}

public boolean isSameCluster(Cluster cluster){

if (cluster.eachVectorSize != this.eachVectorSize){
return false;
}

int counter = 0;
for (MyVector vector:cluster.getVectors()){
if (hasVector(vector)){
counter ++;
}
}

return counter == vectors.size();

}



public static void main(String[] args){
Expand All @@ -102,7 +72,6 @@ public static void main(String[] args){
cluster2.addToCluster(myVector2);
cluster2.addToCluster(myVector);

System.out.println(cluster.isSameCluster(cluster2));



Expand Down
45 changes: 23 additions & 22 deletions src/Algo/KMeans.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
public class KMeans {

private List<MyVector> vectors = new ArrayList<>();

private List<MyVector> centers = new ArrayList<>(); //质心

private int numberOfCluster = 0;
Expand All @@ -23,8 +22,6 @@ public KMeans(List<MyVector> vectors,int numberOfCluster){
this.numberOfCluster = numberOfCluster;
initCenters();
initClusters();


}

public List<Cluster> getClusters(){
Expand Down Expand Up @@ -59,10 +56,9 @@ public void startClustering(){
boolean converged = false;

while (!converged && counter < numberOfIteration){

//printCenters();
System.out.println("第"+counter+"次迭代");

// printCenters();
double[][] distanceMatrix = new double[vectors.size()][numberOfCluster];

//生成距离矩阵
Expand Down Expand Up @@ -107,10 +103,15 @@ public void startClustering(){

System.out.println("聚类完成\n迭代次数"+counter);

// printClusters();
//printClusters();

}

/**
* 检测是否收敛(中心点和上次比是否变化)
* @param lastCenters
* @return
*/
private boolean isConverged(List<MyVector> lastCenters){

if (lastCenters.size() != numberOfCluster){
Expand Down Expand Up @@ -138,7 +139,11 @@ private boolean isConverged(List<MyVector> lastCenters){

}


/**
* 得到数组中最小值的下标
* @param arr
* @return
*/
private int getMinDistanceIndex(double[] arr){

double min = 99999999999999999999999.0;
Expand All @@ -153,6 +158,10 @@ private int getMinDistanceIndex(double[] arr){
return index;
}

/**
* 打印二维数组
* @param mat
*/
private void printMatrix(double[][] mat){

int height = mat.length;
Expand All @@ -166,13 +175,19 @@ private void printMatrix(double[][] mat){
}
}

/**
* 打印中心点
*/
private void printCenters(){
System.out.println("Printing Centers");
for (MyVector vector:centers){
vector.printVector();
}
}

/**
* 打印所有聚类
*/
private void printClusters(){
for (Cluster cluster:clusters){
cluster.printCluster(false);
Expand All @@ -198,36 +213,22 @@ private void initCenters(){

public static void main(String[] args){
List<MyVector> vectors = new ArrayList<>();
vectors.add(new MyVector(new double[]{1.0, 1.0}));
vectors.add(new MyVector(new double[]{9.0, 8.0}));
vectors.add(new MyVector(new double[]{2.0, 1.0}));
vectors.add(new MyVector(new double[]{3.0, 2.0}));
vectors.add(new MyVector(new double[]{10.0, 10.0}));
vectors.add(new MyVector(new double[]{7.0, 8.0}));
vectors.add(new MyVector(new double[]{3.0, 1.0}));
vectors.add(new MyVector(new double[]{8.0, 9.0}));
vectors.add(new MyVector(new double[]{367, 10.0}));
vectors.add(new MyVector(new double[]{10.0, 7.0}));
vectors.add(new MyVector(new double[]{33.0, 8.0}));
vectors.add(new MyVector(new double[]{1.0, 2.0}));
vectors.add(new MyVector(new double[]{33.0, 9.0}));
vectors.add(new MyVector(new double[]{35.0, 7.0}));
vectors.add(new MyVector(new double[]{100.0, 9.0}));
vectors.add(new MyVector(new double[]{33.0, 10.0}));
vectors.add(new MyVector(new double[]{8.0, 10.0}));
vectors.add(new MyVector(new double[]{105.0, 7.0}));
vectors.add(new MyVector(new double[]{99.0, 8.0}));
vectors.add(new MyVector(new double[]{1.0, 4.0}));
vectors.add(new MyVector(new double[]{101.0, 10.0}));
vectors.add(new MyVector(new double[]{333, 8.0}));
vectors.add(new MyVector(new double[]{444, 9.0}));
vectors.add(new MyVector(new double[]{423, 7.0}));
vectors.add(new MyVector(new double[]{0.1, 2.0}));
KMeans kMeans = new KMeans(vectors,5);
KMeans kMeans = new KMeans(vectors,3);
kMeans.startClustering();



}


Expand Down
7 changes: 2 additions & 5 deletions src/ColorPicker/ThemeColorPicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ public ThemeColorPicker(String path){

public void getThemeColor(){

System.out.println("开始分析图片主色调");

KMeans kMeans = new KMeans(vectors,colorNumber);
kMeans.startClustering();
System.out.println("分析完成");
List<Cluster> clusters = kMeans.getClusters();

List<MyVector> vectors = new ArrayList<>();
Expand Down Expand Up @@ -102,8 +99,8 @@ private double getB(int x,int y){
}

public static void main(String[] args){
ThemeColorPicker picker = new ThemeColorPicker("C:\\Users\\Mike\\Desktop\\1.png");
picker.setColorNumber(8);
ThemeColorPicker picker = new ThemeColorPicker("/Users/Mike/Desktop/1.jpg");
picker.setColorNumber(5);
picker.getThemeColor();
}

Expand Down

0 comments on commit 8558ec6

Please sign in to comment.