util.hpp
Go to the documentation of this file.
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
8 //
9 //
10 // License Agreement
11 // For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
22 //
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
26 //
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42 
43 #ifndef __OPENCV_STITCHING_UTIL_HPP__
44 #define __OPENCV_STITCHING_UTIL_HPP__
45 
46 #include <list>
47 #include "opencv2/core/core.hpp"
48 
49 #define ENABLE_LOG 0
50 
51 // TODO remove LOG macros, add logging class
52 #if ENABLE_LOG
53 #ifdef ANDROID
54  #include <iostream>
55  #include <sstream>
56  #include <android/log.h>
57  #define LOG_STITCHING_MSG(msg) \
58  do { \
59  std::stringstream _os; \
60  _os << msg; \
61  __android_log_print(ANDROID_LOG_DEBUG, "STITCHING", "%s", _os.str().c_str()); \
62  } while(0);
63 #else
64  #include <iostream>
65  #define LOG_STITCHING_MSG(msg) for(;;) { std::cout << msg; std::cout.flush(); break; }
66 #endif
67 #else
68  #define LOG_STITCHING_MSG(msg)
69 #endif
70 
71 #define LOG_(_level, _msg) \
72  for(;;) \
73  { \
74  if ((_level) >= ::cv::detail::stitchingLogLevel()) \
75  { \
76  LOG_STITCHING_MSG(_msg); \
77  } \
78  break; \
79  }
80 
81 
82 #define LOG(msg) LOG_(1, msg)
83 #define LOG_CHAT(msg) LOG_(0, msg)
84 
85 #define LOGLN(msg) LOG(msg << std::endl)
86 #define LOGLN_CHAT(msg) LOG_CHAT(msg << std::endl)
87 
88 //#if DEBUG_LOG_CHAT
89 // #define LOG_CHAT(msg) LOG(msg)
90 // #define LOGLN_CHAT(msg) LOGLN(msg)
91 //#else
92 // #define LOG_CHAT(msg) do{}while(0)
93 // #define LOGLN_CHAT(msg) do{}while(0)
94 //#endif
95 
96 namespace cv {
97 namespace detail {
98 
99 class CV_EXPORTS DisjointSets
100 {
101 public:
102  DisjointSets(int elem_count = 0) { createOneElemSets(elem_count); }
103 
104  void createOneElemSets(int elem_count);
105  int findSetByElem(int elem);
106  int mergeSets(int set1, int set2);
107 
108  std::vector<int> parent;
109  std::vector<int> size;
110 
111 private:
112  std::vector<int> rank_;
113 };
114 
115 
116 struct CV_EXPORTS GraphEdge
117 {
118  GraphEdge(int from, int to, float weight);
119  bool operator <(const GraphEdge& other) const { return weight < other.weight; }
120  bool operator >(const GraphEdge& other) const { return weight > other.weight; }
121 
122  int from, to;
123  float weight;
124 };
125 
126 inline GraphEdge::GraphEdge(int _from, int _to, float _weight) : from(_from), to(_to), weight(_weight) {}
127 
128 
129 class CV_EXPORTS Graph
130 {
131 public:
132  Graph(int num_vertices = 0) { create(num_vertices); }
133  void create(int num_vertices) { edges_.assign(num_vertices, std::list<GraphEdge>()); }
134  int numVertices() const { return static_cast<int>(edges_.size()); }
135  void addEdge(int from, int to, float weight);
136  template <typename B> B forEach(B body) const;
137  template <typename B> B walkBreadthFirst(int from, B body) const;
138 
139 private:
140  std::vector< std::list<GraphEdge> > edges_;
141 };
142 
143 
145 // Auxiliary functions
146 
147 CV_EXPORTS bool overlapRoi(Point tl1, Point tl2, Size sz1, Size sz2, Rect &roi);
148 CV_EXPORTS Rect resultRoi(const std::vector<Point> &corners, const std::vector<Mat> &images);
149 CV_EXPORTS Rect resultRoi(const std::vector<Point> &corners, const std::vector<Size> &sizes);
150 CV_EXPORTS Point resultTl(const std::vector<Point> &corners);
151 
152 // Returns random 'count' element subset of the {0,1,...,size-1} set
153 CV_EXPORTS void selectRandomSubset(int count, int size, std::vector<int> &subset);
154 
155 CV_EXPORTS int& stitchingLogLevel();
156 
157 } // namespace detail
158 } // namespace cv
159 
160 #include "util_inl.hpp"
161 
162 #endif // __OPENCV_STITCHING_UTIL_HPP__
Definition: util.hpp:116
GraphEdge(int from, int to, float weight)
Definition: util.hpp:126
const int * sizes
Definition: core_c.h:212
IplImage CvRect * roi
Definition: legacy.hpp:234
CV_EXPORTS Rect resultRoi(const std::vector< Point > &corners, const std::vector< Mat > &images)
const void * elem
Definition: core_c.h:1075
CV_EXPORTS MatExpr operator>(const Mat &a, const Mat &b)
CV_EXPORTS bool overlapRoi(Point tl1, Point tl2, Size sz1, Size sz2, Rect &roi)
Definition: util.hpp:99
The 2D size class.
Definition: core.hpp:81
CV_EXPORTS int & stitchingLogLevel()
void create(int num_vertices)
Definition: util.hpp:133
CV_EXPORTS Point resultTl(const std::vector< Point > &corners)
DisjointSets(int elem_count=0)
Definition: util.hpp:102
int numVertices() const
Definition: util.hpp:134
GLuint GLuint GLsizei count
Definition: core_c.h:973
Definition: util.hpp:129
Graph(int num_vertices=0)
Definition: util.hpp:132
std::vector< int > size
Definition: util.hpp:109
CV_EXPORTS MatExpr operator<(const Mat &a, const Mat &b)
The Core Functionality.
CvSize CvPoint2D32f * corners
Definition: calib3d.hpp:215
template 2D point class.
Definition: core.hpp:82
CV_EXPORTS void selectRandomSubset(int count, int size, std::vector< int > &subset)
int to
Definition: util.hpp:122
const CvMat * B
Definition: calib3d.hpp:161
std::vector< int > parent
Definition: util.hpp:108
float weight
Definition: util.hpp:123
GLsizeiptr size
Definition: core_c.h:939