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) 2013, OpenCV Foundation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
21 //
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.
25 //
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.
28 //
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.
39 //
40 //M*/
41
42 #include "precomp.hpp"
43 #include "opencv2/photo.hpp"
44
45 #include "seamless_cloning.hpp"
46
47 using namespace std;
48 using namespace cv;
49
checkMask(InputArray _mask,Size size)50 static Mat checkMask(InputArray _mask, Size size)
51 {
52 Mat mask = _mask.getMat();
53 Mat gray;
54 if (mask.channels() > 1)
55 cvtColor(mask, gray, COLOR_BGRA2GRAY);
56 else
57 {
58 if (mask.empty())
59 gray = Mat(size.height, size.width, CV_8UC1, Scalar(255));
60 else
61 mask.copyTo(gray);
62 }
63
64 return gray;
65 }
66
seamlessClone(InputArray _src,InputArray _dst,InputArray _mask,Point p,OutputArray _blend,int flags)67 void cv::seamlessClone(InputArray _src, InputArray _dst, InputArray _mask, Point p, OutputArray _blend, int flags)
68 {
69 CV_INSTRUMENT_REGION();
70
71 const Mat src = _src.getMat();
72 const Mat dest = _dst.getMat();
73 Mat mask = checkMask(_mask, src.size());
74 dest.copyTo(_blend);
75 Mat blend = _blend.getMat();
76
77 Mat mask_inner = mask(Rect(1, 1, mask.cols - 2, mask.rows - 2));
78 copyMakeBorder(mask_inner, mask, 1, 1, 1, 1, BORDER_ISOLATED | BORDER_CONSTANT, Scalar(0));
79
80 Rect roi_s = boundingRect(mask);
81 if (roi_s.empty()) return;
82 Rect roi_d(p.x - roi_s.width / 2, p.y - roi_s.height / 2, roi_s.width, roi_s.height);
83
84 Mat destinationROI = dest(roi_d).clone();
85
86 Mat sourceROI = Mat::zeros(roi_s.height, roi_s.width, src.type());
87 src(roi_s).copyTo(sourceROI,mask(roi_s));
88
89 Mat maskROI = mask(roi_s);
90 Mat recoveredROI = blend(roi_d);
91
92 Cloning obj;
93 obj.normalClone(destinationROI,sourceROI,maskROI,recoveredROI,flags);
94 }
95
colorChange(InputArray _src,InputArray _mask,OutputArray _dst,float red,float green,float blue)96 void cv::colorChange(InputArray _src, InputArray _mask, OutputArray _dst, float red, float green, float blue)
97 {
98 CV_INSTRUMENT_REGION();
99
100 Mat src = _src.getMat();
101 Mat mask = checkMask(_mask, src.size());
102 _dst.create(src.size(), src.type());
103 Mat blend = _dst.getMat();
104
105 Mat cs_mask = Mat::zeros(src.size(), src.type());
106 src.copyTo(cs_mask, mask);
107
108 Cloning obj;
109 obj.localColorChange(src, cs_mask, mask, blend, red, green, blue);
110 }
111
illuminationChange(InputArray _src,InputArray _mask,OutputArray _dst,float alpha,float beta)112 void cv::illuminationChange(InputArray _src, InputArray _mask, OutputArray _dst, float alpha, float beta)
113 {
114 CV_INSTRUMENT_REGION();
115
116 Mat src = _src.getMat();
117 Mat mask = checkMask(_mask, src.size());
118 _dst.create(src.size(), src.type());
119 Mat blend = _dst.getMat();
120
121 Mat cs_mask = Mat::zeros(src.size(), src.type());
122 src.copyTo(cs_mask, mask);
123
124 Cloning obj;
125 obj.illuminationChange(src, cs_mask, mask, blend, alpha, beta);
126
127 }
128
textureFlattening(InputArray _src,InputArray _mask,OutputArray _dst,float low_threshold,float high_threshold,int kernel_size)129 void cv::textureFlattening(InputArray _src, InputArray _mask, OutputArray _dst,
130 float low_threshold, float high_threshold, int kernel_size)
131 {
132 CV_INSTRUMENT_REGION();
133
134 Mat src = _src.getMat();
135 Mat mask = checkMask(_mask, src.size());
136 _dst.create(src.size(), src.type());
137 Mat blend = _dst.getMat();
138
139 Mat cs_mask = Mat::zeros(src.size(), src.type());
140 src.copyTo(cs_mask, mask);
141
142 Cloning obj;
143 obj.textureFlatten(src, cs_mask, mask, low_threshold, high_threshold, kernel_size, blend);
144 }
145