Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/photo/src/seamless_cloning.cpp
16339 views
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
50
static Mat checkMask(InputArray _mask, Size size)
51
{
52
Mat mask = _mask.getMat();
53
Mat gray;
54
if (mask.channels() == 3)
55
cvtColor(mask, gray, COLOR_BGR2GRAY);
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
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
Rect roi_d(p.x - roi_s.width / 2, p.y - roi_s.height / 2, roi_s.width, roi_s.height);
82
83
Mat destinationROI = dest(roi_d).clone();
84
85
Mat sourceROI = Mat::zeros(roi_s.height, roi_s.width, src.type());
86
src(roi_s).copyTo(sourceROI,mask(roi_s));
87
88
Mat maskROI = mask(roi_s);
89
Mat recoveredROI = blend(roi_d);
90
91
Cloning obj;
92
obj.normalClone(destinationROI,sourceROI,maskROI,recoveredROI,flags);
93
}
94
95
void cv::colorChange(InputArray _src, InputArray _mask, OutputArray _dst, float red, float green, float blue)
96
{
97
CV_INSTRUMENT_REGION();
98
99
Mat src = _src.getMat();
100
Mat mask = checkMask(_mask, src.size());
101
_dst.create(src.size(), src.type());
102
Mat blend = _dst.getMat();
103
104
Mat cs_mask = Mat::zeros(src.size(), src.type());
105
src.copyTo(cs_mask, mask);
106
107
Cloning obj;
108
obj.localColorChange(src, cs_mask, mask, blend, red, green, blue);
109
}
110
111
void cv::illuminationChange(InputArray _src, InputArray _mask, OutputArray _dst, float alpha, float beta)
112
{
113
CV_INSTRUMENT_REGION();
114
115
Mat src = _src.getMat();
116
Mat mask = checkMask(_mask, src.size());
117
_dst.create(src.size(), src.type());
118
Mat blend = _dst.getMat();
119
120
Mat cs_mask = Mat::zeros(src.size(), src.type());
121
src.copyTo(cs_mask, mask);
122
123
Cloning obj;
124
obj.illuminationChange(src, cs_mask, mask, blend, alpha, beta);
125
126
}
127
128
void cv::textureFlattening(InputArray _src, InputArray _mask, OutputArray _dst,
129
float low_threshold, float high_threshold, int kernel_size)
130
{
131
CV_INSTRUMENT_REGION();
132
133
Mat src = _src.getMat();
134
Mat mask = checkMask(_mask, src.size());
135
_dst.create(src.size(), src.type());
136
Mat blend = _dst.getMat();
137
138
Mat cs_mask = Mat::zeros(src.size(), src.type());
139
src.copyTo(cs_mask, mask);
140
141
Cloning obj;
142
obj.textureFlatten(src, cs_mask, mask, low_threshold, high_threshold, kernel_size, blend);
143
}
144
145