updated saving matches graph in opencv_stitching
authorAlexey Spizhevoy <no@email>
Mon, 15 Aug 2011 09:22:22 +0000 (09:22 +0000)
committerAlexey Spizhevoy <no@email>
Mon, 15 Aug 2011 09:22:22 +0000 (09:22 +0000)
modules/stitching/motion_estimators.cpp

index cf359f1..56aa4d1 100644 (file)
@@ -414,57 +414,59 @@ string matchesGraphAsString(vector<string> &pathes, vector<MatchesInfo> &pairwis
     stringstream str;\r
     str << "graph matches_graph{\n";\r
 \r
-    set<int> added_imgs;\r
+    const int num_images = static_cast<int>(pathes.size());\r
+    set<pair<int,int> > span_tree_edges;\r
+    DjSets comps(num_images);\r
 \r
-    // Add matches\r
-    for (size_t i = 0; i < pairwise_matches.size(); ++i)\r
+    for (int i = 0; i < num_images; ++i)\r
     {\r
-        if (pairwise_matches[i].src_img_idx < pairwise_matches[i].dst_img_idx &&\r
-            pairwise_matches[i].confidence > conf_threshold)\r
+        for (int j = 0; j < num_images; ++j)\r
         {\r
-            string name_src = pathes[pairwise_matches[i].src_img_idx];\r
+            if (pairwise_matches[i*num_images + j].confidence < conf_threshold)\r
+                continue;\r
+            int comp1 = comps.find(i);\r
+            int comp2 = comps.find(j);\r
+            if (comp1 != comp2)\r
+            {\r
+                comps.merge(comp1, comp2);\r
+                span_tree_edges.insert(make_pair(i, j));\r
+            }\r
+        }\r
+    }\r
+\r
+    for (set<pair<int,int> >::const_iterator itr = span_tree_edges.begin();\r
+         itr != span_tree_edges.end(); ++itr)\r
+    {\r
+        pair<int,int> edge = *itr;\r
+        if (span_tree_edges.find(edge) != span_tree_edges.end())\r
+        {\r
+            string name_src = pathes[edge.first];\r
             size_t prefix_len = name_src.find_last_of("/\\");\r
             if (prefix_len != string::npos) prefix_len++; else prefix_len = 0;\r
             name_src = name_src.substr(prefix_len, name_src.size() - prefix_len);\r
 \r
-            string name_dst = pathes[pairwise_matches[i].dst_img_idx];\r
+            string name_dst = pathes[edge.second];\r
             prefix_len = name_dst.find_last_of("/\\");\r
             if (prefix_len != string::npos) prefix_len++; else prefix_len = 0;\r
             name_dst = name_dst.substr(prefix_len, name_dst.size() - prefix_len);\r
 \r
-            added_imgs.insert(pairwise_matches[i].src_img_idx);\r
-            added_imgs.insert(pairwise_matches[i].dst_img_idx);\r
-\r
+            int pos = edge.first*num_images + edge.second;\r
             str << "\"" << name_src << "\" -- \"" << name_dst << "\""\r
-                << "[label=\"Nm=" << pairwise_matches[i].matches.size()\r
-                << ", Ni=" << pairwise_matches[i].num_inliers\r
-                << ", C=" << pairwise_matches[i].confidence << "\"];\n";\r
+                << "[label=\"Nm=" << pairwise_matches[pos].matches.size()\r
+                << ", Ni=" << pairwise_matches[pos].num_inliers\r
+                << ", C=" << pairwise_matches[pos].confidence << "\"];\n";\r
         }\r
     }\r
 \r
-    // Add unmatched images\r
-    for (size_t i = 0; i < pairwise_matches.size(); ++i)\r
+    for (size_t i = 0; i < comps.size.size(); ++i)\r
     {\r
-        if (pairwise_matches[i].src_img_idx < pairwise_matches[i].dst_img_idx)\r
+        if (comps.size[comps.find(i)] == 1)\r
         {\r
-            if (added_imgs.find(pairwise_matches[i].src_img_idx) == added_imgs.end())\r
-            {\r
-                added_imgs.insert(pairwise_matches[i].src_img_idx);\r
-                string name = pathes[pairwise_matches[i].src_img_idx];\r
-                size_t prefix_len = name.find_last_of("/\\");\r
-                if (prefix_len != string::npos) prefix_len++; else prefix_len = 0;\r
-                name = name.substr(prefix_len, name.size() - prefix_len);\r
-                str << "\"" << name << "\";\n";\r
-            }\r
-            if (added_imgs.find(pairwise_matches[i].dst_img_idx) == added_imgs.end())\r
-            {\r
-                added_imgs.insert(pairwise_matches[i].dst_img_idx);\r
-                string name = pathes[pairwise_matches[i].dst_img_idx];\r
-                size_t prefix_len = name.find_last_of("/\\");\r
-                if (prefix_len != string::npos) prefix_len++; else prefix_len = 0;\r
-                name = name.substr(prefix_len, name.size() - prefix_len);\r
-                str << "\"" << name << "\";\n";\r
-            }\r
+            string name = pathes[i];\r
+            size_t prefix_len = name.find_last_of("/\\");\r
+            if (prefix_len != string::npos) prefix_len++; else prefix_len = 0;\r
+            name = name.substr(prefix_len, name.size() - prefix_len);\r
+            str << "\"" << name << "\";\n";\r
         }\r
     }\r
 \r