Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/videostab/src/deblurring.cpp
16354 views
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
CV_INSTRUMENT_REGION();
56
57
Mat Gx, Gy;
58
Sobel(frame, Gx, CV_32F, 1, 0);
59
Sobel(frame, Gy, CV_32F, 0, 1);
60
double normGx = norm(Gx);
61
double normGy = norm(Gy);
62
double sumSq = normGx*normGx + normGy*normGy;
63
return static_cast<float>(1. / (sumSq / frame.size().area() + 1e-6));
64
}
65
66
67
WeightingDeblurer::WeightingDeblurer()
68
{
69
setSensitivity(0.1f);
70
}
71
72
73
void WeightingDeblurer::deblur(int idx, Mat &frame)
74
{
75
CV_INSTRUMENT_REGION();
76
77
CV_Assert(frame.type() == CV_8UC3);
78
79
bSum_.create(frame.size());
80
gSum_.create(frame.size());
81
rSum_.create(frame.size());
82
wSum_.create(frame.size());
83
84
for (int y = 0; y < frame.rows; ++y)
85
{
86
for (int x = 0; x < frame.cols; ++x)
87
{
88
Point3_<uchar> p = frame.at<Point3_<uchar> >(y,x);
89
bSum_(y,x) = p.x;
90
gSum_(y,x) = p.y;
91
rSum_(y,x) = p.z;
92
wSum_(y,x) = 1.f;
93
}
94
}
95
96
for (int k = idx - radius_; k <= idx + radius_; ++k)
97
{
98
const Mat &neighbor = at(k, *frames_);
99
float bRatio = at(idx, *blurrinessRates_) / at(k, *blurrinessRates_);
100
Mat_<float> M = getMotion(idx, k, *motions_);
101
102
if (bRatio > 1.f)
103
{
104
for (int y = 0; y < frame.rows; ++y)
105
{
106
for (int x = 0; x < frame.cols; ++x)
107
{
108
int x1 = cvRound(M(0,0)*x + M(0,1)*y + M(0,2));
109
int y1 = cvRound(M(1,0)*x + M(1,1)*y + M(1,2));
110
111
if (x1 >= 0 && x1 < neighbor.cols && y1 >= 0 && y1 < neighbor.rows)
112
{
113
const Point3_<uchar> &p = frame.at<Point3_<uchar> >(y,x);
114
const Point3_<uchar> &p1 = neighbor.at<Point3_<uchar> >(y1,x1);
115
float w = bRatio * sensitivity_ /
116
(sensitivity_ + std::abs(intensity(p1) - intensity(p)));
117
bSum_(y,x) += w * p1.x;
118
gSum_(y,x) += w * p1.y;
119
rSum_(y,x) += w * p1.z;
120
wSum_(y,x) += w;
121
}
122
}
123
}
124
}
125
}
126
127
for (int y = 0; y < frame.rows; ++y)
128
{
129
for (int x = 0; x < frame.cols; ++x)
130
{
131
float wSumInv = 1.f / wSum_(y,x);
132
frame.at<Point3_<uchar> >(y,x) = Point3_<uchar>(
133
static_cast<uchar>(bSum_(y,x)*wSumInv),
134
static_cast<uchar>(gSum_(y,x)*wSumInv),
135
static_cast<uchar>(rSum_(y,x)*wSumInv));
136
}
137
}
138
}
139
140
} // namespace videostab
141
} // namespace cv
142
143