Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv_contrib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv_contrib
Commits
d9728347
Commit
d9728347
authored
Jun 13, 2014
by
jaco
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
BING Objectness porting progress II
parent
dae0dc9c
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
239 additions
and
66 deletions
+239
-66
computeSaliency.cpp
modules/saliency/samples/computeSaliency.cpp
+27
-9
CmShow.cpp
modules/saliency/src/CmShow.cpp
+83
-0
CmShow.h
modules/saliency/src/CmShow.h
+9
-0
ValStructVec.h
modules/saliency/src/ValStructVec.h
+86
-42
objectnessBING.cpp
modules/saliency/src/objectnessBING.cpp
+34
-15
No files found.
modules/saliency/samples/computeSaliency.cpp
View file @
d9728347
...
...
@@ -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
(
saliency
Algorithm
->
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
;
}
}
...
...
modules/saliency/src/CmShow.cpp
0 → 100644
View file @
d9728347
#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
);
}
modules/saliency/src/CmShow.h
0 → 100644
View file @
d9728347
#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
);
};
modules/saliency/src/ValStructVec.h
View file @
d9728347
...
...
@@ -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
;
}
/*
v
oid valStructVecDemo
()
template
<
typename
VT
,
typename
ST
>
v
ector
<
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]));
}
*/
modules/saliency/src/objectnessBING.cpp
View file @
d9728347
...
...
@@ -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
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment