include/opencv2/stitching/detail/util.hpp
Go to the documentation of this file.
00001 /*M///////////////////////////////////////////////////////////////////////////////////////
00002 //
00003 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
00004 //
00005 //  By downloading, copying, installing or using the software you agree to this license.
00006 //  If you do not agree to this license, do not download, install,
00007 //  copy or use the software.
00008 //
00009 //
00010 //                          License Agreement
00011 //                For Open Source Computer Vision Library
00012 //
00013 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
00014 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
00015 // Third party copyrights are property of their respective owners.
00016 //
00017 // Redistribution and use in source and binary forms, with or without modification,
00018 // are permitted provided that the following conditions are met:
00019 //
00020 //   * Redistribution's of source code must retain the above copyright notice,
00021 //     this list of conditions and the following disclaimer.
00022 //
00023 //   * Redistribution's in binary form must reproduce the above copyright notice,
00024 //     this list of conditions and the following disclaimer in the documentation
00025 //     and/or other materials provided with the distribution.
00026 //
00027 //   * The name of the copyright holders may not be used to endorse or promote products
00028 //     derived from this software without specific prior written permission.
00029 //
00030 // This software is provided by the copyright holders and contributors "as is" and
00031 // any express or implied warranties, including, but not limited to, the implied
00032 // warranties of merchantability and fitness for a particular purpose are disclaimed.
00033 // In no event shall the Intel Corporation or contributors be liable for any direct,
00034 // indirect, incidental, special, exemplary, or consequential damages
00035 // (including, but not limited to, procurement of substitute goods or services;
00036 // loss of use, data, or profits; or business interruption) however caused
00037 // and on any theory of liability, whether in contract, strict liability,
00038 // or tort (including negligence or otherwise) arising in any way out of
00039 // the use of this software, even if advised of the possibility of such damage.
00040 //
00041 //M*/
00042 
00043 #ifndef __OPENCV_STITCHING_UTIL_HPP__
00044 #define __OPENCV_STITCHING_UTIL_HPP__
00045 
00046 #include <list>
00047 #include "opencv2/core/core.hpp"
00048 
00049 #define ENABLE_LOG 0
00050 
00051 // TODO remove LOG macros, add logging class
00052 #if ENABLE_LOG
00053 #ifdef ANDROID
00054   #include <iostream>
00055   #include <sstream>
00056   #include <android/log.h>
00057   #define LOG_STITCHING_MSG(msg) \
00058     do { \
00059         std::stringstream _os; \
00060         _os << msg; \
00061        __android_log_print(ANDROID_LOG_DEBUG, "STITCHING", "%s", _os.str().c_str()); \
00062     } while(0);
00063 #else
00064   #include <iostream>
00065   #define LOG_STITCHING_MSG(msg) for(;;) { std::cout << msg; std::cout.flush(); break; }
00066 #endif
00067 #else
00068   #define LOG_STITCHING_MSG(msg)
00069 #endif
00070 
00071 #define LOG_(_level, _msg)                     \
00072     for(;;)                                    \
00073     {                                          \
00074         if ((_level) >= ::cv::detail::stitchingLogLevel()) \
00075         {                                      \
00076             LOG_STITCHING_MSG(_msg);           \
00077         }                                      \
00078     break;                                 \
00079     }
00080 
00081 
00082 #define LOG(msg) LOG_(1, msg)
00083 #define LOG_CHAT(msg) LOG_(0, msg)
00084 
00085 #define LOGLN(msg) LOG(msg << std::endl)
00086 #define LOGLN_CHAT(msg) LOG_CHAT(msg << std::endl)
00087 
00088 //#if DEBUG_LOG_CHAT
00089 //  #define LOG_CHAT(msg) LOG(msg)
00090 //  #define LOGLN_CHAT(msg) LOGLN(msg)
00091 //#else
00092 //  #define LOG_CHAT(msg) do{}while(0)
00093 //  #define LOGLN_CHAT(msg) do{}while(0)
00094 //#endif
00095 
00096 namespace cv {
00097 namespace detail {
00098 
00099 class CV_EXPORTS DisjointSets
00100 {
00101 public:
00102     DisjointSets(int elem_count = 0) { createOneElemSets(elem_count); }
00103 
00104     void createOneElemSets(int elem_count);
00105     int findSetByElem(int elem);
00106     int mergeSets(int set1, int set2);
00107 
00108     std::vector<int> parent;
00109     std::vector<int> size;
00110 
00111 private:
00112     std::vector<int> rank_;
00113 };
00114 
00115 
00116 struct CV_EXPORTS GraphEdge
00117 {
00118     GraphEdge(int from, int to, float weight);
00119     bool operator <(const GraphEdge& other) const { return weight < other.weight; }
00120     bool operator >(const GraphEdge& other) const { return weight > other.weight; }
00121 
00122     int from, to;
00123     float weight;
00124 };
00125 
00126 inline GraphEdge::GraphEdge(int _from, int _to, float _weight) : from(_from), to(_to), weight(_weight) {}
00127 
00128 
00129 class CV_EXPORTS Graph
00130 {
00131 public:
00132     Graph(int num_vertices = 0) { create(num_vertices); }
00133     void create(int num_vertices) { edges_.assign(num_vertices, std::list<GraphEdge>()); }
00134     int numVertices() const { return static_cast<int>(edges_.size()); }
00135     void addEdge(int from, int to, float weight);
00136     template <typename B> B forEach(B body) const;
00137     template <typename B> B walkBreadthFirst(int from, B body) const;
00138 
00139 private:
00140     std::vector< std::list<GraphEdge> > edges_;
00141 };
00142 
00143 
00145 // Auxiliary functions
00146 
00147 CV_EXPORTS bool overlapRoi(Point tl1, Point tl2, Size sz1, Size sz2, Rect &roi);
00148 CV_EXPORTS Rect resultRoi(const std::vector<Point> &corners, const std::vector<Mat> &images);
00149 CV_EXPORTS Rect resultRoi(const std::vector<Point> &corners, const std::vector<Size> &sizes);
00150 CV_EXPORTS Point resultTl(const std::vector<Point> &corners);
00151 
00152 // Returns random 'count' element subset of the {0,1,...,size-1} set
00153 CV_EXPORTS void selectRandomSubset(int count, int size, std::vector<int> &subset);
00154 
00155 CV_EXPORTS int& stitchingLogLevel();
00156 
00157 } // namespace detail
00158 } // namespace cv
00159 
00160 #include "util_inl.hpp"
00161 
00162 #endif // __OPENCV_STITCHING_UTIL_HPP__