Skip to content

Commit bc5d9c5

Browse files
committed
Merge pull request #92 from Bellaktris/inpainting
Inpainting
2 parents f07e304 + 63c308a commit bc5d9c5

File tree

10 files changed

+469
-206
lines changed

10 files changed

+469
-206
lines changed

modules/xphoto/samples/inpainting.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include "opencv2/core/utility.hpp"
77
#include "opencv2/imgproc/types_c.h"
88

9+
#include <ctime>
10+
#include <iostream>
911

1012
const char* keys =
1113
{
@@ -54,9 +56,15 @@ int main( int argc, const char** argv )
5456
printf( "Cannot read image file: %s\n", maskFilename.c_str() );
5557
return -1;
5658
}
59+
cv::threshold(mask, mask, 128, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
5760

5861
cv::Mat res(src.size(), src.type());
62+
63+
int time = clock();
5964
cv::xphoto::inpaint( src, mask, res, cv::xphoto::INPAINT_SHIFTMAP );
65+
std::cout << "time = " << (clock() - time)
66+
/ double(CLOCKS_PER_SEC) << std::endl;
67+
6068
cv::cvtColor(res, res, CV_Lab2RGB);
6169

6270
if ( outFilename == "" )
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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's of source code must retain the above copyright notice,
18+
// this list of conditions and the following disclaimer.
19+
//
20+
// * Redistribution's in binary form must reproduce the above copyright notice,
21+
// this list of conditions and the following disclaimer in the documentation
22+
// and/or other materials provided with the distribution.
23+
//
24+
// * The name of Intel Corporation may not be used to endorse or promote products
25+
// derived from this software without specific prior written permission.
26+
//
27+
// This software is provided by the copyright holders and contributors "as is" and
28+
// any express or implied warranties, including, but not limited to, the implied
29+
// warranties of merchantability and fitness for a particular purpose are disclaimed.
30+
// In no event shall the Intel Corporation or contributors be liable for any direct,
31+
// indirect, incidental, special, exemplary, or consequential damages
32+
// (including, but not limited to, procurement of substitute goods or services;
33+
// loss of use, data, or profits; or business interruption) however caused
34+
// and on any theory of liability, whether in contract, strict liability,
35+
// or tort (including negligence or otherwise) arising in any way out of
36+
// the use of this software, even if advised of the possibility of such damage.
37+
//
38+
//M*/
39+
40+
#ifndef __ADVANCED_TYPES_HPP__
41+
#define __ADVANCED_TYPES_HPP__
42+
#ifdef __cplusplus
43+
44+
#include <opencv2/core.hpp>
45+
46+
/********************* Functions *********************/
47+
48+
namespace cv
49+
{
50+
51+
template <typename _Tp, typename _Tp2> static inline
52+
cv::Size_<_Tp> operator * (const _Tp2 x, const cv::Size_<_Tp> &sz)
53+
{
54+
return cv::Size_<_Tp>(cv::saturate_cast<_Tp>(x*sz.width), cv::saturate_cast<_Tp>(x*sz.height));
55+
}
56+
57+
template <typename _Tp, typename _Tp2> static inline
58+
cv::Size_<_Tp> operator / (const cv::Size_<_Tp> &sz, const _Tp2 x)
59+
{
60+
return cv::Size_<_Tp>(cv::saturate_cast<_Tp>(sz.width/x), cv::saturate_cast<_Tp>(sz.height/x));
61+
}
62+
63+
} // cv
64+
65+
#endif
66+
#endif /* __ADVANCED_TYPES_HPP__ */

modules/xphoto/src/annf.hpp

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@
4040
#ifndef __ANNF_HPP__
4141
#define __ANNF_HPP__
4242

43+
#include <vector>
44+
#include <stack>
45+
#include <limits>
46+
#include <algorithm>
47+
#include <iterator>
48+
#include <iostream>
49+
#include <fstream>
50+
#include <time.h>
51+
#include <functional>
52+
4353
#include "norm2.hpp"
4454
#include "whs.hpp"
4555

@@ -95,11 +105,12 @@ template <typename Tp, int cn> class KDTree
95105
};
96106

