-
Notifications
You must be signed in to change notification settings - Fork 43
/
display-leap-data-opencv.c
148 lines (117 loc) · 2.96 KB
/
display-leap-data-opencv.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
/*
** Author: Elina Lijouvni
** License: GPL v3
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <cv.h>
#include <highgui.h>
typedef struct ctx_s ctx_t;
struct ctx_s
{
int quit;
};
#if 0
static void
fprintf_data(FILE *fp, const char * title, unsigned char *data, size_t size)
{
int i;
printf("%s", title);
for (i = 0; i < size; i++) {
if ( ! (i % 16) )
printf("\n");
printf("%02x ", data[i]);
}
printf("\n");
}
#endif
#define VFRAME_WIDTH 640
#define VFRAME_HEIGHT 240
#define VFRAME_SIZE (VFRAME_WIDTH * VFRAME_HEIGHT)
typedef struct frame_s frame_t;
struct frame_s
{
IplImage* frame;
uint32_t id;
uint32_t data_len;
uint32_t state;
};
#define UVC_STREAM_EOF (1 << 1)
static void
process_video_frame(ctx_t *ctx, frame_t *frame)
{
int key;
cvShowImage("mainWin", frame->frame );
key = cvWaitKey(1);
if (key == 'q' || key == 0x1B)
ctx->quit = 1;
}
static void
process_usb_frame(ctx_t *ctx, frame_t *frame, unsigned char *data, int size)
{
int i;
int bHeaderLen = data[0];
int bmHeaderInfo = data[1];
uint32_t dwPresentationTime = *( (uint32_t *) &data[2] );
//printf("frame time: %u\n", dwPresentationTime);
if (frame->id == 0)
frame->id = dwPresentationTime;
for (i = bHeaderLen; i < size ; i += 2) {
if (frame->data_len >= VFRAME_SIZE)
break ;
CvScalar s;
s.val[2] = data[i];
s.val[1] = data[i+1];
s.val[0] = 0;
int x = frame->data_len % VFRAME_WIDTH;
int y = frame->data_len / VFRAME_WIDTH;
cvSet2D(frame->frame, 2 * y, x, s);
cvSet2D(frame->frame, 2 * y + 1, x, s);
frame->data_len++;
}
if (bmHeaderInfo & UVC_STREAM_EOF) {
//printf("End-of-Frame. Got %i\n", frame->data_len);
if (frame->data_len != VFRAME_SIZE) {
//printf("wrong frame size got %i expected %i\n", frame->data_len, VFRAME_SIZE);
frame->data_len = 0;
frame->id = 0;
return ;
}
process_video_frame(ctx, frame);
frame->data_len = 0;
frame->id = 0;
}
else {
if (dwPresentationTime != frame->id && frame->id > 0) {
//printf("mixed frame TS: dropping frame\n");
frame->id = dwPresentationTime;
/* frame->data_len = 0; */
/* frame->id = 0; */
/* return ; */
}
}
}
int
main(int argc, char *argv[])
{
ctx_t ctx_data;
memset(&ctx_data, 0, sizeof (ctx_data));
ctx_t *ctx = &ctx_data;
cvNamedWindow("mainWin", 0);
cvResizeWindow("mainWin", VFRAME_WIDTH, VFRAME_HEIGHT * 2);
frame_t frame;
memset(&frame, 0, sizeof (frame));
frame.frame = cvCreateImage( cvSize(VFRAME_WIDTH, 2 * VFRAME_HEIGHT), IPL_DEPTH_8U, 3);
for ( ; ; ) {
unsigned char data[16384];
int usb_frame_size;
if ( feof(stdin) || ctx->quit )
break ;
fread(&usb_frame_size, sizeof (usb_frame_size), 1, stdin);
fread(data, usb_frame_size, 1, stdin);
process_usb_frame(ctx, &frame, data, usb_frame_size);
}
cvReleaseImage(&frame.frame);
return (0);
}