Repository navigation
Expand file tree
/
Copy pathCmSaliencyGC.cpp
More file actions
295 lines (267 loc) · 8.85 KB
/
Copy pathCmSaliencyGC.cpp
File metadata and controls
295 lines (267 loc) · 8.85 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include "stdafx.h"
#include "CmSaliencyGC.h"
CmSaliencyGC::CmSaliencyGC(CMat &img3f, CStr &outName, bool GET_CSD)
:_gmm(DEFAULT_GMM_NUM), _img3f(img3f), _nameNE(outName), _GET_CSD(GET_CSD)
{
}
const int CmSaliencyGC::DEFAULT_GMM_NUM = 15;
void CmSaliencyGC::HistgramGMMs()
{
// Color quantization
Mat _colorNums1f, _binBGR3f;
CmColorQua::D_Quantize(_img3f, _HistBinIdx1i, _binBGR3f, _colorNums1f);
//CmShow::HistBins(_binBGR3f, _colorNums1i, _nameNE + "_QT.jpg", true);
_colorNums1f.convertTo(_colorNums1f, CV_32F);
// Train GMMs
Mat gmmIdx1i;
_gmm.BuildGMMs(_binBGR3f, gmmIdx1i, _colorNums1f);
_NUM = _gmm.RefineGMMs(_binBGR3f, gmmIdx1i, _colorNums1f);
_PixelSalCi1f.resize(_NUM);
_HistBinSalCi1f.resize(_NUM);
_gmmClrs.resize(_NUM);
_gmmW.resize(_NUM);
_csd.resize(_NUM);
_gu.resize(_NUM);
_fSal.resize(_NUM);
// Assign GMM color means and weights
for (int i = 0; i < _NUM; i++)
_gmmClrs[i] = _gmm.getMean(i), _gmmW[i] = _gmm.getWeight(i);
// Assign GMMs for each components
_gmm.GetProbs(_binBGR3f, _HistBinSalCi1f);
#pragma omp parallel for
for (int c = 0; c < _NUM; c++){
_PixelSalCi1f[c].create(_img3f.size(), CV_32FC1);
float *prob = (float*)(_HistBinSalCi1f[c].data);
for (int y = 0; y < _HistBinIdx1i.rows; y++){
const int *_idx = _HistBinIdx1i.ptr<int>(y);
float* probCI = _PixelSalCi1f[c].ptr<float>(y);
for (int x = 0; x < _HistBinIdx1i.cols; x++)
probCI[x] = prob[_idx[x]];
}
blur(_PixelSalCi1f[c], _PixelSalCi1f[c], Size(3, 3));
}
}
Mat CmSaliencyGC::GetSaliencyCues()
{
vecD d;
Mat salMatCSD;
if (_GET_CSD){
GetCSD(_csd, d);
salMatCSD = GetSalFromGMMs(_csd);
}
GetGU(_gu, d, 0.4, 15);
Mat salMatGU = GetSalFromGMMs(_gu);
if (_GET_CSD)
return SpatialVar(salMatCSD) < SpatialVar(salMatGU) ? salMatCSD : salMatGU;
else
return salMatGU;
}
void CmSaliencyGC::MergeGMMs()
{
_ClusteredIdx.resize(_NUM);
for (int i = 0; i < _NUM; i++)
_ClusteredIdx[i] = i;
CmAPCluster apCluter;
CmAPCluster::apcluster32 apFun = apCluter.GetApFun();
unsigned int N = _NUM * _NUM;
if (apFun != NULL){
Mat_<double> cor = Mat_<double>::zeros(_NUM, _NUM);
for (int y = 0; y < _img3f.rows; y++) {
for (int x = 0; x < _img3f.cols; x++){
vector<CostfIdx> pIdx;
pIdx.reserve(_NUM);
for (int c = 0;c < _NUM; c++)
pIdx.push_back(make_pair(_PixelSalCi1f[c].at<float>(y, x), c));
sort(pIdx.begin(), pIdx.end(), std::greater<CostfIdx>());
int i1 = pIdx[0].second, i2 = pIdx[1].second;
float cost = min(pIdx[0].first, pIdx[1].first);
cor(i1, i2) += cost;
cor(i2, i1) += cost;
}
}
double scale = _img3f.rows*_img3f.cols * 7 / 1e4; // Scale values for better illustration
for (int i = 0; i < _NUM; i++)
for (int j = 0; j < i; j++)
cor(i, j) = cor(j, i) = log(1 + cor(i, j) / (scale * min(_gmmW[i], _gmmW[j]) + 1E-4)) - 1;
//CmShow::Correlation(_gmmClrs, cor, _nameNE + "_MCOR.png", 20);
double preference = 4*sum(cor).val[0]/(cor.cols*cor.rows);
for (int i = 0; i < _NUM; i++)
cor(i, i) = preference; // Preference for apcluster
double netSim = 0;
apFun(cor.ptr<double>(0), NULL, NULL, N, &_ClusteredIdx[0], &netSim, &apCluter.apoptions);
}
_NUM_Merged = CmAPCluster::ReMapIdx(_ClusteredIdx);
_pciM1f.resize(_NUM_Merged);
if (apFun != NULL){
for (int i = 0; i < _NUM_Merged; i++)
_pciM1f[i] = Mat::zeros(_img3f.size(), CV_32FC1);
for (int i = 0; i < _NUM; i++)
_pciM1f[_ClusteredIdx[i]] += _PixelSalCi1f[i];
}
else{
for (int i = 0; i < _NUM; i++)
_pciM1f[i] = _PixelSalCi1f[i];
}
}
// Return the center distance and spatial variance of X and Y direction. Pixel locations are normalized to [0, 1]
double CmSaliencyGC::SpatialVar(CMat& map1f, double &cD)
{
// Find centroid of the map
const int h = map1f.rows, w = map1f.cols;
const double H = h, W = w;
double xM = 0, yM = 0, sumP = EPS;
for (int y = 0; y < h; y++) {
const float* prob = map1f.ptr<float>(y);
for (int x = 0; x < w; x++)
sumP += prob[x], xM += prob[x] * x, yM += prob[x] * y;
}
xM /= sumP, yM /= sumP;
// Find variance of X and Y direction
double vX = 0, vY = 0;
cD = 0; // average center distance
for (int y = 0; y < h; y++) {
const float* prob = map1f.ptr<float>(y);
for (int x = 0; x < w; x++){
double p = prob[x];
vX += p * sqr(x - xM), vY += p * sqr(y - yM);
cD += p * sqrt(sqr(x/W - 0.5) + sqr(y/H - 0.5));
}
}
cD /= sumP;
return vX/sumP + vY/sumP;
}
void CmSaliencyGC::GetCSD(vecD &_csd, vecD &_cD)
{
MergeGMMs();
vecD csd, cD;
int num = (int)_pciM1f.size(); // Number of Gaussian
Size imgSz = _pciM1f[0].size();
double centerX = imgSz.width / 2, centerY = imgSz.height / 2;
cD.resize(num);
vecD V(num), D(num);
#pragma omp parallel for
for (int i = 0; i < num; i++)
V[i] = SpatialVar(_pciM1f[i], cD[i]);
normalize(V, V, 0, 1, CV_MINMAX);
normalize(cD, D, 0, 1, CV_MINMAX);
csd.resize(num);
for (int i = 0; i < num; i++)
csd[i] = (1-V[i])*(1-D[i]);
normalize(csd, csd, 0, 1, CV_MINMAX);
_csd.resize(_NUM);
_cD.resize(_NUM);
for (int i = 0; i < _NUM; i++)
_csd[i] = csd[_ClusteredIdx[i]], _cD[i] = cD[_ClusteredIdx[i]];
}
void CmSaliencyGC::ViewValues(vecD &vals, CStr &ext)
{
int K = _NUM;
Mat_<Vec3f> color3f(1, K);
Mat_<double> weight1d(1, K), cov1d(1, K);
for (int i = 0; i < K; i++) {
color3f(0, i) = _gmm.getMean(i);
weight1d(0, i) = vals[i];
cov1d(0, i) = sqrt(_gmm.GetGaussians()[i].det);
}
//CmShow::HistBins(color3f, weight1d, _nameNE + ext, false, cov1d);
}
void CmSaliencyGC::GetGU(vecD& gc, vecD &d, double sigmaDist, double dominate)
{
if (d.size() == 0){
d.resize(_NUM);
#pragma omp parallel for
for (int i = 0; i < _NUM; i++)
SpatialVar(_PixelSalCi1f[i], d[i]);
}
Mat_<double> clrDist;
clrDist = Mat_<double>::zeros(_NUM, _NUM);
vector<Vec3f> gmmClrs(_NUM);
cvtColor(_gmmClrs, gmmClrs, CV_BGR2Lab);
vecD maxDist(_NUM);
for (int i = 0; i < _NUM; i++) for (int j = 0; j < i; j++){
double dCrnt = vecDist(gmmClrs[i], gmmClrs[j]);
clrDist(i, j) = clrDist(j, i) = dCrnt;
maxDist[i] = max(maxDist[i], dCrnt);
maxDist[j] = max(maxDist[j], dCrnt);
}
for (int i = 0; i < _NUM; i++) {
gc[i] = 0;
for (int j = 0; j < _NUM; j++)
gc[i] += _gmmW[j] * clrDist(i, j) / maxDist[i]; //min(clrDist(i, j), dominate);
}
for (int i = 0; i < _NUM; i++)
_gu[i] = _gu[i] * exp(-9.0 * sqr(d[i])); //
normalize(_gu, _gu, 0, 1, CV_MINMAX);
}
void CmSaliencyGC::ReScale(vecD& salVals, double minRatio)
{
while (1){
double sumW = 0, maxCrnt = 0;
for (int i = 0; i < _NUM; i++)
if (salVals[i] > 0.95)
sumW += _gmmW[i];
else
maxCrnt = max(maxCrnt, salVals[i]);
if (sumW < minRatio && maxCrnt > EPS)
for (int i = 0; i < _NUM; i++)
salVals[i] = min(salVals[i]/maxCrnt, 1.0);
else
break;
}
}
Mat CmSaliencyGC::GetSalFromGMMs(vecD &val, bool bNormlize)
{
ReScale(val);
Mat binSal = Mat::zeros(_HistBinSalCi1f[0].size(), CV_32F);
int num = (int)_HistBinSalCi1f.size(), type = _HistBinSalCi1f[0].type();
for (int c = 0; c < num; c++){
Mat tmp;
_HistBinSalCi1f[c].convertTo(tmp, type, val[c]);
add(binSal, tmp, binSal);
}
normalize(binSal, binSal, -4, 4, NORM_MINMAX);
float *bVal = binSal.ptr<float>(0);
for(int i = 0; i < binSal.cols; i++)
bVal[i] = 1 / (1 + exp(-bVal[i])); // Sigmoid
if (bNormlize)
normalize(binSal, binSal, 0, 1, NORM_MINMAX);
Mat salMat(_HistBinIdx1i.size(), CV_32F);
float* binSalVal = (float*)(binSal.data);
#pragma omp parallel for
for (int r = 0; r < _HistBinIdx1i.rows; r++){
const int* idx = _HistBinIdx1i.ptr<int>(r);
float* sal = salMat.ptr<float>(r);
for (int c = 0; c < _HistBinIdx1i.cols; c++)
sal[c] = binSalVal[idx[c]];
}
blur(salMat, salMat, Size(3, 3));
normalize(salMat, salMat, 0, 1, NORM_MINMAX);
return salMat;
}
// See my CVPR 11 paper http://mmcheng.net/SalObj/ for source code used to generate
// nice statistical plots, many comparison figures and latex (see supplemental material).
int CmSaliencyGC::Demo(CStr imgW, CStr salDir)
{
CmFile::MkDir(salDir);
vecS namesNE, des;
string imgDir, ext;
int imgNum = CmFile::GetNamesNE(imgW, namesNE, imgDir, ext);
CmTimer tm("Maps");
tm.Start();
printf("%d images found in %s\n", imgNum, _S(imgDir + "*.jpg"));
#pragma omp parallel for
for (int i = 0; i < imgNum; i++){
Mat img = imread(imgDir + namesNE[i] + ".jpg");
CStr outName = salDir + namesNE[i];
img.convertTo(img, CV_32FC3, 1.0/255);
CmSaliencyGC sGC(img, outName, true);
sGC.HistgramGMMs();
Mat sal1f = sGC.GetSaliencyCues();
imwrite(outName + "_GC.png", sal1f*255);
}
tm.Stop();
printf("Speed of GC method: %g seconds = %g fps\n", tm.TimeInSeconds()/imgNum, imgNum/tm.TimeInSeconds());
des.push_back("GC");
CmEvaluation::Evaluate(imgDir + "*.png", salDir, salDir + "Eval.m", des);
return 0;
}