@@ -48,7 +48,8 @@ Mat BackgroundContrast::saliencyMapGenerator( const Mat img )
48
48
{
49
49
for (unsigned j = 0 ; j < bdIds.size (); j++)
50
50
{
51
- adjcMatrix.at <uchar>(i, j) = 1 ;
51
+ adjcMatrix.at <uchar>(bdIds[i], bdIds[j]) = 1 ;
52
+ adjcMatrix.at <uchar>(bdIds[j], bdIds[i]) = 1 ;
52
53
}
53
54
}
54
55
getColorPosDis (img, idxImg, colDistM, posDistM, adjcMatrix.size [0 ]);
@@ -63,6 +64,7 @@ Mat BackgroundContrast::saliencyMapGenerator( const Mat img )
63
64
}
64
65
}
65
66
return saliency;
67
+ return img;
66
68
}
67
69
68
70
bool BackgroundContrast::computeSaliencyImpl ( InputArray image, OutputArray saliencyMap )
@@ -73,7 +75,7 @@ bool BackgroundContrast::computeSaliencyImpl( InputArray image, OutputArray sali
73
75
void BackgroundContrast::superpixelSplit ( const Mat img, Mat& idxImg, Mat& adjcMatrix)
74
76
{
75
77
Ptr<SuperpixelSEEDS> seeds;
76
- seeds = createSuperpixelSEEDS ( img.size ().width , img.size ().height , img.channels (), min (img.size ().width * img.size ().height / 600 , 300 ), 4 , 2 , 5 , false );
78
+ seeds = createSuperpixelSEEDS ( img.size ().width , img.size ().height , img.channels (), min (img.size ().width * img.size ().height / 600 , 3000 ), 4 , 2 , 5 , false );
77
79
seeds->iterate( img, 4 );
78
80
Mat mask;
79
81
adjcMatrix = Mat::eye ( seeds->getNumberOfSuperpixels (), seeds->getNumberOfSuperpixels (), CV_8U );
@@ -155,7 +157,7 @@ void BackgroundContrast::getColorPosDis( const Mat img, const Mat idxImg, Mat& c
155
157
}
156
158
meanPos.col (0 ) /= img.size [0 ];
157
159
meanPos.col (1 ) /= img.size [1 ];
158
-
160
+ rgb2lab (meanCol, meanCol);
159
161
for ( int i = 0 ; i < meanCol.size [1 ]; i++)
160
162
{
161
163
Mat temp = ( repeat (meanCol.col (i), 1 , nOfSP) - repeat (meanCol.col (i).t (), nOfSP, 1 ) );
@@ -188,7 +190,7 @@ void BackgroundContrast::boundaryConnectivity(const Mat adjcMatrix, const Mat co
188
190
}
189
191
else
190
192
{
191
- geoDistMatrix.at <double >(i, j) = colDistM.at <double >(i, j);
193
+ geoDistMatrix.at <double >(i, j) = max ( 0.0 , colDistM.at <double >(i, j) - clipVal );
192
194
}
193
195
}
194
196
}
@@ -203,9 +205,7 @@ void BackgroundContrast::boundaryConnectivity(const Mat adjcMatrix, const Mat co
203
205
}
204
206
}
205
207
}
206
- pow (geoDistMatrix, 2 , geoDistMatrix);
207
- geoDistMatrix /= (-1 * 2 * geoSigma * geoSigma);
208
- exp (geoDistMatrix, geoDistMatrix);
208
+ dist2WeightMatrix (geoDistMatrix, geoDistMatrix, geoSigma);
209
209
bdProb = Mat (adjcMatrix.size [0 ], 1 , CV_64F, Scalar::all (0.0 ));
210
210
for ( int i = 0 ; i < adjcMatrix.size [0 ]; i++ )
211
211
{
@@ -215,33 +215,74 @@ void BackgroundContrast::boundaryConnectivity(const Mat adjcMatrix, const Mat co
215
215
}
216
216
bdProb.at <double >(i, 0 ) /= sqrt (sum (geoDistMatrix.row (i)).val [0 ]);
217
217
}
218
-
219
- pow (bdProb, 2 , bdProb);
220
- bdProb /= (-1 * 2 );
221
- exp (bdProb, bdProb);
218
+ dist2WeightMatrix (bdProb, bdProb, 1 );
222
219
bdProb = 1 - bdProb;
223
220
}
224
221
225
222
void BackgroundContrast::getWeightedContrast ( const Mat colDistM, const Mat posDistM, const Mat bgProb, Mat& wCtr )
226
223
{
227
224
wCtr = posDistM.clone ();
228
- pow (wCtr, 2 , wCtr);
229
- wCtr /= (-1 * 2 * 0.16 );
230
- exp (wCtr, wCtr);
225
+ dist2WeightMatrix (wCtr,wCtr, 0.4 );
231
226
multiply (colDistM, wCtr, wCtr);
232
227
wCtr *= bgProb;
233
228
}
234
229
230
+ void BackgroundContrast::dist2WeightMatrix ( Mat& input, Mat& output, double sigma )
231
+ {
232
+ Mat temp = input.clone ();
233
+ output = input.clone ();
234
+ for ( int i = 0 ; i < output.size [0 ]; i++ )
235
+ {
236
+ for ( int j = 0 ; j < output.size [1 ]; j++ )
237
+ {
238
+ // if (temp.at<double>(i, j) > 3 * sigma) output.at<double>(i, j) = 0;
239
+ // else
240
+ // {
241
+ output.at <double >(i, j) = exp (-1 * temp.at <double >(i, j) * temp.at <double >(i, j) / 2 / sigma / sigma);
242
+ // }
243
+ }
244
+ }
245
+ }
246
+
247
+ void BackgroundContrast::rgb2lab ( Mat& inputMeanCol, Mat& outputMeanCol )
248
+ {
249
+ Mat temp = Mat (inputMeanCol.size [0 ], 1 , CV_32FC3, Scalar::all (0 ));
250
+ for ( int i = 0 ; i < inputMeanCol.size [0 ]; i++ )
251
+ {
252
+ temp.at <Vec3f>(i, 0 )[0 ] = inputMeanCol.at <double >(i, 0 ) / 255 ;
253
+ temp.at <Vec3f>(i, 0 )[1 ] = inputMeanCol.at <double >(i, 1 ) / 255 ;
254
+ temp.at <Vec3f>(i, 0 )[2 ] = inputMeanCol.at <double >(i, 2 ) / 255 ;
255
+ }
256
+ cvtColor (temp, temp, COLOR_BGR2Lab);
257
+ outputMeanCol = inputMeanCol.clone ();
258
+ for ( int i = 0 ; i < outputMeanCol.size [0 ]; i++ )
259
+ {
260
+ outputMeanCol.at <double >(i, 0 ) = temp.at <Vec3f>(i, 0 )[0 ];
261
+ outputMeanCol.at <double >(i, 1 ) = temp.at <Vec3f>(i, 0 )[1 ];
262
+ outputMeanCol.at <double >(i, 2 ) = temp.at <Vec3f>(i, 0 )[2 ];
263
+ }
264
+ }
235
265
236
- void BackgroundContrast::saliencyMapVisualize ( InputArray _saliencyMap )
266
+ Mat BackgroundContrast::saliencyMapVisualize ( InputArray _saliencyMap, int option )
237
267
{
238
268
Mat saliency = _saliencyMap.getMat ().clone ();
269
+
239
270
double mi = 0 , ma = 0 ;
240
271
minMaxLoc ( saliency, &mi, &ma );
241
272
saliency -= mi;
242
- saliency /= ( ma - mi );
273
+ saliency /= ( ma - mi + 0.000001 );
274
+
275
+ if (option != 0 )
276
+ {
277
+ saliency *= 255 ;
278
+ saliency.convertTo (saliency, CV_8U);
279
+ if (option == 1 ) threshold (saliency, saliency, 0 , 255 , THRESH_BINARY | THRESH_OTSU);
280
+ else if (option == 2 ) threshold (saliency, saliency, 0 , 255 , THRESH_TOZERO | THRESH_OTSU);
281
+ // threshold(saliency, saliency, 0, 255, THRESH_TOZERO | THRESH_OTSU);
282
+ }
243
283
imshow ( " saliencyVisual" , saliency );
244
284
waitKey ( 0 );
285
+ return saliency;
245
286
}
246
287
247
288
}
0 commit comments