Package image.processing.patterns.cluster

Examples of image.processing.patterns.cluster.Cluster


               
    // SPECIAL CASE: If only one vector
    if (vectors.size() == 1) {
      // Create a single cluster and return it
      DoubleArray vector = vectors.get(0);
      Cluster cluster = new Cluster(vectorsSize);
      cluster.addVector(vector);
      clusters.add(cluster);
      return clusters;
    }
   
    // (1) Randomly generate k empty clusters with a random mean (cluster
    // center)
    for(int i=0; i< k; i++){
      DoubleArray meanVector = generateRandomVector(minValue, maxValue, vectorsSize);
      Cluster cluster = new Cluster(vectorsSize);
      cluster.setMean(meanVector);
      clusters.add(cluster);
    }

    // (2) Repeat the two next steps until the assignment hasn't changed
    boolean changed;
    while(true) {
      iterationCount++;
      changed = false;
      // (2.1) Assign each point to the nearest cluster center.

      // / for each vector
      for (DoubleArray vector : vectors) {
        // find the nearest cluster and the cluster containing the item
        Cluster nearestCluster = null;
        Cluster containingCluster = null;
        double distanceToNearestCluster = Double.MAX_VALUE;

        // for each cluster
        for (Cluster cluster : clusters) {
          // calculate the distance of the cluster mean to the vector
          double distance = euclideanDistance(cluster.getmean(), vector);
          // if it is the smallest distance until now, record this cluster
          // and the distance
          if (distance < distanceToNearestCluster) {
            nearestCluster = cluster;
            distanceToNearestCluster = distance;
          }
          // if the cluster contain the vector already,
          // remember that too!
          if (cluster.contains(vector)) {
            containingCluster = cluster;
          }
        }

        // if the nearest cluster is not the cluster containing
        // the vector
        if (containingCluster != nearestCluster) {
          // remove the vector from the containing cluster
          if (containingCluster != null) {
            containingCluster.remove(vector);
          }
          // add the vector to the nearest cluster
          nearestCluster.addVector(vector);
          changed = true;
        }
View Full Code Here


                        System.out.println(line);
      DoubleArray theVector = new DoubleArray(vector, vectorCount);
                        vectorCount++;
     
      // Initiallly we create a cluster for each vector
      Cluster cluster = new Cluster(vector.length);
      cluster.addVector(theVector);
      cluster.setMean(theVector.clone());
      clusters.add(cluster);
    }
    reader.close(); // close the input file

    // (2) Loop to combine the two closest clusters into a bigger cluster
View Full Code Here

   * @return true if a merge was done, otherwise false.
   */
  private boolean mergeTheClosestCluster() {
    // These variables will contain the two closest clusters that
    // can be merged
    Cluster clusterToMerge1 = null;
    Cluster clusterToMerge2 = null;
    double minClusterDistance = Integer.MAX_VALUE;

    // find the two closest clusters with distance > threshold
    // by comparing all pairs of clusters i and j
    for (int i = 0; i < clusters.size(); i++) {
      for (int j = i + 1; j < clusters.size(); j++) {
        // calculate the distance between i and j
        double distance = euclideanDistance(clusters.get(i).getmean(), clusters.get(j).getmean());
        // if the distance is less than the max distance allowed
        // and if it is the smallest distance until now
        if (distance < minClusterDistance && distance <= maxDistance) {
          // record this pair of clusters
          minClusterDistance = distance;
          clusterToMerge1 = clusters.get(i);
          clusterToMerge2 = clusters.get(j);
        }
      }
    }

    // if no close clusters were found, return false
    if (clusterToMerge1 == null) {
      return false;
    }

    // else, merge the two closest clusters
    for(DoubleArray vector : clusterToMerge2.getVectors()){
      clusterToMerge1.addVector(vector);
    }
    // after mergint, we need to recompute the mean of the resulting cluster
    clusterToMerge1.recomputeClusterMean();
    // we delete the cluster that was merged
View Full Code Here

    System.out.println("Vector size: " + vectors.size());
    // SPECIAL CASE: If only one vector
    if (vectors.size() == 1) {
      // Create a single cluster and return it
      DoubleArray vector = vectors.get(0);
      Cluster cluster = new Cluster(vectorsSize);
      cluster.addVector(vector);
      clusters.add(cluster);
      return clusters;
    }
   
    // (1) Randomly generate k empty clusters with a random mean (cluster
    // center)
    for(int i=0; i< k; i++){
                        int rand = 0 + (int)(Math.random() * vectors.size()-1);
      DoubleArray meanVector = vectors.get(rand);
      Cluster cluster = new Cluster(vectorsSize);
      cluster.setMean(meanVector);
      clusters.add(cluster);
    }

    // (2) Repeat the two next steps until the assignment hasn't changed
    boolean changed;
    while(true) {
      iterationCount++;
      changed = false;
      // (2.1) Assign each point to the nearest cluster center.

      // / for each vector
      for (DoubleArray vector : vectors) {
        // find the nearest cluster and the cluster containing the item
        Cluster nearestCluster = null;
        Cluster containingCluster = null;
        double distanceToNearestCluster = Double.MAX_VALUE;

        // for each cluster
        for (Cluster cluster : clusters) {
          // calculate the distance of the cluster mean to the vector
          double distance = euclideanDistance(cluster.getmean(), vector);
          // if it is the smallest distance until now, record this cluster
          // and the distance
          if (distance < distanceToNearestCluster) {
            nearestCluster = cluster;
            distanceToNearestCluster = distance;
          }
          // if the cluster contain the vector already,
          // remember that too!
          if (cluster.contains(vector)) {
            containingCluster = cluster;
          }
        }

        // if the nearest cluster is not the cluster containing
        // the vector
        if (containingCluster != nearestCluster) {
          // remove the vector from the containing cluster
          if (containingCluster != null) {
            containingCluster.remove(vector);
          }
          // add the vector to the nearest cluster
          nearestCluster.addVector(vector);
          changed = true;
        }
View Full Code Here

TOP

Related Classes of image.processing.patterns.cluster.Cluster

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.