97107
template <typename Tp, int cn> int KDTree <Tp, cn>::
98-
getMaxSpreadN(const int _left, const int _right) const
108+
getMaxSpreadN(const int left, const int right) const
99109
{
100-
cv::Vec<Tp, cn> maxValue = data[ idx[_left] ],
101-
minValue = data[ idx[_left] ];
102-
for (int i = _left + 1; i < _right; i += cn)
110+
cv::Vec<Tp, cn> maxValue = data[ idx[left] ],
111+
minValue = data[ idx[left] ];
112+
113+
for (int i = left + 1; i < right; ++i)
103114
for (int j = 0; j < cn; ++j)
104115
{
105116
minValue[j] = std::min( minValue[j], data[idx[i]][j] );
@@ -143,6 +154,7 @@ KDTree(const cv::Mat &img, const int _leafNumber, const int _zeroThresh)
143154
}
144155

145156
int nth = _left + (_right - _left)/2;
157+
146158
int dimIdx = getMaxSpreadN(_left, _right);
147159
KDTreeComparator comp( this, dimIdx );
148160

@@ -168,8 +180,8 @@ updateDist(const int leaf, const int &idx0, int &bestIdx, double &dist)
168180
if (abs(ny - y) < zeroThresh &&
169181
abs(nx - x) < zeroThresh)
170182
continue;
171-
if (nx > width - 1 || nx < 1 ||
172-
ny > height - 1 || ny > 1 )
183+
if (nx >= width - 1 || nx < 1 ||
184+
ny >= height - 1 || ny < 1 )
173185
continue;
174186

175187
double ndist = norm2(data[idx0], data[idx[k]]);
@@ -184,9 +196,12 @@ updateDist(const int leaf, const int &idx0, int &bestIdx, double &dist)
184196

185197
/************************** ANNF search **************************/
186198

187-
static void dominantTransforms(const cv::Mat &img, std::vector <cv::Matx33f> &transforms,
199+
static void dominantTransforms(const cv::Mat &img, std::vector <cv::Point2i> &transforms,
188200
const int nTransform, const int psize)
189201
{
202+
const int zeroThresh = 2*psize;
203+
const int leafNum = 64;
204+
190205
/** Walsh-Hadamard Transformation **/
191206

192207
std::vector <cv::Mat> channels;
@@ -204,7 +219,7 @@ static void dominantTransforms(const cv::Mat &img, std::vector <cv::Matx33f> &tr
204219
cv::Mat whs; // Walsh-Hadamard series
205220
cv::merge(channels, whs);
206221

207-
KDTree <float, 24> kdTree(whs, 16, 32);
222+
KDTree <float, 24> kdTree(whs, leafNum, zeroThresh);
208223
std::vector <int> annf( whs.total(), 0 );
209224

210225
/** Propagation-assisted kd-tree search **/
@@ -217,13 +232,13 @@ static void dominantTransforms(const cv::Mat &img, std::vector <cv::Matx33f> &tr
217232

218233
int dy[] = {0, 1, 0}, dx[] = {0, 0, 1};
219234
for (int k = 0; k < int( sizeof(dy)/sizeof(int) ); ++k)
220-
if (i - dy[k] >= 0 && j - dx[k] >= 0)
235+
if ( i - dy[k] >= 0 && j - dx[k] >= 0 )
221236
{
222237
int neighbor = (i - dy[k])*whs.cols + (j - dx[k]);
223-
int leafIdx = k == 0 ? neighbor :
224-
annf[neighbor] + dy[k]*whs.cols + dx[k];
238+
int leafIdx = (dx[k] == 0 && dy[k] == 0)
239+
? neighbor : annf[neighbor] + dy[k]*whs.cols + dx[k];
225240
kdTree.updateDist(leafIdx, current,
226-
annf[i*whs.cols + j], dist);
241+
annf[i*whs.cols + j], dist);
227242
}
228243
}
229244

@@ -232,8 +247,8 @@ static void dominantTransforms(const cv::Mat &img, std::vector <cv::Matx33f> &tr
232247
cv::Mat_<double> annfHist(2*whs.rows - 1, 2*whs.cols - 1, 0.0),
233248
_annfHist(2*whs.rows - 1, 2*whs.cols - 1, 0.0);
234249
for (size_t i = 0; i < annf.size(); ++i)
235-
++annfHist( (annf[i] - int(i))/whs.cols + whs.rows - 1,
236-
(annf[i] - int(i))%whs.cols + whs.cols - 1 );
250+
++annfHist( annf[i]/whs.cols - int(i)/whs.cols + whs.rows - 1,
251+
annf[i]%whs.cols - int(i)%whs.cols + whs.cols - 1 );
237252

238253
cv::GaussianBlur( annfHist, annfHist,
239254
cv::Size(0, 0), std::sqrt(2.0), 0.0, cv::BORDER_CONSTANT);
@@ -264,9 +279,7 @@ static void dominantTransforms(const cv::Mat &img, std::vector <cv::Matx33f> &tr
264279
for (int i = 0; i < nTransform; ++i)
265280
{
266281
int idx = amount[i].second;
267-
transforms[i] = cv::Matx33f(1, 0, float(shiftM[idx].x),
268-
0, 1, float(shiftM[idx].y),
269-
0, 0, 1 );
282+
transforms[i] = cv::Point2i( shiftM[idx].x, shiftM[idx].y );
270283
}
271284
}
272285

modules/xphoto/src/blending.hpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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's of source code must retain the above copyright notice,
18+
// this list of conditions and the following disclaimer.
19+
//
20+
// * Redistribution's in binary form must reproduce the above copyright notice,
21+
// this list of conditions and the following disclaimer in the documentation
22+
// and/or other materials provided with the distribution.
23+
//
24+
// * The name of Intel Corporation may not be used to endorse or promote products
25+
// derived from this software without specific prior written permission.
26+
//
27+
// This software is provided by the copyright holders and contributors "as is" and
28+
// any express or implied warranties, including, but not limited to, the implied
29+
// warranties of merchantability and fitness for a particular purpose are disclaimed.
30+
// In no event shall the Intel Corporation or contributors be liable for any direct,
31+
// indirect, incidental, special, exemplary, or consequential damages
32+
// (including, but not limited to, procurement of substitute goods or services;
33+
// loss of use, data, or profits; or business interruption) however caused
34+
// and on any theory of liability, whether in contract, strict liability,
35+
// or tort (including negligence or otherwise) arising in any way out of
36+
// the use of this software, even if advised of the possibility of such damage.
37+
//
38+
//M*/
39+
40+
#ifndef __BLENDING_HPP__
41+
#define __BLENDING_HPP__
42+
43+
44+
45+
#endif /* __BLENDING_HPP__ */

modules/xphoto/src/gcgraph.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ TWeight GCGraph<TWeight>::maxFlow()
254254
minWeight = MIN(minWeight, weight);
255255
CV_Assert( minWeight > 0 );
256256
}
257-
weight = fabs(v->weight);
257+
weight = abs( TWeight(v->weight) );
258258
minWeight = MIN(minWeight, weight);
259259
CV_Assert( minWeight > 0 );
260260
}

0 commit comments

Comments
 (0)