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/deblurring.hpp" 45 #include "opencv2/videostab/global_motion.hpp" 46 #include "opencv2/videostab/ring_buffer.hpp" 47 48 namespace cv 49 { 50 namespace videostab 51 { 52 53 float calcBlurriness(const Mat &frame) 54 { 55 Mat Gx, Gy; 56 Sobel(frame, Gx, CV_32F, 1, 0); 57 Sobel(frame, Gy, CV_32F, 0, 1); 58 double normGx = norm(Gx); 59 double normGy = norm(Gy); 60 double sumSq = normGx*normGx + normGy*normGy; 61 return static_cast<float>(1. / (sumSq / frame.size().area() + 1e-6)); 62 } 63 64 65 WeightingDeblurer::WeightingDeblurer() 66 { 67 setSensitivity(0.1f); 68 } 69 70 71 void WeightingDeblurer::deblur(int idx, Mat &frame) 72 { 73 CV_Assert(frame.type() == CV_8UC3); 74 75 bSum_.create(frame.size()); 76 gSum_.create(frame.size()); 77 rSum_.create(frame.size()); 78 wSum_.create(frame.size()); 79 80 for (int y = 0; y < frame.rows; ++y) 81 { 82 for (int x = 0; x < frame.cols; ++x) 83 { 84 Point3_<uchar> p = frame.at<Point3_<uchar> >(y,x); 85 bSum_(y,x) = p.x; 86 gSum_(y,x) = p.y; 87 rSum_(y,x) = p.z; 88 wSum_(y,x) = 1.f; 89 } 90 } 91 92 for (int k = idx - radius_; k <= idx + radius_; ++k) 93 { 94 const Mat &neighbor = at(k, *frames_); 95 float bRatio = at(idx, *blurrinessRates_) / at(k, *blurrinessRates_); 96 Mat_<float> M = getMotion(idx, k, *motions_); 97 98 if (bRatio > 1.f) 99 { 100 for (int y = 0; y < frame.rows; ++y) 101 { 102 for (int x = 0; x < frame.cols; ++x) 103 { 104 int x1 = cvRound(M(0,0)*x + M(0,1)*y + M(0,2)); 105 int y1 = cvRound(M(1,0)*x + M(1,1)*y + M(1,2)); 106 107 if (x1 >= 0 && x1 < neighbor.cols && y1 >= 0 && y1 < neighbor.rows) 108 { 109 const Point3_<uchar> &p = frame.at<Point3_<uchar> >(y,x); 110 const Point3_<uchar> &p1 = neighbor.at<Point3_<uchar> >(y1,x1); 111 float w = bRatio * sensitivity_ / 112 (sensitivity_ + std::abs(intensity(p1) - intensity(p))); 113 bSum_(y,x) += w * p1.x; 114 gSum_(y,x) += w * p1.y; 115 rSum_(y,x) += w * p1.z; 116 wSum_(y,x) += w; 117 } 118 } 119 } 120 } 121 } 122 123 for (int y = 0; y < frame.rows; ++y) 124 { 125 for (int x = 0; x < frame.cols; ++x) 126 { 127 float wSumInv = 1.f / wSum_(y,x); 128 frame.at<Point3_<uchar> >(y,x) = Point3_<uchar>( 129 static_cast<uchar>(bSum_(y,x)*wSumInv), 130 static_cast<uchar>(gSum_(y,x)*wSumInv), 131 static_cast<uchar>(rSum_(y,x)*wSumInv)); 132 } 133 } 134 } 135 136 } // namespace videostab 137 } // namespace cv 138