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.hpp"
48
49 #ifndef ENABLE_LOG
50 #define ENABLE_LOG 0
51 #endif
52
53 // TODO remove LOG macros, add logging class
54 #if ENABLE_LOG
55 #ifdef ANDROID
56 #include <iostream>
57 #include <sstream>
58 #include <android/log.h>
59 #define LOG_STITCHING_MSG(msg) \
60 do { \
61 Stringstream _os; \
62 _os << msg; \
63 __android_log_print(ANDROID_LOG_DEBUG, "STITCHING", "%s", _os.str().c_str()); \
64 } while(0);
65 #else
66 #include <iostream>
67 #define LOG_STITCHING_MSG(msg) for(;;) { std::cout << msg; std::cout.flush(); break; }
68 #endif
69 #else
70 #define LOG_STITCHING_MSG(msg)
71 #endif
72
73 #define LOG_(_level, _msg) \
74 for(;;) \
75 { \
76 using namespace std; \
77 if ((_level) >= ::cv::detail::stitchingLogLevel()) \
78 { \
79 LOG_STITCHING_MSG(_msg); \
80 } \
81 break; \
82 }
83
84
85 #define LOG(msg) LOG_(1, msg)
86 #define LOG_CHAT(msg) LOG_(0, msg)
87
88 #define LOGLN(msg) LOG(msg << std::endl)
89 #define LOGLN_CHAT(msg) LOG_CHAT(msg << std::endl)
90
91 //#if DEBUG_LOG_CHAT
92 // #define LOG_CHAT(msg) LOG(msg)
93 // #define LOGLN_CHAT(msg) LOGLN(msg)
94 //#else
95 // #define LOG_CHAT(msg) do{}while(0)
96 // #define LOGLN_CHAT(msg) do{}while(0)
97 //#endif
98
99 namespace cv {
100 namespace detail {
101
102 //! @addtogroup stitching
103 //! @{
104
105 class CV_EXPORTS DisjointSets
106 {
107 public:
DisjointSets(int elem_count=0)108 DisjointSets(int elem_count = 0) { createOneElemSets(elem_count); }
109
110 void createOneElemSets(int elem_count);
111 int findSetByElem(int elem);
112 int mergeSets(int set1, int set2);
113
114 std::vector<int> parent;
115 std::vector<int> size;
116
117 private:
118 std::vector<int> rank_;
119 };
120
121
122 struct CV_EXPORTS GraphEdge
123 {
124 GraphEdge(int from, int to, float weight);
operator <cv::detail::GraphEdge125 bool operator <(const GraphEdge& other) const { return weight < other.weight; }
operator >cv::detail::GraphEdge126 bool operator >(const GraphEdge& other) const { return weight > other.weight; }
127
128 int from, to;
129 float weight;
130 };
131
GraphEdge(int _from,int _to,float _weight)132 inline GraphEdge::GraphEdge(int _from, int _to, float _weight) : from(_from), to(_to), weight(_weight) {}
133
134
135 class CV_EXPORTS Graph
136 {
137 public:
Graph(int num_vertices=0)138 Graph(int num_vertices = 0) { create(num_vertices); }
create(int num_vertices)139 void create(int num_vertices) { edges_.assign(num_vertices, std::list<GraphEdge>()); }
numVertices() const140 int numVertices() const { return static_cast<int>(edges_.size()); }
141 void addEdge(int from, int to, float weight);
142 template <typename B> B forEach(B body) const;
143 template <typename B> B walkBreadthFirst(int from, B body) const;
144
145 private:
146 std::vector< std::list<GraphEdge> > edges_;
147 };
148
149
150 //////////////////////////////////////////////////////////////////////////////
151 // Auxiliary functions
152
153 CV_EXPORTS bool overlapRoi(Point tl1, Point tl2, Size sz1, Size sz2, Rect &roi);
154 CV_EXPORTS Rect resultRoi(const std::vector<Point> &corners, const std::vector<UMat> &images);
155 CV_EXPORTS Rect resultRoi(const std::vector<Point> &corners, const std::vector<Size> &sizes);
156 CV_EXPORTS Rect resultRoiIntersection(const std::vector<Point> &corners, const std::vector<Size> &sizes);
157 CV_EXPORTS Point resultTl(const std::vector<Point> &corners);
158
159 // Returns random 'count' element subset of the {0,1,...,size-1} set
160 CV_EXPORTS void selectRandomSubset(int count, int size, std::vector<int> &subset);
161
162 CV_EXPORTS int& stitchingLogLevel();
163
164 //! @}
165
166 } // namespace detail
167 } // namespace cv
168
169 #include "util_inl.hpp"
170
171 #endif // __OPENCV_STITCHING_UTIL_HPP__
172