/
landingGuide.c
287 lines (201 loc) · 6.83 KB
/
landingGuide.c
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* landingGuide.c
*
* The main landing guide system file. Initialises the landing guide system
*/
#include "SDL/SDL.h"
#include "frame.h"
#include "video.h"
#include "two_pass.h"
#define END_DELAY 200
SDL_Surface *frame = NULL;
SDL_Surface *screen = NULL;
int xBox = 350, yBox = 200;
//----------------my function prototypes-----------------
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination );
SDL_Surface* create_cam_img (unsigned char *pixels, int w, int h);
static void mainloop (void);
void process_video(unsigned char *pixels, unsigned char processedPixels[][FRAME_WIDTH]);
int main (int argc, char **argv) {
dev_name = "/dev/video0";
io = IO_METHOD_MMAP;
pixels = (unsigned char *) malloc (sizeof(unsigned char) * FRAME_HEIGHT * FRAME_WIDTH * 3);
//Start SDL
SDL_Init( SDL_INIT_EVERYTHING );
screen = SDL_SetVideoMode(FRAME_WIDTH, FRAME_HEIGHT, 24, SDL_SWSURFACE);
//init_tracker();
open_device ();
init_device ();
start_capturing ();
mainloop ();
stop_capturing ();
uninit_device ();
close_device ();
SDL_Delay(END_DELAY);
SDL_FreeSurface(frame);
//Quit SDL
SDL_Quit();
free(pixels);
//cleanup_tracker();
exit (EXIT_SUCCESS);
return 0;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
//Make a temporary rectangle to hold the offsets
SDL_Rect offset;
//Give the offsets to the rectangle
offset.x = x;
offset.y = y;
//Blit the surface
SDL_BlitSurface( source, NULL, destination, &offset );
}
SDL_Surface* create_cam_img (unsigned char *pixels, int w,int h)
{
int bpp,pitch;
Uint32 rmask, gmask, bmask, amask;
bpp=24;
pitch = w * 3;
amask = 0;
bmask = 0xff0000;
gmask = 0x00ff00;
rmask = 0x0000ff;
return SDL_CreateRGBSurfaceFrom (pixels,w,h,bpp,pitch,rmask,gmask,bmask,amask);
}
void draw_collinear(unsigned char *pixels, collinear co) {
coord p1 = co.point1;
coord p2 = co.point2;
coord p3 = co.point3;
draw_box(pixels, p1.x, p1.y, BOX_WIDTH, BOX_HEIGHT);
draw_box(pixels, p2.x, p2.y, BOX_WIDTH, BOX_HEIGHT);
draw_box(pixels, p3.x, p3.y, BOX_WIDTH, BOX_HEIGHT);
}
static void mainloop (void) {
int i,j;
unsigned char processedPixels[FRAME_HEIGHT][FRAME_WIDTH];
for (;;) {
fd_set fds;
struct timeval tv;
int r;
FD_ZERO (&fds);
FD_SET (fd, &fds);
/* Timeout. */
tv.tv_sec = 2;
tv.tv_usec = 0;
r = select (fd + 1, &fds, NULL, NULL, &tv);
if (-1 == r) {
if (EINTR == errno)
continue;
errno_exit ("select");
}
if (0 == r) {
fprintf (stderr, "select timeout\n");
exit (EXIT_FAILURE);
}
if (read_frame ()) {
// process the video
process_video(pixels, processedPixels);
// convert grayscale processed pixels to an RGB format
convert_grayscale_to_rgb(pixels, processedPixels,FRAME_WIDTH, FRAME_HEIGHT);
// process blobs
int labels[FRAME_HEIGHT][FRAME_WIDTH];
for(i = 0; i < FRAME_HEIGHT; i++) {
for(j = 0; j < FRAME_WIDTH; j++) {
labels[i][j] = -1;
}
}
int numBlobs = two_pass(processedPixels, labels, FRAME_WIDTH, FRAME_HEIGHT);
blob blobs[MAX_BLOBS];
extract_blobs(blobs, numBlobs, labels, FRAME_WIDTH, FRAME_HEIGHT);
// remove too small/big blobs
numBlobs = apply_blob_size_heuristic(blobs, numBlobs);
// get all center points for blobs
coord centerCoords[MAX_BLOBS];// = (coord*)malloc(sizeof(coord) * numBlobs);
get_blob_centers(blobs, numBlobs, centerCoords);
collinear points[MAX_BLOBS];
int shortSegments = get_more_straight_sides(centerCoords, numBlobs, points);
coord shapeCoords[5];
int shapeFound = 0;
for(i = 0; i < shortSegments; i++) {
coord p1 = points[i].point1;
coord p2 = points[i].point2;
coord p3 = points[i].point3;
double m1 = fabs(distance(p1, p2));
double m2 = fabs(distance(p2, p3));
double m3 = fabs(distance(p1, p3));
if(is_long_side(points[i])) {
collinear shortSide;
int shortSideFound = get_short_side(centerCoords, numBlobs, points[i], &shortSide);
if(shortSideFound) {
draw_collinear(pixels, shortSide);
draw_collinear(pixels, points[i]);
/*print_point(p1);
print_point(p2);
print_point(p3); */
//printf(" -------------------- SHORT SIDE \n");
/*
print_point(shortSide.point1);
print_point(shortSide.point2);
print_point(shortSide.point3);*/
printf("\n");
shapeCoords[0] = p1;
shapeCoords[1] = p2;
shapeCoords[2] = p3;
shapeCoords[3] = shortSide.point1;
shapeCoords[4] = shortSide.point3;
shapeFound = 1;
break;
}
} else if(is_short_side(points[i])) {
collinear longSide;
int longSideFound = get_long_side(centerCoords, numBlobs, points[i], &longSide);
if(longSideFound) {
draw_collinear(pixels, longSide);
draw_collinear(pixels, points[i]);
/*
print_point(p1);
print_point(p2);
print_point(p3); */
//printf(" ======================== LONG SIDE\n");
/*
print_point(longSide.point1);
print_point(longSide.point2);
print_point(longSide.point3); */
printf("\n");
shapeCoords[0] = p1;
shapeCoords[1] = p2;
shapeCoords[2] = p3;
shapeCoords[3] = longSide.point1;
shapeCoords[4] = longSide.point3;
shapeFound = 1;
break;
}
}
}
if(shapeFound) {
int imageCenterX = FRAME_WIDTH / 2;
int imageCenterY = FRAME_HEIGHT / 2;
coord center;
center.x = imageCenterX;
center.y = imageCenterY;
double distance = distance_from_center(shapeCoords[1]);
double angle = quadrant_angle(shapeCoords[1]);
if(distance > 100) {
printf("MOVE TOWARDS CENTER :: ANGLE : %f :: DISTANCE : %f(pixels)\n",
angle, distance);
//print_direction(angle);
} else {
printf("MOVE DOWN HEIGHT : ?\n");
}
print_image(centerCoords, numBlobs, shapeCoords, 5);
}
free_blobs(blobs, numBlobs);
frame = create_cam_img(pixels, FRAME_WIDTH, FRAME_HEIGHT);
apply_surface(0, 0, frame, screen);
SDL_Flip(screen);
}
}
}
void process_video(unsigned char *pixels, unsigned char processedPixels[][FRAME_WIDTH]) {
filter(pixels, processedPixels, FRAME_WIDTH, FRAME_HEIGHT);
}