inpainting.cpp 16.4 KB
Newer Older
Bellaktris's avatar
Bellaktris committed
1 2 3 4 5 6 7 8 9 10 11
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
12
//                       (3-clause BSD License)
Bellaktris's avatar
Bellaktris committed
13
//
14
// Copyright (C) 2000-2019, Intel Corporation, all rights reserved.
Bellaktris's avatar
Bellaktris committed
15
// Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
16 17 18 19
// Copyright (C) 2009-2016, NVIDIA Corporation, all rights reserved.
// Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
// Copyright (C) 2015-2016, OpenCV Foundation, all rights reserved.
// Copyright (C) 2015-2016, Itseez Inc., all rights reserved.
Bellaktris's avatar
Bellaktris committed
20 21
// Third party copyrights are property of their respective owners.
//
22 23 24
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
Bellaktris's avatar
Bellaktris committed
25 26 27 28 29 30 31
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
32 33 34
//   * Neither the names of the copyright holders nor the names of the contributors
//    may be used to endorse or promote products derived from this software
//    without specific prior written permission.
Bellaktris's avatar
Bellaktris committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#include <vector>
50
#include <stack>
Bellaktris's avatar
Bellaktris committed
51
#include <limits>
Bellaktris's avatar
Bellaktris committed
52 53 54
#include <algorithm>
#include <iterator>
#include <iostream>
Bellaktris's avatar
Bellaktris committed
55
#include <fstream>
Bellaktris's avatar
Bellaktris committed
56
#include <time.h>
57
#include <functional>
58 59
#include <string>
#include <tuple>
Bellaktris's avatar
Bellaktris committed
60 61

#include "opencv2/xphoto.hpp"
62

Bellaktris's avatar
Bellaktris committed
63
#include "opencv2/imgproc.hpp"
64
#include "opencv2/imgproc/imgproc_c.h"
Bellaktris's avatar
Bellaktris committed
65 66 67 68 69 70 71

#include "opencv2/core.hpp"
#include "opencv2/core/core_c.h"

#include "opencv2/core/types.hpp"
#include "opencv2/core/types_c.h"

72 73 74
#include "photomontage.hpp"
#include "annf.hpp"
#include "advanced_types.hpp"
Bellaktris's avatar
Bellaktris committed
75

76 77
#include "inpainting_fsr.impl.hpp"

