seamless_cloning.cpp 5.89 KB
Newer Older
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 41
/*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) 2013, OpenCV Foundation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * 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 the copyright holders 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*/

siddharth's avatar
siddharth committed
42 43
#include "precomp.hpp"
#include "opencv2/photo.hpp"
44

siddharth's avatar
siddharth committed
45 46 47 48 49
#include "seamless_cloning.hpp"

using namespace std;
using namespace cv;

50
void cv::seamlessClone(InputArray _src, InputArray _dst, InputArray _mask, Point p, OutputArray _blend, int flags)
siddharth's avatar
siddharth committed
51
{
52 53
    CV_INSTRUMENT_REGION()

Adrien BAK's avatar
Adrien BAK committed
54 55 56
    const Mat src  = _src.getMat();
    const Mat dest = _dst.getMat();
    const Mat mask = _mask.getMat();
57 58
    _blend.create(dest.size(), CV_8UC3);
    Mat blend = _blend.getMat();
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

    int minx = INT_MAX, miny = INT_MAX, maxx = INT_MIN, maxy = INT_MIN;
    int h = mask.size().height;
    int w = mask.size().width;

    Mat gray = Mat(mask.size(),CV_8UC1);
    Mat dst_mask = Mat::zeros(dest.size(),CV_8UC1);
    Mat cs_mask = Mat::zeros(src.size(),CV_8UC3);
    Mat cd_mask = Mat::zeros(dest.size(),CV_8UC3);

    if(mask.channels() == 3)
        cvtColor(mask, gray, COLOR_BGR2GRAY );
    else
        gray = mask;

    for(int i=0;i<h;i++)
    {
        for(int j=0;j<w;j++)
        {
            if(gray.at<uchar>(i,j) == 255)
            {
                minx = std::min(minx,i);
                maxx = std::max(maxx,i);
                miny = std::min(miny,j);
                maxy = std::max(maxy,j);
            }
        }
    }

    int lenx = maxx - minx;
    int leny = maxy - miny;

91 92
    Mat patch = Mat::zeros(Size(leny, lenx), CV_8UC3);

93 94 95 96 97
    int minxd = p.y - lenx/2;
    int maxxd = p.y + lenx/2;
    int minyd = p.x - leny/2;
    int maxyd = p.x + leny/2;

98
    CV_Assert(minxd >= 0 && minyd >= 0 && maxxd <= dest.rows && maxyd <= dest.cols);
99

siddharth's avatar
siddharth committed
100 101
    Rect roi_d(minyd,minxd,leny,lenx);
    Rect roi_s(miny,minx,leny,lenx);
102

siddharth's avatar
siddharth committed
103 104
    Mat destinationROI = dst_mask(roi_d);
    Mat sourceROI = cs_mask(roi_s);
105

siddharth's avatar
siddharth committed
106 107
    gray(roi_s).copyTo(destinationROI);
    src(roi_s).copyTo(sourceROI,gray(roi_s));
108
    src(roi_s).copyTo(patch, gray(roi_s));
109

siddharth's avatar
siddharth committed
110 111
    destinationROI = cd_mask(roi_d);
    cs_mask(roi_s).copyTo(destinationROI);
112

113

114
    Cloning obj;
Adrien BAK's avatar
Adrien BAK committed
115
    obj.normalClone(dest,cd_mask,dst_mask,blend,flags);
siddharth's avatar
siddharth committed
116

siddharth's avatar
siddharth committed
117 118
}

119
void cv::colorChange(InputArray _src, InputArray _mask, OutputArray _dst, float r, float g, float b)
siddharth's avatar
siddharth committed
120
{
121 122
    CV_INSTRUMENT_REGION()

123 124 125 126 127 128 129 130
    Mat src  = _src.getMat();
    Mat mask  = _mask.getMat();
    _dst.create(src.size(), src.type());
    Mat blend = _dst.getMat();

    float red = r;
    float green = g;
    float blue = b;
131 132 133

    Mat gray = Mat::zeros(mask.size(),CV_8UC1);

siddharth's avatar
siddharth committed
134 135 136 137
    if(mask.channels() == 3)
        cvtColor(mask, gray, COLOR_BGR2GRAY );
    else
        gray = mask;
138

139 140
    Mat cs_mask = Mat::zeros(src.size(),CV_8UC3);

141
    src.copyTo(cs_mask,gray);
142 143

    Cloning obj;
Adrien BAK's avatar
Adrien BAK committed
144
    obj.localColorChange(src,cs_mask,gray,blend,red,green,blue);
siddharth's avatar
siddharth committed
145 146
}

147
void cv::illuminationChange(InputArray _src, InputArray _mask, OutputArray _dst, float a, float b)
siddharth's avatar
siddharth committed
148
{
149 150
    CV_INSTRUMENT_REGION()

siddharth's avatar
siddharth committed
151

152 153 154 155 156 157
    Mat src  = _src.getMat();
    Mat mask  = _mask.getMat();
    _dst.create(src.size(), src.type());
    Mat blend = _dst.getMat();
    float alpha = a;
    float beta = b;
158 159

    Mat gray = Mat::zeros(mask.size(),CV_8UC1);
siddharth's avatar
siddharth committed
160 161 162 163 164

    if(mask.channels() == 3)
        cvtColor(mask, gray, COLOR_BGR2GRAY );
    else
        gray = mask;
165 166 167

    Mat cs_mask = Mat::zeros(src.size(),CV_8UC3);

168
    src.copyTo(cs_mask,gray);
169

170
    Cloning obj;
Adrien BAK's avatar
Adrien BAK committed
171
    obj.illuminationChange(src,cs_mask,gray,blend,alpha,beta);
siddharth's avatar
siddharth committed
172 173

}
siddharth's avatar
siddharth committed
174

175
void cv::textureFlattening(InputArray _src, InputArray _mask, OutputArray _dst,
Adrien BAK's avatar
Adrien BAK committed
176
                           float low_threshold, float high_threshold, int kernel_size)
siddharth's avatar
siddharth committed
177
{
178 179
    CV_INSTRUMENT_REGION()

siddharth's avatar
siddharth committed
180

181 182 183 184
    Mat src  = _src.getMat();
    Mat mask  = _mask.getMat();
    _dst.create(src.size(), src.type());
    Mat blend = _dst.getMat();
siddharth's avatar
siddharth committed
185

siddharth's avatar
siddharth committed
186 187 188 189 190 191 192 193 194
    Mat gray = Mat::zeros(mask.size(),CV_8UC1);

    if(mask.channels() == 3)
        cvtColor(mask, gray, COLOR_BGR2GRAY );
    else
        gray = mask;

    Mat cs_mask = Mat::zeros(src.size(),CV_8UC3);

195
    src.copyTo(cs_mask,gray);
196

siddharth's avatar
siddharth committed
197
    Cloning obj;
Adrien BAK's avatar
Adrien BAK committed
198
    obj.textureFlatten(src,cs_mask,gray,low_threshold,high_threshold,kernel_size,blend);
siddharth's avatar
siddharth committed
199
}