inpainting.cpp 8.55 KB
Newer Older
Bellaktris's avatar
Bellaktris committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/*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
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
//   * 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.
//
//   * The name of Intel Corporation may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// 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>
41
#include <stack>
Bellaktris's avatar
Bellaktris committed
42
#include <limits>
Bellaktris's avatar
Bellaktris committed
43 44 45 46
#include <algorithm>
#include <iterator>
#include <iostream>
#include <time.h>
47
#include <functional>
Bellaktris's avatar
Bellaktris committed
48 49

#include "opencv2/xphoto.hpp"
50

Bellaktris's avatar
Bellaktris committed
51
#include "opencv2/imgproc.hpp"
52
#include "opencv2/imgproc/imgproc_c.h"
Bellaktris's avatar
Bellaktris committed
53 54 55 56 57 58 59

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

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

60 61
#include "opencv2/highgui.hpp"

Bellaktris's avatar
Bellaktris committed
62 63 64 65 66
namespace xphotoInternal
{
#  include "photomontage.hpp"
#  include "annf.hpp"
}
Bellaktris's avatar
Bellaktris committed
67

68 69
namespace cv
{
70 71 72
namespace xphoto
{

73
    template <typename Tp, unsigned int cn>
74 75
    static void shiftMapInpaint(const Mat &src, const Mat &mask, Mat &dst,
        const int nTransform = 60, const int psize = 8)
Bellaktris's avatar
Bellaktris committed
76
    {
77
        /** Preparing input **/
78
        cv::Mat img;
79
        src.convertTo( img, CV_32F );
80 81 82 83
        img.setTo(0, 255 - mask);

        /** ANNF computation **/
        std::vector <Matx33f> transforms( nTransform );
84 85
        xphotoInternal::dominantTransforms(img,
                    transforms, nTransform, psize);
Bellaktris's avatar
Bellaktris committed
86 87

        /** Warping **/
88 89 90 91 92
        std::vector <Mat> images( nTransform + 1 ); // source image transformed with transforms
        std::vector <Mat> masks( nTransform + 1 );  // definition domain for current shift

        Mat_<uchar> invMask = 255 - mask;
        dilate(invMask, invMask, Mat(), Point(-1,-1), 2);
Bellaktris's avatar
Bellaktris committed
93

94
        img.copyTo( images[0] );
Bellaktris's avatar
Bellaktris committed
95 96 97 98
        mask.copyTo( masks[0] );

        for (int i = 0; i < nTransform; ++i)
        {
99 100
            warpPerspective( images[0], images[i + 1], transforms[i],
                             images[0].size(), INTER_LINEAR );
Bellaktris's avatar
Bellaktris committed
101

102 103 104
            warpPerspective( masks[0], masks[i + 1], transforms[i],
                             masks[0].size(), INTER_NEAREST);
            masks[i + 1] &= invMask;
Bellaktris's avatar
Bellaktris committed
105 106 107
        }

        /** Stitching **/
108 109 110
        Mat photomontageResult;
        xphotoInternal::Photomontage < cv::Vec <float, cn> >( images, masks )
            .assignResImage(photomontageResult);
111 112

        /** Writing result **/
113
        photomontageResult.convertTo( dst, dst.type() );
Bellaktris's avatar
Bellaktris committed
114 115
    }

116 117
    template <typename Tp, unsigned int cn>
    void inpaint(const Mat &src, const Mat &mask, Mat &dst, const int algorithmType)
Bellaktris's avatar
Bellaktris committed
118
    {
119
        dst.create( src.size(), src.type() );
Bellaktris's avatar
Bellaktris committed
120 121 122

        switch ( algorithmType )
        {
123
            case xphoto::INPAINT_SHIFTMAP:
124
                shiftMapInpaint <Tp, cn>(src, mask, dst);
Bellaktris's avatar
Bellaktris committed
125 126
                break;
            default:
127 128
                CV_Error_( CV_StsNotImplemented,
                    ("Unsupported algorithm type (=%d)", algorithmType) );
Bellaktris's avatar
Bellaktris committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
                break;
        }
    }

    /*! The function reconstructs the selected image area from known area.
    *  \param src : source image.
    *  \param mask : inpainting mask, 8-bit 1-channel image. Zero pixels indicate the area that needs to be inpainted.
    *  \param dst : destination image.
    *  \param algorithmType : inpainting method.
    */
    void inpaint(const Mat &src, const Mat &mask, Mat &dst, const int algorithmType)
    {
        CV_Assert( mask.channels() == 1 && mask.depth() == CV_8U );
        CV_Assert( src.rows == mask.rows && src.cols == mask.cols );

        switch ( src.type() )
        {
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
            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
164
            case CV_8UC3:
165
                inpaint <uchar,  3>( src, mask, dst, algorithmType );
Bellaktris's avatar
Bellaktris committed
166
                break;
167 168 169 170 171 172 173 174 175
            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
176
            case CV_16SC3:
177
                inpaint <short,  3>( src, mask, dst, algorithmType );
Bellaktris's avatar
Bellaktris committed
178
                break;
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
            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
200
            case CV_32SC3:
201
                inpaint <int,    3>( src, mask, dst, algorithmType );
Bellaktris's avatar
Bellaktris committed
202
                break;
203 204 205 206 207 208 209 210 211
            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
212
            case CV_32FC3:
213
                inpaint <float,  3>( src, mask, dst, algorithmType );
Bellaktris's avatar
Bellaktris committed
214
                break;
215 216 217 218 219 220 221 222 223
            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
224
            case CV_64FC3:
225
                inpaint <double, 3>( src, mask, dst, algorithmType );
Bellaktris's avatar
Bellaktris committed
226
                break;
227 228 229
            case CV_64FC4:
                inpaint <double, 4>( src, mask, dst, algorithmType );
                break;
Bellaktris's avatar
Bellaktris committed
230
            default:
231 232 233
                CV_Error_( CV_StsNotImplemented,
                    ("Unsupported source image format (=%d)",
                    src.type()) );
Bellaktris's avatar
Bellaktris committed
234 235 236
                break;
        }
    }
Bellaktris's avatar
Bellaktris committed
237
}
238
}