Skip to content

Commit 6f5d019

Browse files
committed
change meanCol from bgr to cielab color space
1 parent 92f07ad commit 6f5d019

File tree

2 files changed

+60
-17
lines changed

2 files changed

+60
-17
lines changed

modules/saliency/include/opencv2/saliency/saliencySpecializedClasses.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,16 @@ class CV_EXPORTS_W BackgroundContrast : public StaticSaliency
218218
return computeSaliencyImpl( image, saliencyMap );
219219
}
220220
Mat saliencyMapGenerator( const Mat );
221-
void saliencyMapVisualize( InputArray _saliencyMap );
221+
Mat saliencyMapVisualize( InputArray _saliencyMap, int = 0 );
222222
protected:
223223
bool computeSaliencyImpl( InputArray image, OutputArray saliencyMap );
224224
void superpixelSplit( const Mat, Mat&, Mat& );
225225
std::vector<unsigned> getBndPatchIds( const Mat, int = 8);
226226
void getColorPosDis( const Mat, const Mat, Mat&, Mat&, int );
227227
void boundaryConnectivity( const Mat, const Mat, Mat&, std::vector<unsigned>, double = 3.0, double = 7.0 );
228228
void getWeightedContrast( const Mat, const Mat, const Mat, Mat& );
229+
void dist2WeightMatrix( Mat&, Mat&, double );
230+
void rgb2lab( Mat&, Mat& );
229231
};
230232

231233

modules/saliency/src/BackgroundContrast.cpp

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ Mat BackgroundContrast::saliencyMapGenerator( const Mat img )
4848
{
4949
for (unsigned j = 0; j < bdIds.size(); j++)
5050
{
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;
5253
}
5354
}
5455
getColorPosDis(img, idxImg, colDistM, posDistM, adjcMatrix.size[0]);
@@ -63,6 +64,7 @@ Mat BackgroundContrast::saliencyMapGenerator( const Mat img )
6364
}
6465
}
6566
return saliency;
67+
return img;
6668
}
6769

6870
bool BackgroundContrast::computeSaliencyImpl( InputArray image, OutputArray saliencyMap )
@@ -73,7 +75,7 @@ bool BackgroundContrast::computeSaliencyImpl( InputArray image, OutputArray sali
7375
void BackgroundContrast::superpixelSplit( const Mat img, Mat& idxImg, Mat& adjcMatrix)
7476
{
7577
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);
7779
seeds->iterate( img, 4 );
7880
Mat mask;
7981
adjcMatrix = Mat::eye( seeds->getNumberOfSuperpixels(), seeds->getNumberOfSuperpixels(), CV_8U );
@@ -155,7 +157,7 @@ void BackgroundContrast::getColorPosDis( const Mat img, const Mat idxImg, Mat& c
155157
}
156158
meanPos.col(0) /= img.size[0];
157159
meanPos.col(1) /= img.size[1];
158-
160+
rgb2lab(meanCol, meanCol);
159161
for ( int i = 0; i < meanCol.size[1]; i++)
160162
{
161163
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
188190
}
189191
else
190192
{
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);
192194
}
193195
}
194196
}
@@ -203,9 +205,7 @@ void BackgroundContrast::boundaryConnectivity(const Mat adjcMatrix, const Mat co
203205
}
204206
}
205207
}
206-
pow(geoDistMatrix, 2, geoDistMatrix);
207-
geoDistMatrix /= (-1 * 2 * geoSigma * geoSigma);
208-
exp(geoDistMatrix, geoDistMatrix);
208+
dist2WeightMatrix(geoDistMatrix, geoDistMatrix, geoSigma);
209209
bdProb = Mat(adjcMatrix.size[0], 1, CV_64F, Scalar::all(0.0));
210210
for ( int i = 0; i < adjcMatrix.size[0]; i++ )
211211
{
@@ -215,33 +215,74 @@ void BackgroundContrast::boundaryConnectivity(const Mat adjcMatrix, const Mat co
215215
}
216216
bdProb.at<double>(i, 0) /= sqrt(sum(geoDistMatrix.row(i)).val[0]);
217217
}
218-
219-
pow(bdProb, 2, bdProb);
220-
bdProb /= (-1 * 2);
221-
exp(bdProb, bdProb);
218+
dist2WeightMatrix(bdProb, bdProb, 1);
222219
bdProb = 1 - bdProb;
223220
}
224221

225222
void BackgroundContrast::getWeightedContrast( const Mat colDistM, const Mat posDistM, const Mat bgProb, Mat& wCtr )
226223
{
227224
wCtr = posDistM.clone();
228-
pow(wCtr, 2, wCtr);
229-
wCtr /= (-1 * 2 * 0.16);
230-
exp(wCtr, wCtr);
225+
dist2WeightMatrix(wCtr,wCtr, 0.4);
231226
multiply(colDistM, wCtr, wCtr);
232227
wCtr *= bgProb;
233228
}
234229

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+
}
235265

236-
void BackgroundContrast::saliencyMapVisualize( InputArray _saliencyMap )
266+
Mat BackgroundContrast::saliencyMapVisualize( InputArray _saliencyMap, int option )
237267
{
238268
Mat saliency = _saliencyMap.getMat().clone();
269+
239270
double mi = 0, ma = 0;
240271
minMaxLoc( saliency, &mi, &ma );
241272
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+
}
243283
imshow( "saliencyVisual", saliency );
244284
waitKey( 0 );
285+
return saliency;
245286
}
246287

247288
}

0 commit comments

Comments
 (0)