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 #include "precomp.hpp" 44 45 namespace cv { 46 namespace detail { 47 48 void DisjointSets::createOneElemSets(int n) 49 { 50 rank_.assign(n, 0); 51 size.assign(n, 1); 52 parent.resize(n); 53 for (int i = 0; i < n; ++i) 54 parent[i] = i; 55 } 56 57 58 int DisjointSets::findSetByElem(int elem) 59 { 60 int set = elem; 61 while (set != parent[set]) 62 set = parent[set]; 63 int next; 64 while (elem != parent[elem]) 65 { 66 next = parent[elem]; 67 parent[elem] = set; 68 elem = next; 69 } 70 return set; 71 } 72 73 74 int DisjointSets::mergeSets(int set1, int set2) 75 { 76 if (rank_[set1] < rank_[set2]) 77 { 78 parent[set1] = set2; 79 size[set2] += size[set1]; 80 return set2; 81 } 82 if (rank_[set2] < rank_[set1]) 83 { 84 parent[set2] = set1; 85 size[set1] += size[set2]; 86 return set1; 87 } 88 parent[set1] = set2; 89 rank_[set2]++; 90 size[set2] += size[set1]; 91 return set2; 92 } 93 94 95 void Graph::addEdge(int from, int to, float weight) 96 { 97 edges_[from].push_back(GraphEdge(from, to, weight)); 98 } 99 100 101 bool overlapRoi(Point tl1, Point tl2, Size sz1, Size sz2, Rect &roi) 102 { 103 int x_tl = std::max(tl1.x, tl2.x); 104 int y_tl = std::max(tl1.y, tl2.y); 105 int x_br = std::min(tl1.x + sz1.width, tl2.x + sz2.width); 106 int y_br = std::min(tl1.y + sz1.height, tl2.y + sz2.height); 107 if (x_tl < x_br && y_tl < y_br) 108 { 109 roi = Rect(x_tl, y_tl, x_br - x_tl, y_br - y_tl); 110 return true; 111 } 112 return false; 113 } 114 115 116 Rect resultRoi(const std::vector<Point> &corners, const std::vector<UMat> &images) 117 { 118 std::vector<Size> sizes(images.size()); 119 for (size_t i = 0; i < images.size(); ++i) 120 sizes[i] = images[i].size(); 121 return resultRoi(corners, sizes); 122 } 123 124 125 Rect resultRoi(const std::vector<Point> &corners, const std::vector<Size> &sizes) 126 { 127 CV_Assert(sizes.size() == corners.size()); 128 Point tl(std::numeric_limits<int>::max(), std::numeric_limits<int>::max()); 129 Point br(std::numeric_limits<int>::min(), std::numeric_limits<int>::min()); 130 for (size_t i = 0; i < corners.size(); ++i) 131 { 132 tl.x = std::min(tl.x, corners[i].x); 133 tl.y = std::min(tl.y, corners[i].y); 134 br.x = std::max(br.x, corners[i].x + sizes[i].width); 135 br.y = std::max(br.y, corners[i].y + sizes[i].height); 136 } 137 return Rect(tl, br); 138 } 139 140 Rect resultRoiIntersection(const std::vector<Point> &corners, const std::vector<Size> &sizes) 141 { 142 CV_Assert(sizes.size() == corners.size()); 143 Point tl(std::numeric_limits<int>::min(), std::numeric_limits<int>::min()); 144 Point br(std::numeric_limits<int>::max(), std::numeric_limits<int>::max()); 145 for (size_t i = 0; i < corners.size(); ++i) 146 { 147 tl.x = std::max(tl.x, corners[i].x); 148 tl.y = std::max(tl.y, corners[i].y); 149 br.x = std::min(br.x, corners[i].x + sizes[i].width); 150 br.y = std::min(br.y, corners[i].y + sizes[i].height); 151 } 152 return Rect(tl, br); 153 } 154 155 156 Point resultTl(const std::vector<Point> &corners) 157 { 158 Point tl(std::numeric_limits<int>::max(), std::numeric_limits<int>::max()); 159 for (size_t i = 0; i < corners.size(); ++i) 160 { 161 tl.x = std::min(tl.x, corners[i].x); 162 tl.y = std::min(tl.y, corners[i].y); 163 } 164 return tl; 165 } 166 167 168 void selectRandomSubset(int count, int size, std::vector<int> &subset) 169 { 170 subset.clear(); 171 for (int i = 0; i < size; ++i) 172 { 173 if (randu<int>() % (size - i) < count) 174 { 175 subset.push_back(i); 176 count--; 177 } 178 } 179 } 180 181 int& stitchingLogLevel() 182 { 183 static int _log_level=1; 184 return _log_level; 185 } 186 187 } // namespace detail 188 } // namespace cv 189