1
/*M///////////////////////////////////////////////////////////////////////////////////////
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11
// For Open Source Computer Vision Library
13
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
14
// Third party copyrights are property of their respective owners.
16
// Redistribution and use in source and binary forms, with or without modification,
17
// are permitted provided that the following conditions are met:
19
// * Redistribution's of source code must retain the above copyright notice,
20
// this list of conditions and the following disclaimer.
22
// * Redistribution's in binary form must reproduce the above copyright notice,
23
// this list of conditions and the following disclaimer in the documentation
24
// and/or other materials provided with the distribution.
26
// * The name of the copyright holders may not be used to endorse or promote products
27
// derived from this software without specific prior written permission.
29
// This software is provided by the copyright holders and contributors "as is" and
30
// any express or implied warranties, including, but not limited to, the implied
31
// warranties of merchantability and fitness for a particular purpose are disclaimed.
32
// In no event shall the Intel Corporation or contributors be liable for any direct,
33
// indirect, incidental, special, exemplary, or consequential damages
34
// (including, but not limited to, procurement of substitute goods or services;
35
// loss of use, data, or profits; or business interruption) however caused
36
// and on any theory of liability, whether in contract, strict liability,
37
// or tort (including negligence or otherwise) arising in any way out of
38
// the use of this software, even if advised of the possibility of such damage.
42
#include "precomp.hpp"
43
#include "opencv2/photo.hpp"
45
#include "seamless_cloning.hpp"
50
void cv::seamlessClone(InputArray _src, InputArray _dst, InputArray _mask, Point p, OutputArray _blend, int flags)
52
const Mat src = _src.getMat();
53
const Mat dest = _dst.getMat();
54
const Mat mask = _mask.getMat();
55
_blend.create(dest.size(), CV_8UC3);
56
Mat blend = _blend.getMat();
58
int minx = INT_MAX, miny = INT_MAX, maxx = INT_MIN, maxy = INT_MIN;
59
int h = mask.size().height;
60
int w = mask.size().width;
62
Mat gray = Mat(mask.size(),CV_8UC1);
63
Mat dst_mask = Mat::zeros(dest.size(),CV_8UC1);
64
Mat cs_mask = Mat::zeros(src.size(),CV_8UC3);
65
Mat cd_mask = Mat::zeros(dest.size(),CV_8UC3);
67
if(mask.channels() == 3)
68
cvtColor(mask, gray, COLOR_BGR2GRAY );
76
if(gray.at<uchar>(i,j) == 255)
78
minx = std::min(minx,i);
79
maxx = std::max(maxx,i);
80
miny = std::min(miny,j);
81
maxy = std::max(maxy,j);
86
int lenx = maxx - minx;
87
int leny = maxy - miny;
89
Mat patch = Mat::zeros(Size(leny, lenx), CV_8UC3);
91
int minxd = p.y - lenx/2;
92
int maxxd = p.y + lenx/2;
93
int minyd = p.x - leny/2;
94
int maxyd = p.x + leny/2;
96
CV_Assert(minxd >= 0 && minyd >= 0 && maxxd <= dest.rows && maxyd <= dest.cols);
98
Rect roi_d(minyd,minxd,leny,lenx);
99
Rect roi_s(miny,minx,leny,lenx);
101
Mat destinationROI = dst_mask(roi_d);
102
Mat sourceROI = cs_mask(roi_s);
104
gray(roi_s).copyTo(destinationROI);
105
src(roi_s).copyTo(sourceROI,gray(roi_s));
106
src(roi_s).copyTo(patch, gray(roi_s));
108
destinationROI = cd_mask(roi_d);
109
cs_mask(roi_s).copyTo(destinationROI);
113
obj.normalClone(dest,cd_mask,dst_mask,blend,flags);
117
void cv::colorChange(InputArray _src, InputArray _mask, OutputArray _dst, float r, float g, float b)
119
Mat src = _src.getMat();
120
Mat mask = _mask.getMat();
121
_dst.create(src.size(), src.type());
122
Mat blend = _dst.getMat();
128
Mat gray = Mat::zeros(mask.size(),CV_8UC1);
130
if(mask.channels() == 3)
131
cvtColor(mask, gray, COLOR_BGR2GRAY );
135
Mat cs_mask = Mat::zeros(src.size(),CV_8UC3);
137
src.copyTo(cs_mask,gray);
140
obj.localColorChange(src,cs_mask,gray,blend,red,green,blue);
143
void cv::illuminationChange(InputArray _src, InputArray _mask, OutputArray _dst, float a, float b)
146
Mat src = _src.getMat();
147
Mat mask = _mask.getMat();
148
_dst.create(src.size(), src.type());
149
Mat blend = _dst.getMat();
153
Mat gray = Mat::zeros(mask.size(),CV_8UC1);
155
if(mask.channels() == 3)
156
cvtColor(mask, gray, COLOR_BGR2GRAY );
160
Mat cs_mask = Mat::zeros(src.size(),CV_8UC3);
162
src.copyTo(cs_mask,gray);
165
obj.illuminationChange(src,cs_mask,gray,blend,alpha,beta);
169
void cv::textureFlattening(InputArray _src, InputArray _mask, OutputArray _dst,
170
float low_threshold, float high_threshold, int kernel_size)
173
Mat src = _src.getMat();
174
Mat mask = _mask.getMat();
175
_dst.create(src.size(), src.type());
176
Mat blend = _dst.getMat();
178
Mat gray = Mat::zeros(mask.size(),CV_8UC1);
180
if(mask.channels() == 3)
181
cvtColor(mask, gray, COLOR_BGR2GRAY );
185
Mat cs_mask = Mat::zeros(src.size(),CV_8UC3);
187
src.copyTo(cs_mask,gray);
190
obj.textureFlatten(src,cs_mask,gray,low_threshold,high_threshold,kernel_size,blend);