Пример #1
0
/**
* This application groups 'num_points' row-vectors (which are randomly
* generated) into 'num_means' clusters through an iterative algorithm - the
* k-means algorith
*/
int main(int argc, char **argv)
{

    int **points;
    int **means;
    int *clusters;

    int i;

    parse_args(argc, argv);
    points = (int **)malloc(sizeof(int *) * num_points);
    for (i=0; i<num_points; i++)
    {
        points[i] = (int *)malloc(sizeof(int) * dim);
    }
    dprintf("Generating points\n");
    generate_points(points, num_points);

    means = (int **)malloc(sizeof(int *) * num_means);
    for (i=0; i<num_means; i++)
    {
        means[i] = (int *)malloc(sizeof(int) * dim);
    }
    dprintf("Generating means\n");
    generate_points(means, num_means);

    clusters = (int *)malloc(sizeof(int) * num_points);
    memset(clusters, -1, sizeof(int) * num_points);

    modified = true;



    dprintf("\n\nStarting iterative algorithm\n");


    while (modified)
    {
        modified = false;
        dprintf(".");

        find_clusters(points, means, clusters);
        calc_means(points, means, clusters);
    }


    dprintf("\n\nFinal Means:\n");
    dump_matrix(means, num_means, dim);

    dprintf("Cleaning up\n");
    for (i=0; i<num_means; i++) {
        free(means[i]);
    }
    free(means);
    for (i=0; i<num_points; i++) {
        free(points[i]);
    }
    free(points);
    return 0;
}
Пример #2
0
/** kmeans_map()
 * Finds the cluster that is most suitable for a given set of points
 * 
 */
void kmeans_map(map_args_t *args) 
{
    assert(args);
    assert(args->length == 1);
    
    kmeans_map_data_t *map_data = args->data;
    find_clusters(map_data->points, map_data->means, map_data->clusters, map_data->length);  
    free(args->data);
}
Пример #3
0
/*!
 * \brief Split a node according to clusters.
 */
static void
split_node (struct rtree_node *node)
{
  int i;
  struct rtree_node *new_node;

  assert (node);
  assert (node->flags.is_leaf ? (void *) node->u.rects[M_SIZE].
          bptr : (void *) node->u.kids[M_SIZE]);
  new_node = find_clusters (node);
  if (node->parent == NULL)     /* split root node */
    {
      struct rtree_node *second;

      second = (struct rtree_node *)calloc (1, sizeof (*second));
      *second = *node;
      if (!second->flags.is_leaf)
        for (i = 0; i < M_SIZE; i++)
          if (second->u.kids[i])
            second->u.kids[i]->parent = second;
      node->flags.is_leaf = 0;
      node->flags.manage = 0;
      second->parent = new_node->parent = node;
      node->u.kids[0] = new_node;
      node->u.kids[1] = second;
      for (i = 2; i < M_SIZE + 1; i++)
        node->u.kids[i] = NULL;
      adjust_bounds (node);
      sort_node (node);
#ifdef SLOW_ASSERTS
      assert (__r_tree_is_good (node));
#endif
      return;
    }
  for (i = 0; i < M_SIZE; i++)
    if (!node->parent->u.kids[i])
      break;
  node->parent->u.kids[i] = new_node;
#ifdef SLOW_ASSERTS
  assert (__r_node_is_good (node));
  assert (__r_node_is_good (new_node));
#endif
  if (i < M_SIZE)
    {
#ifdef SLOW_ASSERTS
      assert (__r_node_is_good (node->parent));
#endif
      sort_node (node->parent);
      return;
    }
  split_node (node->parent);
}
Пример #4
0
/* 
 * Assigns ranks of non-leader nodes.
 * Expands same, min, max rank sets.
 * Leaf sets and clusters remain merged.
 * Sets minrank and maxrank appropriately.
 */
