Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/photo/src/contrast_preserve.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
43
#include "precomp.hpp"
44
#include "opencv2/photo.hpp"
45
#include <cmath>
46
#include <vector>
47
#include <limits>
48
#include "contrast_preserve.hpp"
49
50
using namespace std;
51
using namespace cv;
52
53
void cv::decolor(InputArray _src, OutputArray _dst, OutputArray _color_boost)
54
{
55
CV_INSTRUMENT_REGION();
56
57
Mat I = _src.getMat();
58
_dst.create(I.size(), CV_8UC1);
59
Mat dst = _dst.getMat();
60
61
_color_boost.create(I.size(), CV_8UC3);
62
Mat color_boost = _color_boost.getMat();
63
64
CV_Assert(!I.empty() && (I.channels()==3));
65
66
// Parameter Setting
67
const int maxIter = 15;
68
const double tol = .0001;
69
int iterCount = 0;
70
double E = 0;
71
double pre_E = std::numeric_limits<double>::infinity();
72
73
Mat img;
74
I.convertTo(img, CV_32FC3, 1.0/255.0);
75
76
// Initialization
77
Decolor obj;
78
79
vector <double> Cg;
80
vector < vector <double> > polyGrad;
81
vector <Vec3i> comb;
82
vector <double> alf;
83
84
obj.grad_system(img,polyGrad,Cg,comb);
85
obj.weak_order(img,alf);
86
87
// Solver
88
Mat Mt = Mat(int(polyGrad.size()),int(polyGrad[0].size()), CV_32FC1);
89
obj.wei_update_matrix(polyGrad,Cg,Mt);
90
91
vector <double> wei;
92
obj.wei_inti(comb,wei);
93
94
//////////////////////////////// main loop starting ////////////////////////////////////////
95
96
vector <double> G_pos(alf.size());
97
vector <double> G_neg(alf.size());
98
vector <double> EXPsum(G_pos.size());
99
vector <double> EXPterm(G_pos.size());
100
vector <double> temp(polyGrad[0].size());
101
vector <double> temp1(polyGrad[0].size());
102
vector <double> temp2(EXPsum.size());
103
vector <double> wei1(polyGrad.size());
104
105
while(sqrt(pow(E-pre_E,2)) > tol)
106
{
107
iterCount +=1;
108
pre_E = E;
109
110
for(size_t i=0; i<polyGrad[0].size(); i++)
111
{
112
double val = 0.0;
113
for(size_t j=0; j<polyGrad.size(); j++)
114
val = val + (polyGrad[j][i] * wei[j]);
115
temp[i] = val - Cg[i];
116
temp1[i] = val + Cg[i];
117
}
118
119
for(size_t i=0; i<alf.size(); i++)
120
{
121
const double sqSigma = obj.sigma * obj.sigma;
122
const double pos = ((1 + alf[i])/2) * exp(-1.0 * 0.5 * (temp[i] * temp[i]) / sqSigma);
123
const double neg = ((1 - alf[i])/2) * exp(-1.0 * 0.5 * (temp1[i] * temp1[i]) / sqSigma);
124
G_pos[i] = pos;
125
G_neg[i] = neg;
126
}
127
128
for(size_t i=0; i<G_pos.size(); i++)
129
EXPsum[i] = G_pos[i]+G_neg[i];
130
131
for(size_t i=0; i<EXPsum.size(); i++)
132
temp2[i] = (EXPsum[i] == 0) ? 1.0 : 0.0;
133
134
for(size_t i=0; i<G_pos.size(); i++)
135
EXPterm[i] = (G_pos[i] - G_neg[i])/(EXPsum[i] + temp2[i]);
136
137
for(int i=0; i<int(polyGrad.size()); i++)
138
{
139
double val1 = 0.0;
140
for(int j=0; j<int(polyGrad[0].size()); j++)
141
{
142
val1 = val1 + (Mt.at<float>(i,j) * EXPterm[j]);
143
}
144
wei1[i] = val1;
145
}
146
147
for(size_t i=0; i<wei.size(); i++)
148
wei[i] = wei1[i];
149
150
E = obj.energyCalcu(Cg, polyGrad, wei);
151
152
if(iterCount > maxIter)
153
break;
154
}
155
156
Mat Gray = Mat::zeros(img.size(),CV_32FC1);
157
obj.grayImContruct(wei, img, Gray);
158
159
Gray.convertTo(dst,CV_8UC1,255);
160
161
/////////////////////////////////// Contrast Boosting /////////////////////////////////
162
163
Mat lab;
164
cvtColor(I,lab,COLOR_BGR2Lab);
165
166
vector <Mat> lab_channel;
167
split(lab,lab_channel);
168
169
dst.copyTo(lab_channel[0]);
170
171
merge(lab_channel,lab);
172
173
cvtColor(lab,color_boost,COLOR_Lab2BGR);
174
}
175
176