forked from blackball/ct
/
test_imglist.cpp
172 lines (147 loc) · 3.78 KB
/
test_imglist.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "imgqueue.h"
#include "ct.h"
#include <stdio.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#define MAX_OBJECTS 10
struct region_params {
int n;
const char * win_name;
CvPoint loc0[ MAX_OBJECTS ];
CvPoint loc1[ MAX_OBJECTS ];
IplImage * objects[ MAX_OBJECTS ];
IplImage * orig_img;
IplImage * cur_img;
};
static void
_mouse_cb(int e, int x, int y, int flags, void * _p) {
struct region_params * p = (struct region_params *)_p;
CvPoint *loc;
int n;
IplImage * tmp;
static int pressed = FALSE;
switch( e ) {
case CV_EVENT_LBUTTONDOWN:
n = p->n;
if( n == MAX_OBJECTS )
return;
loc = p->loc0;
loc[n].x = x;
loc[n].y = y;
pressed = TRUE;
break;
/* on left button up, finalize the rectangle and draw it in black */
case CV_EVENT_LBUTTONUP:
n = p->n;
if( n == MAX_OBJECTS )
return;
loc = p->loc1;
loc[n].x = x;
loc[n].y = y;
cvReleaseImage( &(p->cur_img) );
p->cur_img = NULL;
cvRectangle( p->orig_img, p->loc0[n], loc[n], CV_RGB(0,0,0), 1, 8, 0 );
cvShowImage( p->win_name, p->orig_img );
pressed = FALSE;
p->n++;
break;
case CV_EVENT_MOUSEMOVE:
if (flags & CV_EVENT_FLAG_LBUTTON) {
n = p->n;
if( n == MAX_OBJECTS )
return;
tmp = cvCloneImage( p->orig_img );
loc = p->loc0;
cvRectangle( tmp, loc[n], cvPoint(x, y), CV_RGB(255,255,255), 1, 8, 0 );
cvShowImage( p->win_name, tmp );
if( p->cur_img )
cvReleaseImage( &(p->cur_img) );
p->cur_img = tmp;
}
break;
}
}
static int
_get_regions(const IplImage *frame, CvRect **regions) {
const char * win_name = "select regions";
struct region_params params;
CvRect *r;
int i, x0, y0, x1, y1, w, h;
params.win_name = win_name;
params.orig_img = cvCloneImage(frame);
params.cur_img = NULL;
params.n = 0;
cvNamedWindow( win_name, 1 );
cvShowImage(win_name, frame);
cvSetMouseCallback( win_name, &_mouse_cb, ¶ms);
cvWaitKey(0);
cvDestroyWindow( win_name );
cvReleaseImage( &(params.orig_img) );
if ( params.cur_img )
cvReleaseImage( &(params.cur_img) );
if ( params.n == 0 ) {
*regions = NULL;
return 0;
}
r = (CvRect *)malloc( params.n * sizeof(CvRect) );
/* temporal min, max */
#define _tmin(a,b) ((a) > (b) ? (b) : (a))
#define _tmax(a,b) ((a) < (b) ? (b) : (a))
for (i = 0; i < params.n; ++i) {
x0 = _tmin(params.loc0[i].x, params.loc1[i].x);
x1 = _tmax(params.loc0[i].x, params.loc1[i].x);
y0 = _tmin(params.loc0[i].y, params.loc1[i].y);
y1 = _tmax(params.loc0[i].y, params.loc1[i].y);
w = x1 - x0;
h = y1 - y0;
r[i] = cvRect(x0, y0, w, h);
}
*regions = r;
return params.n;
}
static void
_test_imglist(const char *vname) {
struct ct_t **pcts;
CvRect *objects;
int n = 0;
int i;
const int gray_mode = 0;
ImgQueue queue;
IplImage *gray = 0;
queue.open(vname);
gray = queue.nextImg(gray_mode);
n = _get_regions(gray, &objects);
if (n < 1) {
cvReleaseImage(&gray);
return ;
}
pcts = (struct ct_t **)malloc(n * sizeof(struct ct_t **));
for (i = 0; i < n; ++i) {
pcts[i] = ct_new();
ct_init(pcts[i], gray, &objects[i]);
}
cvNamedWindow("tracking", 1);
while ( gray = queue.nextImg(gray_mode) ) {
#define _draw(f, r) \
cvRectangle(f, cvPoint((r).x, (r).y), \
cvPoint((r).x+(r).width, (r).y+(r).height), \
cvScalar(255,255,255,0), 2,8,0)
for (i = 0; i < n; ++i) {
ct_update(pcts[i], gray, &objects[i]);
_draw(gray, objects[i]);
}
cvShowImage("tracking", gray);
if (27 == cvWaitKey(1)) break;
}
#undef _draw
cvDestroyWindow("tracking");
for (i = 0; i < n; ++i) {
ct_free( &pcts[i] );
}
free( pcts );
free( objects );
}
int main(int argc ,char *argv) {
_test_imglist("astri_1.txt");
return 0;
}