Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
tb-studies
ex3
Commits
8534cd60
Commit
8534cd60
authored
Apr 17, 2016
by
Tadej Borovšak
Browse files
Add trackers
parent
343f2e7f
Changes
2
Hide whitespace changes
Inline
Side-by-side
track-model.cpp
0 → 100644
View file @
8534cd60
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <dirent.h>
#include <sys/types.h>
#include <math.h>
#include <assert.h>
using
namespace
std
;
using
namespace
cv
;
#define WIN "test"
#define SCALE 0.003921569f
typedef
struct
{
Point
tl
,
br
;
Mat
img
;
}
Data
;
static
Scalar
BLACK
(
0
,
0
,
0
);
static
Scalar
RED
(
0
,
0
,
255
);
static
Scalar
GREEN
(
0
,
255
,
0
);
static
Scalar
BLUE
(
255
,
0
,
0
);
static
inline
void
draw_cross
(
Mat
&
img
,
Point
const
&
point
,
Scalar
const
&
color
)
{
Point
d
(
3
,
3
);
line
(
img
,
point
-
d
,
point
+
d
,
color
,
1
,
LINE_AA
,
0
);
d
=
Point
(
-
3
,
3
);
line
(
img
,
point
-
d
,
point
+
d
,
color
,
1
,
LINE_AA
,
0
);
}
static
inline
Mat
load_image
(
string
const
&
name
)
{
Mat
tmp
=
imread
(
name
);
tmp
.
convertTo
(
tmp
,
CV_32FC3
,
SCALE
);
return
tmp
;
}
static
vector
<
string
>
read_directory
(
char
const
*
path
)
{
vector
<
string
>
result
;
dirent
*
de
;
DIR
*
dp
;
string
prefix
(
path
);
dp
=
opendir
(
path
);
if
(
dp
)
{
while
(
true
)
{
de
=
readdir
(
dp
);
if
(
de
==
NULL
)
break
;
if
(
strlen
(
de
->
d_name
)
>
5
)
result
.
push_back
(
prefix
+
string
(
de
->
d_name
));
}
closedir
(
dp
);
sort
(
result
.
begin
(),
result
.
end
());
}
return
result
;
}
static
Mat
get_avg_image
(
vector
<
string
>
&
seq
)
{
Mat
img
=
load_image
(
seq
[
0
]);
for
(
unsigned
int
i
=
1
;
i
<
seq
.
size
();
i
++
)
img
+=
load_image
(
seq
[
i
]);
return
img
/
seq
.
size
();
}
static
inline
bool
is_valid_detection
(
int
area
,
Point
const
&
curr
,
Point
const
&
prev
)
{
int
max_dist
=
20
;
int
min_area
=
10
;
/* Area needs to be large enough */
if
(
area
<
min_area
)
return
false
;
/* If no previous localtion, we use this detection */
if
(
prev
.
x
<
0
)
return
true
;
/* Make sure we're not too far from previous position */
if
(
abs
(
curr
.
x
-
prev
.
x
)
>
max_dist
)
return
false
;
if
(
abs
(
curr
.
y
-
prev
.
y
)
>
max_dist
)
return
false
;
return
true
;
}
static
Point
detect_object
(
Mat
const
&
stats
,
Mat
const
&
centroids
,
Point
const
&
prev
)
{
int
limit
=
stats
.
size
().
height
;
vector
<
Point
>
candidates
;
vector
<
int
>
areas
;
for
(
int
i
=
1
;
i
<
limit
;
i
++
)
{
Point
curr
(
centroids
.
at
<
double
>
(
i
,
0
),
centroids
.
at
<
double
>
(
i
,
1
));
if
(
is_valid_detection
(
stats
.
at
<
int
>
(
i
,
CC_STAT_AREA
),
curr
,
prev
))
{
candidates
.
push_back
(
curr
);
areas
.
push_back
(
stats
.
at
<
int
>
(
i
,
CC_STAT_AREA
));
}
}
if
(
candidates
.
size
()
==
0
)
/* No detection in this frame */
return
Point
(
-
1
,
-
1
);
/* Find largets area */
int
best
=
0
;
for
(
unsigned
int
i
=
1
;
i
<
candidates
.
size
();
i
++
)
if
(
areas
[
i
]
>
areas
[
best
])
best
=
i
;
return
candidates
[
best
];
}
int
main
(
int
,
char
**
argv
)
{
vector
<
string
>
seq
=
read_directory
(
argv
[
1
]);
Mat
avg
=
get_avg_image
(
seq
);
imshow
(
WIN
,
avg
);
waitKey
();
Point
p
(
-
1
,
-
1
);
for
(
unsigned
int
i
=
0
;
i
<
seq
.
size
();
i
++
)
{
Mat
img
=
load_image
(
seq
[
i
]);
Mat
diff
=
cv
::
abs
(
avg
-
img
);
cvtColor
(
diff
,
diff
,
CV_BGR2GRAY
);
threshold
(
diff
,
diff
,
0.3
,
1.0
,
THRESH_BINARY
);
diff
.
convertTo
(
diff
,
CV_8U
,
255
);
morphologyEx
(
diff
,
diff
,
MORPH_CLOSE
,
getStructuringElement
(
MORPH_RECT
,
Size
(
5
,
5
)));
Mat
labels
,
stats
,
centroids
;
connectedComponentsWithStats
(
diff
,
labels
,
stats
,
centroids
);
Point
detected
=
detect_object
(
stats
,
centroids
,
p
);
cvtColor
(
diff
,
diff
,
CV_GRAY2BGR
);
if
(
detected
.
x
>=
0
)
{
p
=
detected
;
draw_cross
(
diff
,
p
,
RED
);
}
imshow
(
WIN
,
diff
);
waitKey
();
}
return
0
;
}
track.cpp
0 → 100644
View file @
8534cd60
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <dirent.h>
#include <sys/types.h>
#include <math.h>
#include <assert.h>
using
namespace
std
;
using
namespace
cv
;
#define WIN "test"
#define SCALE 0.003921569f
typedef
struct
{
Point
tl
,
br
;
Mat
img
;
}
Data
;
static
Scalar
BLACK
(
0
,
0
,
0
);
static
Scalar
RED
(
0
,
0
,
255
);
static
Scalar
GREEN
(
0
,
255
,
0
);
static
Scalar
BLUE
(
255
,
0
,
0
);
static
inline
void
draw_cross
(
Mat
&
img
,
Point
const
&
point
,
Scalar
const
&
color
)
{
Point
d
(
3
,
3
);
line
(
img
,
point
-
d
,
point
+
d
,
color
,
1
,
LINE_AA
,
0
);
d
=
Point
(
-
3
,
3
);
line
(
img
,
point
-
d
,
point
+
d
,
color
,
1
,
LINE_AA
,
0
);
}
static
inline
Mat
load_image
(
string
const
&
name
)
{
Mat
tmp
=
imread
(
name
);
tmp
.
convertTo
(
tmp
,
CV_32FC3
,
SCALE
);
return
tmp
;
}
static
vector
<
string
>
read_directory
(
char
const
*
path
)
{
vector
<
string
>
result
;
dirent
*
de
;
DIR
*
dp
;
string
prefix
(
path
);
dp
=
opendir
(
path
);
if
(
dp
)
{
while
(
true
)
{
de
=
readdir
(
dp
);
if
(
de
==
NULL
)
break
;
if
(
strlen
(
de
->
d_name
)
>
5
)
result
.
push_back
(
prefix
+
string
(
de
->
d_name
));
}
closedir
(
dp
);
sort
(
result
.
begin
(),
result
.
end
());
}
return
result
;
}
static
Mat
get_avg_image
(
vector
<
string
>
&
seq
)
{
Mat
img
=
load_image
(
seq
[
0
]);
for
(
unsigned
int
i
=
1
;
i
<
seq
.
size
();
i
++
)
img
+=
load_image
(
seq
[
i
]);
return
img
/
seq
.
size
();
}
static
inline
bool
is_valid_detection
(
int
area
,
Point
const
&
curr
,
Point
const
&
prev
)
{
int
max_dist
=
20
;
int
min_area
=
10
;
/* Area needs to be large enough */
if
(
area
<
min_area
)
return
false
;
/* If no previous localtion, we use this detection */
if
(
prev
.
x
<
0
)
return
true
;
/* Make sure we're not too far from previous position */
if
(
abs
(
curr
.
x
-
prev
.
x
)
>
max_dist
)
return
false
;
if
(
abs
(
curr
.
y
-
prev
.
y
)
>
max_dist
)
return
false
;
return
true
;
}
static
Point
detect_object
(
Mat
const
&
stats
,
Mat
const
&
centroids
,
Point
const
&
prev
)
{
int
limit
=
stats
.
size
().
height
;
vector
<
Point
>
candidates
;
vector
<
int
>
areas
;
for
(
int
i
=
1
;
i
<
limit
;
i
++
)
{
Point
curr
(
centroids
.
at
<
double
>
(
i
,
0
),
centroids
.
at
<
double
>
(
i
,
1
));
if
(
is_valid_detection
(
stats
.
at
<
int
>
(
i
,
CC_STAT_AREA
),
curr
,
prev
))
{
candidates
.
push_back
(
curr
);
areas
.
push_back
(
stats
.
at
<
int
>
(
i
,
CC_STAT_AREA
));
}
}
if
(
candidates
.
size
()
==
0
)
/* No detection in this frame */
return
Point
(
-
1
,
-
1
);
/* Find largets area */
int
best
=
0
;
for
(
unsigned
int
i
=
1
;
i
<
candidates
.
size
();
i
++
)
if
(
areas
[
i
]
>
areas
[
best
])
best
=
i
;
return
candidates
[
best
];
}
int
main
(
int
,
char
**
argv
)
{
vector
<
string
>
seq
=
read_directory
(
argv
[
1
]);
Mat
avg
=
get_avg_image
(
seq
);
imshow
(
WIN
,
avg
);
waitKey
();
Point
p
(
-
1
,
-
1
);
for
(
unsigned
int
i
=
0
;
i
<
seq
.
size
();
i
++
)
{
Mat
img
=
load_image
(
seq
[
i
]);
Mat
diff
=
cv
::
abs
(
avg
-
img
);
cvtColor
(
diff
,
diff
,
CV_BGR2GRAY
);
threshold
(
diff
,
diff
,
0.3
,
1.0
,
THRESH_BINARY
);
diff
.
convertTo
(
diff
,
CV_8U
,
255
);
morphologyEx
(
diff
,
diff
,
MORPH_CLOSE
,
getStructuringElement
(
MORPH_RECT
,
Size
(
5
,
5
)));
Mat
labels
,
stats
,
centroids
;
connectedComponentsWithStats
(
diff
,
labels
,
stats
,
centroids
);
Point
detected
=
detect_object
(
stats
,
centroids
,
p
);
cvtColor
(
diff
,
diff
,
CV_GRAY2BGR
);
if
(
detected
.
x
>=
0
)
{
p
=
detected
;
draw_cross
(
diff
,
p
,
RED
);
}
imshow
(
WIN
,
diff
);
waitKey
();
}
return
0
;
}
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