Commit d9728347 authored by jaco's avatar jaco

BING Objectness porting progress II

parent dae0dc9c
......@@ -58,7 +58,9 @@ int main( int argc, char** argv )
return -1;
}
Mat image, saliencyMap, binaryMap;
Mat binaryMap;
Mat image;
//OutputArray saliencyMap( image );
cap >> frame;
if( frame.empty() )
......@@ -68,15 +70,31 @@ int main( int argc, char** argv )
frame.copyTo( image );
if( saliencyAlgorithm->computeSaliency( image, saliencyMap ) )
if( saliency_algorithm.find( "SPECTRAL_RESIDUAL" ) == 0 )
{
StaticSaliencySpectralResidual spec;
spec.computeBinaryMap( saliencyMap, binaryMap );
//saliencyAlgorithm->computeBinaryMap( saliencyMap, binaryMap );
imshow( "Saliency Map", saliencyMap );
imshow( "Original Image", image );
imshow( "Binary Map", binaryMap );
waitKey( 0 );
Mat saliencyMap;
if( saliencyAlgorithm->computeSaliency( image, saliencyMap ) )
{
StaticSaliencySpectralResidual spec;
//Mat salMat=saliencyMap.getMat();
spec.computeBinaryMap( saliencyMap, binaryMap );
//saliencyAlgorithm->computeBinaryMap( saliencyMap, binaryMap );
imshow( "Saliency Map", saliencyMap );
imshow( "Original Image", image );
imshow( "Binary Map", binaryMap );
waitKey( 0 );
}
}
else if( saliency_algorithm.find( "BING" ) == 0 )
{
vector<Vec4i> saliencyMap;
if( saliencyAlgorithm->computeSaliency( image, saliencyMap ) )
{
std::cout << "-----------------OBJECTNESS-----------" << std::endl;
std::cout << "OBJ BB VECTOR SIZE" << saliencyMap.size() << std::endl;
}
}
......
#include "kyheader.h"
#include "CmShow.h"
#include "opencv2/core.hpp"
typedef pair<int, int> CostiIdx;
Mat CmShow::HistBins(CMat& color3f, CMat& val, CStr& title, bool descendShow, CMat &with)
{
// Prepare data
int H = 300, spaceH = 6, barH = 10, n = color3f.cols;
CV_Assert(color3f.size() == val.size() && color3f.rows == 1);
Mat binVal1i, binColor3b, width1i;
if (with.size() == val.size())
with.convertTo(width1i, CV_32S, 400/sum(with).val[0]); // Default shown width
else
width1i = Mat(1, n, CV_32S, Scalar(10)); // Default bin width = 10
int W = cvRound(sum(width1i).val[0]);
color3f.convertTo(binColor3b, CV_8UC3, 255);
double maxVal, minVal;
minMaxLoc(val, &minVal, &maxVal);
printf("%g\n", H/max(maxVal, -minVal));
val.convertTo(binVal1i, CV_32S, 20000);
Size szShow(W, H + spaceH + barH);
szShow.height += minVal < 0 && !descendShow ? H + spaceH : 0;
Mat showImg3b(szShow, CV_8UC3, Scalar(255, 255, 255));
int* binH = (int*)(binVal1i.data);
Vec3b* binColor = (Vec3b*)(binColor3b.data);
int* binW = (int*)(width1i.data);
vector<CostiIdx> costIdx(n);
if (descendShow){
for (int i = 0; i < n; i++)
costIdx[i] = make_pair(binH[i], i);
sort(costIdx.begin(), costIdx.end(), std::greater<CostiIdx>());
}
// Show image
for (int i = 0, x = 0; i < n; i++){
int idx = descendShow ? costIdx[i].second : i;
int h = descendShow ? abs(binH[idx]) : binH[idx];
Scalar color(binColor[idx]);
Rect reg(x, H + spaceH, binW[idx], barH);
showImg3b(reg) = color; // Draw bar
rectangle(showImg3b, reg, Scalar(0));
reg.height = abs(h);
reg.y = h >= 0 ? H - h : H + 2 * spaceH + barH;
showImg3b(reg) = color;
rectangle(showImg3b, reg, Scalar(0));
x += binW[idx];
}
imshow(String(title.c_str()), showImg3b);
return showImg3b;
}
void CmShow::showTinyMat(CStr &title, CMat &m)
{
int scale = 50, sz = m.rows * m.cols;
while (sz > 200){
scale /= 2;
sz /= 4;
}
Mat img;
resize(m, img, Size(), scale, scale, INTER_NEAREST );
if (img.channels() == 3)
cvtColor(img, img, COLOR_RGB2BGR);
SaveShow(img, title);
}
void CmShow::SaveShow(CMat& img, CStr& title)
{
if (title.size() == 0)
return;
int mDepth = CV_MAT_DEPTH(img.type());
double scale = (mDepth == CV_32F || mDepth == CV_64F ? 255 : 1);
if (title.size() > 4 && title[title.size() - 4] == '.')
imwrite(String(title.c_str()), img*scale);
else if (title.size())
imshow(String(title.c_str()), img);
}
#pragma once
class CmShow
{
public:
static Mat HistBins(CMat& color3f, CMat& val, CStr& title, bool descendShow = false, CMat &with = Mat());
static void showTinyMat(CStr &title, CMat &m);
static inline void SaveShow(CMat& img, CStr& title);
};
......@@ -7,66 +7,110 @@
template<typename VT, typename ST>
struct ValStructVec
{
ValStructVec(){clear();}
inline int size() const {return sz;}
inline void clear() {sz = 0; structVals.clear(); valIdxes.clear();}
inline void reserve(int resSz){clear(); structVals.reserve(resSz); valIdxes.reserve(resSz); }
inline void pushBack(const VT& val, const ST& structVal) {valIdxes.push_back(make_pair(val, sz)); structVals.push_back(structVal); sz++;}
ValStructVec()
{
clear();
}
inline int size() const
{
return sz;
}
inline void clear()
{
sz = 0;
structVals.clear();
valIdxes.clear();
}
inline void reserve( int resSz )
{
clear();
structVals.reserve( resSz );
valIdxes.reserve( resSz );
}
inline void pushBack( const VT& val, const ST& structVal )
{
valIdxes.push_back( make_pair( val, sz ) );
structVals.push_back( structVal );
sz++;
}
inline const VT& operator ()(int i) const {return valIdxes[i].first;} // Should be called after sort
inline const ST& operator [](int i) const {return structVals[valIdxes[i].second];} // Should be called after sort
inline VT& operator ()(int i) {return valIdxes[i].first;} // Should be called after sort
inline ST& operator [](int i) {return structVals[valIdxes[i].second];} // Should be called after sort
inline const VT& operator ()( int i ) const
{
return valIdxes[i].first;
} // Should be called after sort
inline const ST& operator []( int i ) const
{
return structVals[valIdxes[i].second];
} // Should be called after sort
inline VT& operator ()( int i )
{
return valIdxes[i].first;
} // Should be called after sort
inline ST& operator []( int i )
{
return structVals[valIdxes[i].second];
} // Should be called after sort
void sort(bool descendOrder = true);
const vector<ST> &getSortedStructVal();
void append(const ValStructVec<VT, ST> &newVals, int startV = 0);
void sort( bool descendOrder = true );
const vector<ST> &getSortedStructVal();
vector<pair<VT, int>> getvalIdxes();
void append( const ValStructVec<VT, ST> &newVals, int startV = 0 );
vector<ST> structVals; // struct values
vector<ST> structVals; // struct values
private:
int sz; // size of the value struct vector
vector<pair<VT, int>> valIdxes; // Indexes after sort
bool smaller() {return true;};
vector<ST> sortedStructVals;
private:
int sz; // size of the value struct vector
vector<pair<VT, int>> valIdxes; // Indexes after sort
bool smaller()
{
return true;
}
;
vector<ST> sortedStructVals;
};
template<typename VT, typename ST>
void ValStructVec<VT, ST>::append(const ValStructVec<VT, ST> &newVals, int startV)
void ValStructVec<VT, ST>::append( const ValStructVec<VT, ST> &newVals, int startV )
{
int sz = newVals.size();
for (int i = 0; i < sz; i++)
pushBack((float)((i+300)*startV)/*newVals(i)*/, newVals[i]);
int sz = newVals.size();
for ( int i = 0; i < sz; i++ )
pushBack( (float) ( ( i + 300 ) * startV )/*newVals(i)*/, newVals[i] );
}
template<typename VT, typename ST>
void ValStructVec<VT, ST>::sort(bool descendOrder /* = true */)
void ValStructVec<VT, ST>::sort( bool descendOrder /* = true */)
{
if (descendOrder)
std::sort(valIdxes.begin(), valIdxes.end(), std::greater<pair<VT, int>>());
else
std::sort(valIdxes.begin(), valIdxes.end(), std::less<pair<VT, int>>());
if( descendOrder )
std::sort( valIdxes.begin(), valIdxes.end(), std::greater<pair<VT, int>>() );
else
std::sort( valIdxes.begin(), valIdxes.end(), std::less<pair<VT, int>>() );
}
template<typename VT, typename ST>
const vector<ST>& ValStructVec<VT, ST>::getSortedStructVal()
{
sortedStructVals.resize(sz);
for (int i = 0; i < sz; i++)
sortedStructVals[i] = structVals[valIdxes[i].second];
return sortedStructVals;
sortedStructVals.resize( sz );
for ( int i = 0; i < sz; i++ )
sortedStructVals[i] = structVals[valIdxes[i].second];
return sortedStructVals;
}
/*
void valStructVecDemo()
template<typename VT, typename ST>
vector<pair<VT, int>> ValStructVec<VT, ST>::getvalIdxes()
{
ValStructVec<int, string> sVals;
sVals.pushBack(3, "String 3");
sVals.pushBack(5, "String 5");
sVals.pushBack(4, "String 4");
sVals.pushBack(1, "String 1");
sVals.sort(false);
for (int i = 0; i < sVals.size(); i++)
printf("%d, %s\n", sVals(i), _S(sVals[i]));
return valIdxes;
}
*/
/*
void valStructVecDemo()
{
ValStructVec<int, string> sVals;
sVals.pushBack(3, "String 3");
sVals.pushBack(5, "String 5");
sVals.pushBack(4, "String 4");
sVals.pushBack(1, "String 1");
sVals.sort(false);
for (int i = 0; i < sVals.size(); i++)
printf("%d, %s\n", sVals(i), _S(sVals[i]));
}
*/
......@@ -56,11 +56,11 @@ ObjectnessBING::ObjectnessBING()
_base = 2; // base for window size quantization
_W = 8; // feature window size (W, W)
_NSS = 2; //non-maximal suppress size NSS
_logBase=log( _base );
_minT=cvCeil( log( 10. ) / _logBase );
_maxT=cvCeil( log( 500. ) / _logBase );
_numT= _maxT - _minT + 1 ;
_Clr= MAXBGR ;
_logBase = log( _base );
_minT = cvCeil( log( 10. ) / _logBase );
_maxT = cvCeil( log( 500. ) / _logBase );
_numT = _maxT - _minT + 1;
_Clr = MAXBGR;
setColorSpace( _Clr );
......@@ -75,7 +75,8 @@ ObjectnessBING::~ObjectnessBING()
void ObjectnessBING::setColorSpace( int clr )
{
_Clr = clr;
_modelName = "/home/puja/src/opencv_contrib/modules/saliency/src/ObjectnessTrainedModel" + string(format("ObjNessB%gW%d%s", _base, _W, _clrName[_Clr]).c_str());
_modelName = "/home/puja/src/opencv_contrib/modules/saliency/src/ObjectnessTrainedModel"
+ string( format( "ObjNessB%gW%d%s", _base, _W, _clrName[_Clr] ).c_str() );
// _trainDirSI = _voc.localDir + string(format("TrainS1B%gW%d%s/", _base, _W, _clrName[_Clr]).c_str());
// _bbResDir = _voc.resDir + string(format("BBoxesB%gW%d%s/", _base, _W, _clrName[_Clr]).c_str());
}
......@@ -368,6 +369,8 @@ void ObjectnessBING::gradientXY( CMat &x1i, CMat &y1i, Mat &mag1u )
void ObjectnessBING::getObjBndBoxesForSingleImage( Mat img, ValStructVec<float, Vec4i> &finalBoxes, int numDetPerSize )
{
ValStructVec<float, Vec4i> boxes;
finalBoxes.reserve( 10000 );
int scales[3] =
{ 1, 3, 5 };
......@@ -378,7 +381,8 @@ void ObjectnessBING::getObjBndBoxesForSingleImage( Mat img, ValStructVec<float,
CmTimer tm( "Predict" );
tm.Start();
getObjBndBoxes( img, finalBoxes, numDetPerSize );
getObjBndBoxes( img, boxes, numDetPerSize );
finalBoxes.append( boxes, scales[clr] );
tm.Stop();
printf( "Average time for predicting an image (%s) is %gs\n", _clrName[_Clr], tm.TimeInSeconds() );
......@@ -407,12 +411,13 @@ std::string inline removeExtension( std::string const& filename )
// Read matrix from binary file
bool ObjectnessBING::matRead( const string& filename, Mat& _M )
{
String filenamePlusExt(filename.c_str());
filenamePlusExt+=".yml.gz";
FileStorage fs2( filenamePlusExt, FileStorage::READ );
FileStorage fs2( filename + ".yml.gz", FileStorage::READ );
String fileNameString( filename.c_str() );
//String fileNameString( filename.c_str() );
Mat M;
fs2[removeExtension( basename( fileNameString ) )] >> M;
fs2[String(removeExtension( basename( filename ) ).c_str())] >> M;
/*FILE* f = fopen(_S(filename), "rb");
if (f == NULL)
......@@ -449,19 +454,33 @@ void ObjectnessBING::write( cv::FileStorage& fs ) const
bool ObjectnessBING::computeSaliencyImpl( const InputArray image, OutputArray objBoundingBox )
{
ValStructVec<float, Vec4i> &finalBoxes;
ValStructVec<float, Vec4i> finalBoxes;
getObjBndBoxesForSingleImage( image.getMat(), finalBoxes, 250 );
// List of rectangles returned by objectess function in ascending order.
// At the top there are the rectangles with lower values of ​​objectness, ie more
// likely to have objects in them.
objBoundingBox = finalBoxes.getSortedStructVal();
//vector<Vec4i> >
//objBoundingBox = finalBoxes.getSortedStructVal();
/* vector<Vec4i> tmp=finalBoxes.getSortedStructVal();
objBoundingBox.create(tmp.size(), 1, CV_8U);
Mat obj= objBoundingBox.getMat();
obj=tmp;*/
Mat obj = objBoundingBox.getMat();
obj = Mat(finalBoxes.getSortedStructVal());
/* Mat obj2 = objBoundingBox.getMatRef();
obj2 = Mat(finalBoxes.getSortedStructVal());*/
// List of the rectangles' objectness value
unsigned long int valIdxesSize = finalBoxes.valIdxes.size();
unsigned long int valIdxesSize = finalBoxes.getvalIdxes().size();
objectnessValues.resize( valIdxesSize );
for ( uint i = 0; i < valIdxesSize; i++ )
objectnessValues[i] = finalBoxes.valIdxes[i].first;
objectnessValues[i] = finalBoxes.getvalIdxes()[i].first;
return true;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment