Skip to content

Commit a40edad

Browse files
committed
finish bd module
1 parent 123e03b commit a40edad

File tree

3 files changed

+63
-10
lines changed

3 files changed

+63
-10
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,13 @@ This method use the convolution layers of the pretrained AlexNet, linear combina
202202
*/
203203
class CV_EXPORTS_W BackgroundContrast : public StaticSaliency
204204
{
205-
//private:
206-
205+
private:
206+
int limitOfSP;
207+
int nOfLevel;
208+
int usePrior;
209+
int histBin;
207210
public:
208-
BackgroundContrast();
211+
BackgroundContrast( int = 600, int = 4, int = 2, int = 5 );
209212
virtual ~BackgroundContrast();
210213
CV_WRAP static Ptr<BackgroundContrast> create()
211214
{
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// This file is part of OpenCV project.
2+
// It is subject to the license terms in the LICENSE file found in the top-level directory
3+
// of this distribution and at http://opencv.org/license.html.
4+
5+
6+
#include <opencv2/core.hpp>
7+
#include <opencv2/imgproc.hpp>
8+
#include <opencv2/highgui.hpp>
9+
#include <opencv2/saliency.hpp>
10+
#include <opencv2/ximgproc.hpp>
11+
#include <vector>
12+
#include <string>
13+
#include <iostream>
14+
#include <fstream>
15+
16+
using namespace std;
17+
using namespace cv;
18+
using namespace saliency;
19+
20+
21+
int main(int argc, char* argv[])
22+
{
23+
const char *keys =
24+
"{ help h usage ? | | show this message }"
25+
"{ img_path | | path to img }"
26+
"{ foregroundImg_path| | path to saliency img generated by other method }"
27+
"{ optimization |0 | 0 for aggregated optimized with no outside saliency, 1 for optimizaed with outside saliency }";
28+
29+
CommandLineParser parser(argc, argv, keys);
30+
if (parser.has("help"))
31+
{
32+
parser.printMessage();
33+
return 0;
34+
}
35+
string img_path = parser.get<string>("img_path");
36+
string foregroundImg_path = parser.get<string>("foregroundImg_path");
37+
38+
Mat img = imread(img_path);
39+
Mat fgImg = imread(foregroundImg_path);
40+
BackgroundContrast bc;
41+
if ( parser.get<bool>( "optimization" ) )
42+
{
43+
bc.saliencyMapVisualize(bc.saliencyMapGenerator(img, fgImg, 1), 0);
44+
}
45+
else
46+
{
47+
bc.saliencyMapVisualize(bc.saliencyMapGenerator(img), 0);
48+
}
49+
return 0;
50+
} //main

modules/saliency/src/BackgroundContrast.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,8 @@ namespace cv
3333
namespace saliency
3434
{
3535

36-
BackgroundContrast::BackgroundContrast()
37-
{
38-
39-
}
40-
BackgroundContrast::~BackgroundContrast(){};
36+
BackgroundContrast::BackgroundContrast( int _limitOfSP, int _nOfLevel, int _usePrior, int _histBin ): limitOfSP(_limitOfSP), nOfLevel(_nOfLevel), usePrior(_usePrior), histBin(_histBin) {}
37+
BackgroundContrast::~BackgroundContrast(){}
4138

4239
Mat BackgroundContrast::saliencyMapGenerator( const Mat img, const Mat fgImg, int option )
4340
{
@@ -63,13 +60,14 @@ Mat BackgroundContrast::saliencyMapGenerator( const Mat img, const Mat fgImg, in
6360
else
6461
{
6562
Mat temp = fgImg.clone();
63+
wCtr = Mat(adjcMatrix.size[0], 1, CV_64F, Scalar::all(0.0));
6664
resize(temp, temp, img.size());
6765
vector<int> szOfSP = vector<int>(adjcMatrix.size[0], 0);
6866
for ( int i = 0; i < img.size[0]; i++ )
6967
{
7068
for ( int j = 0; j < img.size[1]; j++ )
7169
{
72-
szOfSP[idxImg.at<unsigned>(i, j)]++;
70+
szOfSP[idxImg.at<unsigned>(i, j)] += 1;
7371
wCtr.at<double>(idxImg.at<unsigned>(i, j), 0) += temp.at<double>(i, j);
7472
}
7573
}
@@ -145,13 +143,15 @@ void BackgroundContrast::saliencyOptimize( const Mat adjcMatrix, const Mat colDi
145143

146144
bool BackgroundContrast::computeSaliencyImpl( InputArray image, OutputArray saliencyMap )
147145
{
146+
CV_Assert( !(image.getMat().empty()) );
147+
saliencyMap.assign(saliencyMapGenerator(image.getMat()));
148148
return true;
149149
}
150150

151151
void BackgroundContrast::superpixelSplit( const Mat img, Mat& idxImg, Mat& adjcMatrix)
152152
{
153153
Ptr<SuperpixelSEEDS> seeds;
154-
seeds = createSuperpixelSEEDS( img.size().width, img.size().height, img.channels(), min(img.size().width * img.size().height / 600, 600), 4, 2, 5, false);
154+
seeds = createSuperpixelSEEDS( img.size().width, img.size().height, img.channels(), min(img.size().width * img.size().height / 600, limitOfSP), nOfLevel, usePrior, histBin, false);
155155
seeds->iterate( img, 4 );
156156
Mat mask;
157157
adjcMatrix = Mat::eye( seeds->getNumberOfSuperpixels(), seeds->getNumberOfSuperpixels(), CV_8U );

0 commit comments

Comments
 (0)