static void expand_ranksets(graph_t * g, aspect_t* asp)
{
    int c;
    node_t *n, *leader;

    if ((n = agfstnode(g))) {
	GD_minrank(g) = MAXSHORT;
	GD_maxrank(g) = -1;
	while (n) {
	    leader = UF_find(n);
	    /* The following works because ND_rank(n) == 0 if n is not in a
	     * cluster, and ND_rank(n) = the local rank offset if n is in
	     * a cluster. */
	    if ((leader != n) && (!asp || (ND_rank(n) == 0)))
		ND_rank(n) += ND_rank(leader);

	    if (GD_maxrank(g) < ND_rank(n))
		GD_maxrank(g) = ND_rank(n);
	    if (GD_minrank(g) > ND_rank(n))
		GD_minrank(g) = ND_rank(n);

	    if (ND_ranktype(n) && (ND_ranktype(n) != LEAFSET))
		UF_singleton(n);
	    n = agnxtnode(g, n);
	}
	if (g == dot_root(g)) {
	    if (CL_type == LOCAL) {
		for (c = 1; c <= GD_n_cluster(g); c++)
		    set_minmax(GD_clust(g)[c]);
	    } else {
		find_clusters(g);
	    }
	}
    } else {
	GD_minrank(g) = GD_maxrank(g) = 0;
    }
}
Пример #5
0
Файл: main.c Проект: nareto/ADS
void graph_interface(){
  char input, string[MAX_LINE_LENGTH];
  list_node * ln;
  int end=0, i, min_weight = 1, bfs_depth, min_nodes = 0;
  clusters * clst;
  clst = NULL;

  while(!end){
    if(PPRINT){
      printf("\n \033[1;31mGraph commands:\033[0m \n %3s \t %s \n %3s \t %s \n %3s \t %s \n %3s \t %s \n %3s \t %s \n %3s \t %s \n %3s \t %s \n %3s \t %s \n %3s \t %s \n %3s \t %s \n %3s \t %s \n \n: ",
	     "\033[1;32ma\033[0m", "print an author and its articles",
	     "\033[1;32mA\033[0m", "print an article and its authors",
	     "\033[1;32mn\033[0m", "print an article and its neighbors in the article graph",
	     "\033[1;32mc\033[0m", "print the article count (number of nodes in graph)",
	     "\033[1;32mt\033[0m", "print the graph total edges",
	     "\033[1;32mm\033[0m", "print the graph nodes' medium edges",
	     "\033[1;32mM\033[0m", "print the graph nodes' max_edges",
	     "\033[1;32mw\033[0m", "print the edge mean weight",
	     "\033[1;32mC\033[0m", "calculate clusters",
	     "\033[1;32mr\033[0m", "print representatives from last calculated clusters",
	     "\033[1;32mq\033[0m", "return to main menu");
    }
    else{
      printf("\n Graph commands: \n %3s \t %s \n %3s \t %s \n %3s \t %s \n %3s \t %s \n %3s \t %s \n %3s \t %s \n %3s \t %s \n %3s \t %s \n %3s \t %s \n %3s \t %s \n %3s \t %s \n \n: ",
	     "a", "print an author and its articles",
	     "A", "print an article and its authors",
	     "n", "print an article and its neighbors in the article graph",
	     "c", "print the article count (number of nodes in graph)",
	     "t", "print the graph total edges",
	     "m", "print the graph nodes' medium edges",
	     "M", "print the graph nodes' max_edges",
	     "w", "print the edge mean weight",
	     "C", "calculate clusters",
	     "r", "print representatives from last calculated clusters",
	     "q", "return to main menu");
    }
    scanf(" %c", &input);
    switch(input) {
    case 'a':
      printf("\n Author Name: ");
      flush_input_buffer();
      fgets(string, MAX_LINE_LENGTH, stdin);
      remove_ending_newline(string);
      ln = search_in_hash(string, authors_dict);
      if(ln == NULL)
	printf("\n sorry, author not present \n");
      else
	author_print((author *) ln->key);
      break;
    case 'A':
      printf("\n Article Id: ");
      flush_input_buffer();
      scanf("%d", &i);
      print_article_node(artcl_graph->nodes[i]);
      break;
    case 'n':
      printf("\n Article Id: ");
      flush_input_buffer();
      scanf("%d", &i);
      printf("\n Minimum edge weight: ");
      flush_input_buffer();
      scanf("%d", &min_weight);
      printf("\n Depth of search: ");
      flush_input_buffer();
      scanf("%d", &bfs_depth);
      print_neighbours(artcl_graph->nodes[i], bfs_depth, min_weight);
      break;    
    case 'c':
      printf("\n %d Articles", artcl_graph->n_nodes);
      break;
    case 't':
      printf("\n %d edges in the article graph", total_edges(artcl_graph));
      break;
    case 'm':
      printf("\n Articles in the graph have %f edges in mean", mean_edges(artcl_graph)); 
      break;
    case 'M':
      printf("\n The article with the biggest number of edges is:");
      print_article_node(max_edges(artcl_graph));
      break;
    case 'w':
      printf("\n The mean edge weight is: %f", mean_edge_weight(artcl_graph));
      break;
    case 'C':
      printf("\n Minimum edge weight: ");
      flush_input_buffer();
      scanf("%d", &min_weight);
      if(clst != NULL){
	free(clst->representatives);
	free(clst->nodes_in_cluster);
	free(clst);
      }
      clst = find_clusters(artcl_graph, min_weight);
      printf("\n %d clusters found", clst->n_rpr);
      break;
    case 'r':
      printf("\n Minimum nodes in cluster: ");
      flush_input_buffer();
      scanf("%d", &min_nodes);
      if(clst != NULL){
	for(i=0;i<clst->n_rpr;++i){
	  if(clst->nodes_in_cluster[i]>= min_nodes){
	    if(PPRINT)
	      printf("\n \033[1;32mCluster %d has %d nodes. Representative:\033[0m", i, clst->nodes_in_cluster[i]);
	    else
	      printf("\n Cluster %d has %d nodes. Representative:", i, clst->nodes_in_cluster[i]);
	    print_article_node(clst->representatives[i]);
	  }
	}
      }
      break;
    case 'q':
      end=1;
      break;
    }
  }
}
Пример #6
0
/** 
* This application groups 'num_points' row-vectors (which are randomly
* generated) into 'num_means' clusters through an iterative algorithm - the 
* k-means algorith 
*/
int main(int argc, char **argv) 
{

    int **points;
    int **means;
    int *clusters;
    
    int i;
    struct timeval begin, end;

    parse_args(argc, argv);
    points = (int **)malloc(sizeof(int *) * num_points);
    for (i=0; i<num_points; i++) 
    {
        points[i] = (int *)malloc(sizeof(int) * dim);
    }
    dprintf("Generating points\n");
    generate_points(points, num_points);    

    means = (int **)malloc(sizeof(int *) * num_means);
    for (i=0; i<num_means; i++) 
    {
        means[i] = (int *)malloc(sizeof(int) * dim);
    }
    dprintf("Generating means\n");
    generate_points(means, num_means);
    
    clusters = (int *)malloc(sizeof(int) * num_points);
    memset(clusters, -1, sizeof(int) * num_points);
    
    modified = true;
    
    
    
    dprintf("\n\nStarting iterative algorithm\n");
    
    
    get_time (&begin);
    while (modified) 
    {
        modified = false;
        dprintf(".");
        
        find_clusters(points, means, clusters);
        calc_means(points, means, clusters);
    }
    get_time (&end);

#ifdef TIMING
    fprintf (stderr, "library: %u\n", time_diff (&end, &begin));
#endif

    
    dprintf("\n\nFinal Means:\n");
    dump_matrix(means, num_means, dim);
    
    dprintf("Cleaning up\n");
    for (i=0; i<num_means; i++) {
        free(means[i]);
    }
    free(means);
    for (i=0; i<num_points; i++) {
        free(points[i]);
    }
    free(points);
    return 0;  
}
Пример #7
0
void Pipeline::run(const bool save_clouds, const bool show_clouds)
{
	Logger _log("Pipeline");

	/**
	 * Stage 0: Load images from file
	 */
	Images images;
	load_images(folder_path, images);


	/**
	 * Stage 1: Detect features in loaded images
	 */
	CamFrames cam_Frames;
	DescriptorsVec descriptors_vec;
	extract_features(images, cam_Frames, descriptors_vec);

	// Free Image.gray
	for (int i = 0; i < images.size(); i++)
		const_cast<cv::Mat&>(images[i].gray).release();


	/**
	 * Stage 2: Calculate descriptors and find image pairs through matching
	 */
	ImagePairs image_pairs;
	find_matching_pairs(images, cam_Frames, descriptors_vec, image_pairs);


	// Free some memory
	DescriptorsVec().swap(descriptors_vec);



	/**
	 * State 3: Compute pairwise R and t
	 */
	register_camera(image_pairs, cam_Frames);


	/**
	 * Stage 4: Construct associativity matrix and spanning tree
	 */
	Associativity assocMat(cam_Frames.size());
	for (int p = 0; p < image_pairs.size(); p++)
	{
		ImagePair* pair = &image_pairs[p];
		int i = pair->pair_index.first,
		    j = pair->pair_index.second;

		if (pair -> R.empty()) continue;

		assocMat(i, j) = pair;
		assocMat(j, i) = pair;

		assert(assocMat(i, j) == assocMat(j, i));
		assert(assocMat(i, j)->pair_index.first  == i &&
		       assocMat(i, j)->pair_index.second == j);
	}

	Associativity tree;
	const int camera_num = build_spanning_tree(image_pairs, assocMat, tree);


	/**
	 * Stage 5: Compute global Rs and ts
	 */
	CameraPoses gCameraPoses;
	glo_cam_poses(images, gCameraPoses, image_pairs, tree);


	/**
	 * Stage 6: Find and cluster depth points from local camera frame to global camera frame
	 */
	PointClusters pointClusters;
	PointMap pointMap;
	find_clusters(assocMat, gCameraPoses, cam_Frames, pointClusters, pointMap);

	// Free some memory
	ImagePairs().swap(image_pairs);


	/**
	 * Stage 7: get center of mass from clusters
	 */
	PointCloud pointCloud(pointClusters.size());
	find_CoM(pointClusters, pointCloud);

	// Free pointClusters
	for (int i = 0; i < pointClusters.size(); i++)
		PointCluster().swap(pointClusters[i]);
	PointClusters().swap(pointClusters);


	// Save cloud before BA
	Viewer viewer("Before BA");
	auto cloud  = viewer.createPointCloud(images, gCameraPoses, cameraMatrix);
	int  n      = cloud->points.size();
	auto time   = ptime::second_clock::local_time();
	auto tstamp = ptime::to_iso_string(time);
	auto folder = fs::path(folder_path).filename().string();
	auto fname  = (fmt("%s_%s_%d_noBA.pcd") % folder % tstamp % n).str();

	if (save_clouds)
		viewer.saveCloud(cloud, fname);
	if (show_clouds)
		viewer.showCloudPoints(cloud, false);


	/**
	 * State 8: Bundle Adjustment
	 */
	bundle_adjustment(pointMap, cam_Frames, false, gCameraPoses, pointCloud);
	_log.tok();

	/**
	 * Show calculated point cloud
	 */
	Viewer viewer_ba("After BA no Depth");
	cloud = viewer_ba.createPointCloud(images, gCameraPoses, cameraMatrix);
	n     = cloud->points.size();
	fname = (fmt("%s_%s_%d_BA_noD.pcd") % folder % tstamp % n).str();


	if (save_clouds)
		viewer_ba.saveCloud(cloud, fname);
	if (show_clouds)
		viewer_ba.showCloudPoints(cloud,false);

	bundle_adjustment(pointMap, cam_Frames, true, gCameraPoses, pointCloud);

	// Free some memory
	PointMap().swap(pointMap);
	CamFrames().swap(cam_Frames);
	PointCloud().swap(pointCloud);

	/**
	 * Show calculated point cloud
	 */
	Viewer viewer_baD("After BA with Depth");
	cloud = viewer_baD.createPointCloud(images, gCameraPoses, cameraMatrix);
	n     = cloud->points.size();
	fname = (fmt("%s_%s_%d_BA_D.pcd") % folder % tstamp % n).str();

	// Free some memory
	Images().swap(images);
	CameraPoses().swap(gCameraPoses);

	if (save_clouds)
		viewer_baD.saveCloud(cloud, fname);
	if (show_clouds)
		viewer_baD.showCloudPoints(cloud);



}