comparison c/Motion.cpp @ 190:36968a63efe1

Got the connected_components to finally work using a vecS for the vertex list in the adjacency list. In this case, the component map is simply a vector of ints (which is the type of UndirectedGraph::vextex_descriptor (=graph_traits<FeatureGraph>::vertex_descriptor) and probably UndirectedGraph::vertices_size_type). To use listS, I was told on the Boost mailing list: >> If you truly need listS, you will need to create a vertex index >> map, fill it in before you create the property map, and pass it to the >> vector_property_map constructor (and as a type argument to that class). It may be feasible with a component map like shared_array_property_map< graph_traits<FeatureGraph>::vertex_descriptor, property_map<FeatureGraph, vertex_index_t>::const_type > components(num_vertices(g), get(vertex_index, g));
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 07 Dec 2011 18:51:32 -0500
parents 1116f0a1ff31
children 0e60a306d324
comparison
equal deleted inserted replaced
189:1116f0a1ff31 190:36968a63efe1
5 5
6 #include "opencv2/core/core.hpp" 6 #include "opencv2/core/core.hpp"
7 #include "opencv2/highgui/highgui.hpp" 7 #include "opencv2/highgui/highgui.hpp"
8 8
9 #include <boost/graph/connected_components.hpp> 9 #include <boost/graph/connected_components.hpp>
10 #include <boost/config.hpp>
11 10
12 #include <iostream> 11 #include <iostream>
13 #include <vector> 12 #include <vector>
14 #include <algorithm> 13 #include <algorithm>
15 #include <utility> 14 #include <utility>
16 15
17 using namespace std; 16 using namespace std;
18 using namespace cv; 17 using namespace cv;
18 using namespace boost;
19 19
20 /******************** FeatureTrajectory ********************/ 20 /******************** FeatureTrajectory ********************/
21 21
22 FeatureTrajectory::FeatureTrajectory(const int& frameNum, const cv::Point2f& p, const Mat& homography) 22 FeatureTrajectory::FeatureTrajectory(const int& frameNum, const cv::Point2f& p, const Mat& homography)
23 : lost(false) { 23 : lost(false) {
73 } 73 }
74 74
75 bool FeatureTrajectory::minMaxSimilarity(const FeatureTrajectory& ft, const int& firstInstant, const int& lastInstant, float connectionDistance, float segmentationDistance) { 75 bool FeatureTrajectory::minMaxSimilarity(const FeatureTrajectory& ft, const int& firstInstant, const int& lastInstant, float connectionDistance, float segmentationDistance) {
76 float minDistance = norm(positions->getPointAtInstant(firstInstant)-ft.positions->getPointAtInstant(firstInstant)); 76 float minDistance = norm(positions->getPointAtInstant(firstInstant)-ft.positions->getPointAtInstant(firstInstant));
77 float maxDistance = minDistance; 77 float maxDistance = minDistance;
78 bool connected = minDistance <= connectionDistance; 78 bool connected = (minDistance <= connectionDistance);
79 int t=firstInstant+1; 79 int t=firstInstant+1;
80 while (t <= lastInstant && connected) { 80 while (t <= lastInstant && connected) {
81 float distance = norm(positions->getPointAtInstant(t)-ft.positions->getPointAtInstant(t)); 81 float distance = norm(positions->getPointAtInstant(t)-ft.positions->getPointAtInstant(t));
82 if (distance < minDistance) 82 if (distance < minDistance)
83 minDistance = distance; 83 minDistance = distance;
147 /******************** FeatureGraph ********************/ 147 /******************** FeatureGraph ********************/
148 148
149 void FeatureGraph::addFeature(const FeatureTrajectoryPtr& ft) { 149 void FeatureGraph::addFeature(const FeatureTrajectoryPtr& ft) {
150 UndirectedGraph::vertex_descriptor newVertex = add_vertex(graph); 150 UndirectedGraph::vertex_descriptor newVertex = add_vertex(graph);
151 graph[newVertex].feature = ft; 151 graph[newVertex].feature = ft;
152 for (boost::graph_traits<UndirectedGraph>::vertex_iterator vi = vertices(graph).first; 152 for (graph_traits<UndirectedGraph>::vertex_iterator vi = vertices(graph).first;
153 vi!=vertices(graph).second; ++vi) { // vi pointer to vertex_descriptor 153 vi!=vertices(graph).second; ++vi) { // vi pointer to vertex_descriptor
154 FeatureTrajectoryPtr ft2 = graph[*vi].feature; 154 FeatureTrajectoryPtr ft2 = graph[*vi].feature;
155 int lastInstant = static_cast<int>(min(ft->getLastInstant(), ft2->getLastInstant())); 155 int lastInstant = static_cast<int>(min(ft->getLastInstant(), ft2->getLastInstant()));
156 int firstInstant = static_cast<int>(max(ft->getFirstInstant(), ft2->getFirstInstant())); 156 int firstInstant = static_cast<int>(max(ft->getFirstInstant(), ft2->getFirstInstant()));
157 if (lastInstant-firstInstant > static_cast<int>(minFeatureTime)) { // equivalent to lastInstant-firstInstant+1 >= minFeatureTime 157 if (lastInstant-firstInstant > static_cast<int>(minFeatureTime)) { // equivalent to lastInstant-firstInstant+1 >= minFeatureTime
164 } 164 }
165 } 165 }
166 } 166 }
167 167
168 void FeatureGraph::connectedComponents(const int& lastInstant) { 168 void FeatureGraph::connectedComponents(const int& lastInstant) {
169 typedef boost::graph_traits<UndirectedGraph>::vertices_size_type vertices_size_type; 169 vector<UndirectedGraph::vertex_descriptor> components(num_vertices(graph));
170 vector<vertices_size_type> components(num_vertices(graph)); 170 //vector_property_map<UndirectedGraph::vertex_descriptor> components(num_vertices(graph));
171 // int num = connected_components(graph, &components[0]); 171 //vector_property_map< graph_traits<UndirectedGraph>::vertex_descriptor, property_map<UndirectedGraph, vertex_index_t>::const_type > components(num_vertices(graph));
172 // // todo change the type of the component map http://www.boost.org/doc/libs/1_48_0/libs/graph/doc/connected_components.html
173 // cout << "Total number of components: " << num << endl;
174 172
175 // for (unsigned int i = 0; i < components.size(); ++i) 173 int num = connected_components(graph, &components[0]);
176 // cout << "Vertex " << i <<" is in component " << components[i] << endl; 174 cout << "Total number of components: " << num << endl;
177 // cout << endl; 175
176 for (unsigned int i = 0; i < num_vertices(graph); ++i)
177 cout << "Vertex " << i <<" is in component " << components[i] << endl;
178 cout << endl;
178 } 179 }
179 180
180 string FeatureGraph::informationString(void) { 181 string FeatureGraph::informationString(void) {
181 stringstream ss; 182 stringstream ss;
182 ss << num_vertices(graph) << " vertices, " << num_edges(graph) << " edges"; 183 ss << num_vertices(graph) << " vertices, " << num_edges(graph) << " edges";