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-2011, 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 #include "opencv2/videostab/outlier_rejection.hpp" 45 46 namespace cv 47 { 48 namespace videostab 49 { 50 51 void NullOutlierRejector::process( 52 Size /*frameSize*/, InputArray points0, InputArray points1, OutputArray mask) 53 { 54 CV_Assert(points0.type() == points1.type()); 55 CV_Assert(points0.getMat().checkVector(2) == points1.getMat().checkVector(2)); 56 57 int npoints = points0.getMat().checkVector(2); 58 mask.create(1, npoints, CV_8U); 59 Mat mask_ = mask.getMat(); 60 mask_.setTo(1); 61 } 62 63 TranslationBasedLocalOutlierRejector::TranslationBasedLocalOutlierRejector() 64 { 65 setCellSize(Size(50, 50)); 66 setRansacParams(RansacParams::default2dMotion(MM_TRANSLATION)); 67 } 68 69 70 void TranslationBasedLocalOutlierRejector::process( 71 Size frameSize, InputArray points0, InputArray points1, OutputArray mask) 72 { 73 CV_Assert(points0.type() == points1.type()); 74 CV_Assert(points0.getMat().checkVector(2) == points1.getMat().checkVector(2)); 75 76 int npoints = points0.getMat().checkVector(2); 77 78 const Point2f* points0_ = points0.getMat().ptr<Point2f>(); 79 const Point2f* points1_ = points1.getMat().ptr<Point2f>(); 80 81 mask.create(1, npoints, CV_8U); 82 uchar* mask_ = mask.getMat().ptr<uchar>(); 83 84 Size ncells((frameSize.width + cellSize_.width - 1) / cellSize_.width, 85 (frameSize.height + cellSize_.height - 1) / cellSize_.height); 86 87 int cx, cy; 88 89 // fill grid cells 90 91 grid_.assign(ncells.area(), Cell()); 92 93 for (int i = 0; i < npoints; ++i) 94 { 95 cx = std::min(cvRound(points0_[i].x / cellSize_.width), ncells.width - 1); 96 cy = std::min(cvRound(points0_[i].y / cellSize_.height), ncells.height - 1); 97 grid_[cy * ncells.width + cx].push_back(i); 98 } 99 100 // process each cell 101 102 RNG rng(0); 103 int niters = ransacParams_.niters(); 104 int ninliers, ninliersMax; 105 std::vector<int> inliers; 106 float dx, dy, dxBest, dyBest; 107 float x1, y1; 108 int idx; 109 110 for (size_t ci = 0; ci < grid_.size(); ++ci) 111 { 112 // estimate translation model at the current cell using RANSAC 113 114 const Cell &cell = grid_[ci]; 115 ninliersMax = 0; 116 dxBest = dyBest = 0.f; 117 118 // find the best hypothesis 119 120 if (!cell.empty()) 121 { 122 for (int iter = 0; iter < niters; ++iter) 123 { 124 idx = cell[static_cast<unsigned>(rng) % cell.size()]; 125 dx = points1_[idx].x - points0_[idx].x; 126 dy = points1_[idx].y - points0_[idx].y; 127 128 ninliers = 0; 129 for (size_t i = 0; i < cell.size(); ++i) 130 { 131 x1 = points0_[cell[i]].x + dx; 132 y1 = points0_[cell[i]].y + dy; 133 if (sqr(x1 - points1_[cell[i]].x) + sqr(y1 - points1_[cell[i]].y) < 134 sqr(ransacParams_.thresh)) 135 { 136 ninliers++; 137 } 138 } 139 140 if (ninliers > ninliersMax) 141 { 142 ninliersMax = ninliers; 143 dxBest = dx; 144 dyBest = dy; 145 } 146 } 147 } 148 149 // get the best hypothesis inliers 150 151 ninliers = 0; 152 inliers.resize(ninliersMax); 153 for (size_t i = 0; i < cell.size(); ++i) 154 { 155 x1 = points0_[cell[i]].x + dxBest; 156 y1 = points0_[cell[i]].y + dyBest; 157 if (sqr(x1 - points1_[cell[i]].x) + sqr(y1 - points1_[cell[i]].y) < 158 sqr(ransacParams_.thresh)) 159 { 160 inliers[ninliers++] = cell[i]; 161 } 162 } 163 164 // refine the best hypothesis 165 166 dxBest = dyBest = 0.f; 167 for (size_t i = 0; i < inliers.size(); ++i) 168 { 169 dxBest += points1_[inliers[i]].x - points0_[inliers[i]].x; 170 dyBest += points1_[inliers[i]].y - points0_[inliers[i]].y; 171 } 172 if (!inliers.empty()) 173 { 174 dxBest /= inliers.size(); 175 dyBest /= inliers.size(); 176 } 177 178 // set mask elements for refined model inliers 179 180 for (size_t i = 0; i < cell.size(); ++i) 181 { 182 x1 = points0_[cell[i]].x + dxBest; 183 y1 = points0_[cell[i]].y + dyBest; 184 if (sqr(x1 - points1_[cell[i]].x) + sqr(y1 - points1_[cell[i]].y) < 185 sqr(ransacParams_.thresh)) 186 { 187 mask_[cell[i]] = 1; 188 } 189 else 190 { 191 mask_[cell[i]] = 0; 192 } 193 } 194 } 195 } 196 197 } // namespace videostab 198 } // namespace cv 199