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 ) ...@@ -58,7 +58,9 @@ int main( int argc, char** argv )
return -1; return -1;
} }
Mat image, saliencyMap, binaryMap; Mat binaryMap;
Mat image;
//OutputArray saliencyMap( image );
cap >> frame; cap >> frame;
if( frame.empty() ) if( frame.empty() )
...@@ -68,15 +70,31 @@ int main( int argc, char** argv ) ...@@ -68,15 +70,31 @@ int main( int argc, char** argv )
frame.copyTo( image ); frame.copyTo( image );
if( saliencyAlgorithm->computeSaliency( image, saliencyMap ) ) if( saliency_algorithm.find( "SPECTRAL_RESIDUAL" ) == 0 )
{ {
StaticSaliencySpectralResidual spec; Mat saliencyMap;
spec.computeBinaryMap( saliencyMap, binaryMap ); if( saliencyAlgorithm->computeSaliency( image, saliencyMap ) )
//saliencyAlgorithm->computeBinaryMap( saliencyMap, binaryMap ); {
imshow( "Saliency Map", saliencyMap ); StaticSaliencySpectralResidual spec;
imshow( "Original Image", image ); //Mat salMat=saliencyMap.getMat();
imshow( "Binary Map", binaryMap ); spec.computeBinaryMap( saliencyMap, binaryMap );
waitKey( 0 ); //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 @@ ...@@ -7,66 +7,110 @@
template<typename VT, typename ST> template<typename VT, typename ST>
struct ValStructVec struct ValStructVec
{ {
ValStructVec(){clear();} ValStructVec()
inline int size() const {return sz;} {
inline void clear() {sz = 0; structVals.clear(); valIdxes.clear();} 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 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 VT& operator ()( int i ) const
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 return valIdxes[i].first;
inline ST& operator [](int i) {return structVals[valIdxes[i].second];} // Should be called after sort } // 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); void sort( bool descendOrder = true );
const vector<ST> &getSortedStructVal(); const vector<ST> &getSortedStructVal();
void append(const ValStructVec<VT, ST> &newVals, int startV = 0); 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: private:
int sz; // size of the value struct vector int sz; // size of the value struct vector
vector<pair<VT, int>> valIdxes; // Indexes after sort vector<pair<VT, int>> valIdxes; // Indexes after sort
bool smaller() {return true;}; bool smaller()
vector<ST> sortedStructVals; {
return true;
}
;
vector<ST> sortedStructVals;
}; };
template<typename VT, typename ST> 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(); int sz = newVals.size();
for (int i = 0; i < sz; i++) for ( int i = 0; i < sz; i++ )
pushBack((float)((i+300)*startV)/*newVals(i)*/, newVals[i]); pushBack( (float) ( ( i + 300 ) * startV )/*newVals(i)*/, newVals[i] );
} }
template<typename VT, typename ST> template<typename VT, typename ST>
void ValStructVec<VT, ST>::sort(bool descendOrder /* = true */) void ValStructVec<VT, ST>::sort( bool descendOrder /* = true */)
{ {
if (descendOrder) if( descendOrder )
std::sort(valIdxes.begin(), valIdxes.end(), std::greater<pair<VT, int>>()); std::sort( valIdxes.begin(), valIdxes.end(), std::greater<pair<VT, int>>() );
else else
std::sort(valIdxes.begin(), valIdxes.end(), std::less<pair<VT, int>>()); std::sort( valIdxes.begin(), valIdxes.end(), std::less<pair<VT, int>>() );
} }
template<typename VT, typename ST> template<typename VT, typename ST>
const vector<ST>& ValStructVec<VT, ST>::getSortedStructVal() const vector<ST>& ValStructVec<VT, ST>::getSortedStructVal()
{ {
sortedStructVals.resize(sz); sortedStructVals.resize( sz );
for (int i = 0; i < sz; i++) for ( int i = 0; i < sz; i++ )
sortedStructVals[i] = structVals[valIdxes[i].second]; sortedStructVals[i] = structVals[valIdxes[i].second];
return sortedStructVals; return sortedStructVals;
} }
/* template<typename VT, typename ST>
void valStructVecDemo() vector<pair<VT, int>> ValStructVec<VT, ST>::getvalIdxes()
{ {
ValStructVec<int, string> sVals; return valIdxes;
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]));
} }
*/
/*
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() ...@@ -56,11 +56,11 @@ ObjectnessBING::ObjectnessBING()
_base = 2; // base for window size quantization _base = 2; // base for window size quantization
_W = 8; // feature window size (W, W) _W = 8; // feature window size (W, W)
_NSS = 2; //non-maximal suppress size NSS _NSS = 2; //non-maximal suppress size NSS
_logBase=log( _base ); _logBase = log( _base );
_minT=cvCeil( log( 10. ) / _logBase ); _minT = cvCeil( log( 10. ) / _logBase );
_maxT=cvCeil( log( 500. ) / _logBase ); _maxT = cvCeil( log( 500. ) / _logBase );
_numT= _maxT - _minT + 1 ; _numT = _maxT - _minT + 1;
_Clr= MAXBGR ; _Clr = MAXBGR;
setColorSpace( _Clr ); setColorSpace( _Clr );
...@@ -75,7 +75,8 @@ ObjectnessBING::~ObjectnessBING() ...@@ -75,7 +75,8 @@ ObjectnessBING::~ObjectnessBING()
void ObjectnessBING::setColorSpace( int clr ) void ObjectnessBING::setColorSpace( int clr )
{ {
_Clr = 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()); // _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()); // _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 ) ...@@ -368,6 +369,8 @@ void ObjectnessBING::gradientXY( CMat &x1i, CMat &y1i, Mat &mag1u )
void ObjectnessBING::getObjBndBoxesForSingleImage( Mat img, ValStructVec<float, Vec4i> &finalBoxes, int numDetPerSize ) void ObjectnessBING::getObjBndBoxesForSingleImage( Mat img, ValStructVec<float, Vec4i> &finalBoxes, int numDetPerSize )
{ {
ValStructVec<float, Vec4i> boxes;
finalBoxes.reserve( 10000 );
int scales[3] = int scales[3] =
{ 1, 3, 5 }; { 1, 3, 5 };
...@@ -378,7 +381,8 @@ void ObjectnessBING::getObjBndBoxesForSingleImage( Mat img, ValStructVec<float, ...@@ -378,7 +381,8 @@ void ObjectnessBING::getObjBndBoxesForSingleImage( Mat img, ValStructVec<float,
CmTimer tm( "Predict" ); CmTimer tm( "Predict" );
tm.Start(); tm.Start();
getObjBndBoxes( img, finalBoxes, numDetPerSize ); getObjBndBoxes( img, boxes, numDetPerSize );
finalBoxes.append( boxes, scales[clr] );
tm.Stop(); tm.Stop();
printf( "Average time for predicting an image (%s) is %gs\n", _clrName[_Clr], tm.TimeInSeconds() ); 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 ) ...@@ -407,12 +411,13 @@ std::string inline removeExtension( std::string const& filename )
// Read matrix from binary file // Read matrix from binary file
bool ObjectnessBING::matRead( const string& filename, Mat& _M ) 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; Mat M;
fs2[removeExtension( basename( fileNameString ) )] >> M; fs2[String(removeExtension( basename( filename ) ).c_str())] >> M;
/*FILE* f = fopen(_S(filename), "rb"); /*FILE* f = fopen(_S(filename), "rb");
if (f == NULL) if (f == NULL)
...@@ -449,19 +454,33 @@ void ObjectnessBING::write( cv::FileStorage& fs ) const ...@@ -449,19 +454,33 @@ void ObjectnessBING::write( cv::FileStorage& fs ) const
bool ObjectnessBING::computeSaliencyImpl( const InputArray image, OutputArray objBoundingBox ) bool ObjectnessBING::computeSaliencyImpl( const InputArray image, OutputArray objBoundingBox )
{ {
ValStructVec<float, Vec4i> &finalBoxes; ValStructVec<float, Vec4i> finalBoxes;
getObjBndBoxesForSingleImage( image.getMat(), finalBoxes, 250 ); getObjBndBoxesForSingleImage( image.getMat(), finalBoxes, 250 );
// List of rectangles returned by objectess function in ascending order. // List of rectangles returned by objectess function in ascending order.
// At the top there are the rectangles with lower values of ​​objectness, ie more // At the top there are the rectangles with lower values of ​​objectness, ie more
// likely to have objects in them. // 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 // List of the rectangles' objectness value
unsigned long int valIdxesSize = finalBoxes.valIdxes.size(); unsigned long int valIdxesSize = finalBoxes.getvalIdxes().size();
objectnessValues.resize( valIdxesSize ); objectnessValues.resize( valIdxesSize );
for ( uint i = 0; i < valIdxesSize; i++ ) for ( uint i = 0; i < valIdxesSize; i++ )
objectnessValues[i] = finalBoxes.valIdxes[i].first; objectnessValues[i] = finalBoxes.getvalIdxes()[i].first;
return true; 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