78 79
namespace cv
{
80 81
namespace xphoto
{
82
    template <typename Tp, unsigned int cn>
83 84
    static void shiftMapInpaint( const Mat &_src, const Mat &_mask, Mat &dst,
        const int nTransform = 60, const int psize = 8, const cv::Point2i dsize = cv::Point2i(800, 600) )
Bellaktris's avatar
Bellaktris committed
85
    {
86
        /** Preparing input **/
87 88 89 90 91 92 93 94 95 96 97
        cv::Mat src, mask, img, dmask, ddmask;

        const float ls = std::max(/**/ std::min( /*...*/
            std::max(_src.rows, _src.cols)/float(dsize.x),
            std::min(_src.rows, _src.cols)/float(dsize.y)
                                               ), 1.0f /**/);


        cv::resize(_mask, mask, _mask.size()/ls, 0, 0, cv::INTER_NEAREST);
        cv::resize(_src,  src,  _src.size()/ls,  0, 0,    cv::INTER_AREA);

98
        src.convertTo( img, CV_32F );
99
        img.setTo(0, ~(mask > 0));
100

101 102
        cv::erode( mask,  dmask, cv::Mat(), cv::Point(-1,-1), 2);
        cv::erode(dmask, ddmask, cv::Mat(), cv::Point(-1,-1), 2);
Bellaktris's avatar
Bellaktris committed
103

104 105
        std::vector <Point2i> pPath;
        cv::Mat_<int> backref( ddmask.size(), int(-1) );
106

107 108 109 110 111 112 113 114 115 116 117 118
        for (int i = 0; i < ddmask.rows; ++i)
        {
            uchar *dmask_data = (uchar *) ddmask.template ptr<uchar>(i);
            int *backref_data = (int *) backref.template ptr< int >(i);

            for (int j = 0; j < ddmask.cols; ++j)
                if (dmask_data[j] == 0)
                {
                    backref_data[j] = int(pPath.size());
                     pPath.push_back( cv::Point(j, i) );
                }
        }
Bellaktris's avatar
Bellaktris committed
119

120 121 122 123
        /** ANNF computation **/
        std::vector <cv::Point2i> transforms( nTransform );
        dominantTransforms(img, transforms, nTransform, psize);
        transforms.push_back( cv::Point2i(0, 0) );
Bellaktris's avatar
Bellaktris committed
124

125 126 127 128 129
        /** Warping **/
        std::vector <std::vector <cv::Vec <float, cn> > > pointSeq( pPath.size() ); // source image transformed with transforms
        std::vector <int> labelSeq( pPath.size() );                                 // resulting label sequence
        std::vector <std::vector <int> >  linkIdx( pPath.size() );                  // neighbor links for pointSeq elements
        std::vector <std::vector <unsigned char > > maskSeq( pPath.size() );        // corresponding mask
Bellaktris's avatar
Bellaktris committed
130

131 132
        for (size_t i = 0; i < pPath.size(); ++i)
        {
Bellaktris's avatar
Bellaktris committed
133 134
            uchar xmask = dmask.template at<uchar>(pPath[i]);

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
            for (int j = 0; j < nTransform + 1; ++j)
            {
                cv::Point2i u = pPath[i] + transforms[j];

                unsigned char vmask = 0;
                cv::Vec <float, cn> vimg = 0;

                if ( u.y < src.rows && u.y >= 0
                &&   u.x < src.cols && u.x >= 0 )
                {
                    if ( xmask == 0 || j == nTransform )
                        vmask = mask.template at<uchar>(u);
                    vimg = img.template at<cv::Vec<float, cn> >(u);
                }

                maskSeq[i].push_back(vmask);
                pointSeq[i].push_back(vimg);

                if (vmask != 0)
                    labelSeq[i] = j;
            }

            cv::Point2i  p[] = {
                                 pPath[i] + cv::Point2i(0, +1),
                                 pPath[i] + cv::Point2i(+1, 0)
                               };

Bellaktris's avatar
Bellaktris committed
162
            for (uint j = 0; j < sizeof(p)/sizeof(cv::Point2i); ++j)
163 164 165 166 167
                if ( p[j].y < src.rows && p[j].y >= 0 &&
                     p[j].x < src.cols && p[j].x >= 0 )
                    linkIdx[i].push_back( backref(p[j]) );
                else
                    linkIdx[i].push_back( -1 );
Bellaktris's avatar
Bellaktris committed
168 169 170
        }

        /** Stitching **/
171 172 173 174 175
        photomontage( pointSeq, maskSeq, linkIdx, labelSeq );

        /** Upscaling **/
        if (ls != 1)
        {
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
            _src.convertTo( img, CV_32F );

            std::vector <Point2i> __pPath = pPath; pPath.clear();

            cv::Mat_<int> __backref( img.size(), -1 );

            std::vector <std::vector <cv::Vec <float, cn> > > __pointSeq = pointSeq; pointSeq.clear();
            std::vector <int> __labelSeq = labelSeq; labelSeq.clear();
            std::vector <std::vector <int> > __linkIdx = linkIdx; linkIdx.clear();
            std::vector <std::vector <unsigned char > > __maskSeq = maskSeq; maskSeq.clear();

            for (size_t i = 0; i < __pPath.size(); ++i)
            {
                cv::Point2i p[] = {
                    __pPath[i] + cv::Point2i(0, -1),
                    __pPath[i] + cv::Point2i(-1, 0)
                };

Bellaktris's avatar
Bellaktris committed
194
                for (uint j = 0; j < sizeof(p)/sizeof(cv::Point2i); ++j)
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
                    if ( p[j].y < src.rows && p[j].y >= 0 &&
                        p[j].x < src.cols && p[j].x >= 0 )
                        __linkIdx[i].push_back( backref(p[j]) );
                    else
                        __linkIdx[i].push_back( -1 );
            }

            for (size_t k = 0; k < __pPath.size(); ++k)
            {
                int clabel = __labelSeq[k];
                int nearSeam = 0;

                for (size_t i = 0; i < __linkIdx[k].size(); ++i)
                    nearSeam |= ( __linkIdx[k][i] == -1
                        || clabel != __labelSeq[__linkIdx[k][i]] );

                if (nearSeam != 0)
                    for (int i = 0; i < ls; ++i)
                        for (int j = 0; j < ls; ++j)
                        {
                            cv::Point2i u = ls*(__pPath[k] + transforms[__labelSeq[k]]) + cv::Point2i(j, i);

                            pPath.push_back( ls*__pPath[k] + cv::Point2i(j, i) );
                            labelSeq.push_back( 0 );

                            __backref(i, j) = int( pPath.size() );

                            cv::Point2i dv[] = {
                                                 cv::Point2i(0,  0),
                                                 cv::Point2i(-1, 0),
                                                 cv::Point2i(+1, 0),
                                                 cv::Point2i(0, -1),
                                                 cv::Point2i(0, +1)
                                               };

                            std::vector <cv::Vec <float, cn> > pointVec;
                                            std::vector <uchar> maskVec;

Bellaktris's avatar
Bellaktris committed
233
                            for (uint q = 0; q < sizeof(dv)/sizeof(cv::Point2i); ++q)
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
                                if (u.x + dv[q].x >= 0 && u.x + dv[q].x < img.cols
                                &&  u.y + dv[q].y >= 0 && u.y + dv[q].y < img.rows)
                                {
                                    pointVec.push_back(img.template at<cv::Vec <float, cn> >(u + dv[q]));
                                    maskVec.push_back(_mask.template at<uchar>(u + dv[q]));
                                }
                                else
                                {
                                    pointVec.push_back( cv::Vec <float, cn>::all(0) );
                                    maskVec.push_back( 0 );
                                }

                            pointSeq.push_back(pointVec);
                              maskSeq.push_back(maskVec);
                        }
                else
                {
                    cv::Point2i fromIdx = ls*(__pPath[k] + transforms[__labelSeq[k]]),
                                  toIdx = ls*__pPath[k];

                    for (int i = 0; i < ls; ++i)
                    {
                        cv::Vec <float, cn> *from = img.template ptr<cv::Vec <float, cn> >(fromIdx.y + i) + fromIdx.x;
                        cv::Vec <float, cn>   *to = img.template ptr<cv::Vec <float, cn> >(toIdx.y + i) + toIdx.x;

                        for (int j = 0; j < ls; ++j)
                            to[j] = from[j];
                    }
                }
            }


Bellaktris's avatar
Bellaktris committed
266
            for (size_t i = 0; i < pPath.size(); ++i)
267 268 269 270 271 272 273 274
            {
                cv::Point2i  p[] = {
                    pPath[i] + cv::Point2i(0, +1),
                    pPath[i] + cv::Point2i(+1, 0)
                };

                std::vector <int> linkVec;

Bellaktris's avatar
Bellaktris committed
275
                for (uint j = 0; j < sizeof(p)/sizeof(cv::Point2i); ++j)
276 277 278 279 280 281 282 283 284 285
                    if ( p[j].y < src.rows && p[j].y >= 0 &&
                        p[j].x < src.cols && p[j].x >= 0 )
                        linkVec.push_back( __backref(p[j]) );
                    else
                        linkVec.push_back( -1 );

                linkIdx.push_back(linkVec);
            }

            photomontage( pointSeq, maskSeq, linkIdx, labelSeq );
286
        }
287 288

        /** Writing result **/
289 290 291 292 293 294
        for (size_t i = 0; i < labelSeq.size(); ++i)
        {
            cv::Vec <float, cn> val = pointSeq[i][labelSeq[i]];
            img.template at<cv::Vec <float, cn> >(pPath[i]) = val;
        }
        img.convertTo( dst, dst.type() );
Bellaktris's avatar
Bellaktris committed
295 296
    }

297 298
    template <typename Tp, unsigned int cn>
    void inpaint(const Mat &src, const Mat &mask, Mat &dst, const int algorithmType)
Bellaktris's avatar
Bellaktris committed
299
    {
300
        dst.create( src.size(), src.type() );
Bellaktris's avatar
Bellaktris committed
301 302 303

        switch ( algorithmType )
        {
304
            case xphoto::INPAINT_SHIFTMAP:
305
                shiftMapInpaint <Tp, cn>(src, mask, dst);
Bellaktris's avatar
Bellaktris committed
306 307
                break;
            default:
308 309
                CV_Error_( CV_StsNotImplemented,
                    ("Unsupported algorithm type (=%d)", algorithmType) );
Bellaktris's avatar
Bellaktris committed
310 311 312 313
                break;
        }
    }

314 315
    static
    void inpaint_shiftmap(const Mat &src, const Mat &mask, Mat &dst, const int algorithmType)
Bellaktris's avatar
Bellaktris committed
316 317 318
    {
        switch ( src.type() )
        {
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
            case CV_8SC1:
                inpaint <char,   1>( src, mask, dst, algorithmType );
                break;
            case CV_8SC2:
                inpaint <char,   2>( src, mask, dst, algorithmType );
                break;
            case CV_8SC3:
                inpaint <char,   3>( src, mask, dst, algorithmType );
                break;
            case CV_8SC4:
                inpaint <char,   4>( src, mask, dst, algorithmType );
                break;
            case CV_8UC1:
                inpaint <uchar,  1>( src, mask, dst, algorithmType );
                break;
            case CV_8UC2:
                inpaint <uchar,  2>( src, mask, dst, algorithmType );
                break;
Bellaktris's avatar
Bellaktris committed
337
            case CV_8UC3:
338
                inpaint <uchar,  3>( src, mask, dst, algorithmType );
Bellaktris's avatar
Bellaktris committed
339
                break;
340 341 342 343 344 345 346 347 348
            case CV_8UC4:
                inpaint <uchar,  4>( src, mask, dst, algorithmType );
                break;
            case CV_16SC1:
                inpaint <short,  1>( src, mask, dst, algorithmType );
                break;
            case CV_16SC2:
                inpaint <short,  2>( src, mask, dst, algorithmType );
                break;
Bellaktris's avatar
Bellaktris committed
349
            case CV_16SC3:
350
                inpaint <short,  3>( src, mask, dst, algorithmType );
Bellaktris's avatar
Bellaktris committed
351
                break;
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
            case CV_16SC4:
                inpaint <short,  4>( src, mask, dst, algorithmType );
                break;
            case CV_16UC1:
                inpaint <ushort, 1>( src, mask, dst, algorithmType );
                break;
            case CV_16UC2:
                inpaint <ushort, 2>( src, mask, dst, algorithmType );
                break;
            case CV_16UC3:
                inpaint <ushort, 3>( src, mask, dst, algorithmType );
                break;
            case CV_16UC4:
                inpaint <ushort, 4>( src, mask, dst, algorithmType );
                break;
            case CV_32SC1:
                inpaint <int,    1>( src, mask, dst, algorithmType );
                break;
            case CV_32SC2:
                inpaint <int,    2>( src, mask, dst, algorithmType );
                break;
Bellaktris's avatar
Bellaktris committed
373
            case CV_32SC3:
374
                inpaint <int,    3>( src, mask, dst, algorithmType );
Bellaktris's avatar
Bellaktris committed
375
                break;
376 377 378 379 380 381 382 383 384
            case CV_32SC4:
                inpaint <int,    4>( src, mask, dst, algorithmType );
                break;
            case CV_32FC1:
                inpaint <float,  1>( src, mask, dst, algorithmType );
                break;
            case CV_32FC2:
                inpaint <float,  2>( src, mask, dst, algorithmType );
                break;
Bellaktris's avatar
Bellaktris committed
385
            case CV_32FC3:
386
                inpaint <float,  3>( src, mask, dst, algorithmType );
Bellaktris's avatar
Bellaktris committed
387
                break;
388 389 390 391 392 393 394 395 396
            case CV_32FC4:
                inpaint <float,  4>( src, mask, dst, algorithmType );
                break;
            case CV_64FC1:
                inpaint <double, 1>( src, mask, dst, algorithmType );
                break;
            case CV_64FC2:
                inpaint <double, 2>( src, mask, dst, algorithmType );
                break;
Bellaktris's avatar
Bellaktris committed
397
            case CV_64FC3:
398
                inpaint <double, 3>( src, mask, dst, algorithmType );
Bellaktris's avatar
Bellaktris committed
399
                break;
400 401 402
            case CV_64FC4:
                inpaint <double, 4>( src, mask, dst, algorithmType );
                break;
Bellaktris's avatar
Bellaktris committed
403
            default:
404 405 406
                CV_Error_( CV_StsNotImplemented,
                    ("Unsupported source image format (=%d)",
                    src.type()) );
Bellaktris's avatar
Bellaktris committed
407 408
        }
    }
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425

void inpaint(const Mat &src, const Mat &mask, Mat &dst, const int algorithmType)
{
    CV_Assert(!src.empty());
    CV_Assert(!mask.empty());
    CV_CheckTypeEQ(mask.type(), CV_8UC1, "");
    CV_Assert(src.rows == mask.rows && src.cols == mask.cols);

    switch (algorithmType)
    {
        case xphoto::INPAINT_SHIFTMAP:
            return inpaint_shiftmap(src, mask, dst, algorithmType);
        case xphoto::INPAINT_FSR_BEST:
        case xphoto::INPAINT_FSR_FAST:
            return inpaint_fsr(src, mask, dst, algorithmType);
    }
    CV_Error_(Error::StsNotImplemented, ("Unsupported inpainting algorithm type (=%d)", algorithmType));
Bellaktris's avatar
Bellaktris committed
426
}
427 428

}}  // namespace