ImageGraph.cpp

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00002 
00027 #include <boost/graph/connected_components.hpp>
00028 
00029 #include "ImageGraph.h"
00030 
00031 
00032 namespace HuginBase {
00033 
00034     
00035 
00036 void createCPGraph(const PanoramaData & pano, CPGraph & graph)
00037 {
00038     typedef boost::property_map<CPGraph, boost::vertex_index_t>::type CPGraphIndexMap;
00039     
00040     // clear old graph
00041     graph.clear();
00042 
00043     // add all verticies to the graph
00044     int nImg = pano.getNrOfImages();
00045     for (int i = 0; i < nImg; i++) {
00046         add_vertex(graph);
00047     }
00048 
00049     // insert all control points into the graph
00050     const CPVector & cps = pano.getCtrlPoints();
00051     for (CPVector::const_iterator it = cps.begin(); it != cps.end(); ++it) {
00052         // probably very inefficient
00053         boost::graph_traits<CPGraph>::adjacency_iterator ai;
00054         boost::graph_traits<CPGraph>::adjacency_iterator ai_end;
00055 
00056         CPGraphIndexMap index = get(boost::vertex_index, graph);
00057         bool found=false;
00058         for (boost::tuples::tie(ai, ai_end) = adjacent_vertices(it->image1Nr, graph);
00059              ai != ai_end; ++ai)
00060         {
00061             if (index[*ai] == it->image2Nr) found = true;
00062         }
00063         if (!found) {
00064             add_edge(it->image1Nr, it->image2Nr,graph);
00065         }
00066     }
00067     
00068     // Also connect images with linked yaw, pitch, and roll.
00069     // probably very inefficient
00070     for (unsigned int i = 0; i < nImg; i++)
00071     {
00072         const SrcPanoImage & img_i = pano.getImage(i);
00073         for (unsigned int j = i + 1; j < nImg; j++)
00074         {
00075             const SrcPanoImage & img_j = pano.getImage(j);
00076             if (img_i.YawisLinkedWith(img_j) &&
00077                 img_i.PitchisLinkedWith(img_j) &&
00078                 img_i.RollisLinkedWith(img_j))
00079             {
00080                 // Shared position, therefore should be connected.
00081                 boost::graph_traits<CPGraph>::adjacency_iterator ai;
00082                 boost::graph_traits<CPGraph>::adjacency_iterator ai_end;
00083 
00084                 CPGraphIndexMap index = get(boost::vertex_index, graph);
00085                 bool found=false;
00086                 for (boost::tuples::tie(ai, ai_end) = adjacent_vertices(i, graph);
00087                      ai != ai_end; ++ai)
00088                 {
00089                     if (index[*ai] == j) found = true;
00090                 }
00091                 if (!found) {
00092                     add_edge(i, j, graph);
00093                 }
00094             }
00095         }
00096     }
00097 }
00098 
00099 int findCPComponents(const CPGraph & graph, 
00100                      CPComponents & comp)
00101 {
00102     std::vector<unsigned> component(num_vertices(graph));
00103     unsigned num = boost::connected_components(graph, &component[0]);
00104 
00105     // collect components
00106     comp.clear();
00107     std::set<unsigned> empty;
00108     comp.push_back(empty);
00109     for (unsigned i=0; i < component.size(); i++) {
00110         if (comp.size() < component[i]+1) {
00111             comp.push_back(empty);
00112         }
00113         comp[component[i]].insert(i);
00114     }
00115     return num;
00116 }
00117 
00118 
00119 //typedef boost::property_map<OverlapGraph, boost::vertex_index_t>::type OverlayGraphIndexMap;
00120 
00122 struct OverlapSizeCounter
00123 {
00124     OverlapSizeCounter()
00125         : count(0)
00126     { }
00127 
00128     template<typename PIXEL>
00129     void operator()(PIXEL const & img1, PIXEL const & img2)
00130     {
00131         if (img1 > 0 && img2 > 0) {
00132             count++;
00133         }
00134     }
00135 
00136     int getCount()
00137     {
00138         return count;
00139     }
00140 
00141     int count;
00142 };
00143 
00144 /* not needed so far, probably still buggy
00145 
00146 void PT::createOverlapGraph(const Panorama & pano, OverlapGraph & graph)
00147 {
00148     // clear old graph
00149     graph.clear();
00150 
00151     // add all verticies to the graph
00152     unsigned int nImg = pano.getNrOfImages();
00153     for (unsigned int i = 0; i < nImg; i++) {
00154         add_vertex(graph);
00155     }
00156 
00157     PanoramaOptions opts = pano.getOptions();
00158     // small area, for alpha mask overlap analysis.
00159     opts.width = 500;
00160     // find intersecting regions, on a small version of the panorama.
00161     std::vector< PT::RemappedPanoImage<vigra::BRGBImage, vigra::BImage> > rimg(nImg, PT::RemappedPanoImage<vigra::BRGBImage, vigra::BImage>(pano) );
00162 
00163     for (unsigned int imgNr = 0; imgNr < nImg ; imgNr++) {
00164         // calculate alpha channel
00165         rimg[imgNr].setPanoImage(imgNr, opts);
00166         rimg[imgNr].calcAlpha();
00167     }
00168 
00169     ROI<Diff2D> overlap;
00170     // intersect ROI's & masks of all images
00171     for (unsigned int i1 = 0; i1 < nImg ; i1++) {
00172         for (unsigned int i2 = i1; i2 < nImg ; i2++) {
00173             if ( rimg[i1].getROI().intersect(rimg[i2].getROI(), overlap))
00174             {
00175                 OverlapSizeCounter counter;
00176                 inspectTwoImages(overlap.apply(rimg[i1].getAlpha(),
00177                                                rimg[i1].getROI()),
00178                                  overlap.apply(make_pair(rimg[i2].getAlpha().first, rimg[i2].getAlpha().third),
00179                                                rimg[i2].getROI()),
00180                                  counter);
00181                 if (counter.getCount() > 0) {
00182                     OverlapGraph::Edge e = add_edge(i1, i2, graph);
00183                     // todo: save number of overlapping pixels.
00184                     property_map<OverlapGraph, edge_weight_t>::type w
00185                         = get(edge_weight, g);
00186                     put(w, e, counter.getCount());
00187                 }
00188             }
00189         }
00190     }
00191 }
00192 
00193 */
00194 
00195 } //namespace

Generated on 27 Nov 2014 for Hugintrunk by  doxygen 1.4.7