-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSource.cpp
204 lines (156 loc) · 5.16 KB
/
Source.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
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
#include <stdio.h>
#include <opencv\cv.h>
#include <opencv\cxcore.h>
#include <opencv\highgui.h>
#include <string.h>
#include "vc.h"
#include "labelling.h"
#include "ca.h"
int main(void)
{
// Vídeo
char *videofile = "video-tp2.avi";
CvCapture *capture;
IplImage *frame = NULL;
//Variaveis trabalho
IVC *ivcFrame;
IVC *hsvFrame;
IVC *binaryFrame;
IVC *labels;
OVC *blobsAtual;
OVC *blobsAnterior;
int nlabels = 0;
int nlabelsOld = 0;
int frameJump = 10; //Quantos frames deverão ser ignorados apos ser analizado 1
int countJump = 0;
bool firstFrame = true;
Fruta frutas[10];
int nFrutas = 0;
struct
{
int width, height;
int ntotalframes;
int fps;
int nframe;
} video;
// Texto
CvFont font, fontbkg;
double hScale = 0.5;
double vScale = 0.5;
int lineWidth = 1;
char str[500] = { 0 };
// Outros
int key = 0;
/* Leitura de vídeo de um ficheiro */
/* NOTA IMPORTANTE:
O ficheiro video-tp2.avi deverá estar localizado no mesmo directório que o ficheiro de código fonte.
*/
capture = cvCaptureFromFile(videofile);
/* Verifica se foi possível abrir o ficheiro de vídeo */
if (!capture)
{
fprintf(stderr, "Erro ao abrir o ficheiro de vídeo!\n");
return 1;
}
/* Número total de frames no vídeo */
video.ntotalframes = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT);
/* Frame rate do vídeo */
video.fps = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
/* Resolução do vídeo */
video.width = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);
video.height = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
//Alocar memória para as imagens IVC
MyLog(INFO, "A alocar memoria para imagens");
ivcFrame = vc_image_new(video.width, video.height, 3, 255);
labels = vc_image_new(video.width, video.height, 1, 255);
hsvFrame = vc_image_new(video.width, video.height, 3, 255);
binaryFrame = vc_image_new(video.width, video.height, 1, 255);
/* Cria uma janela para exibir o vídeo */
cvNamedWindow("VC - TP2", CV_WINDOW_AUTOSIZE);
/* Inicializa a fonte */
cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, hScale, vScale, 0, lineWidth);
cvInitFont(&fontbkg, CV_FONT_HERSHEY_SIMPLEX, hScale, vScale, 0, lineWidth + 1);
MyLog(INFO, "A analisar video. Pressione P para pausa ou Q para sair.");
while (key != 'q') {
if (key == 'p') {
key = 'a';
while (key != 'p') {
key = cvWaitKey();
}
}
/* Leitura de uma frame do vídeo */
frame = cvQueryFrame(capture);
/* Verifica se conseguiu ler a frame */
if (!frame) break;
/* Número da frame a processar */
video.nframe = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES);
//Converter frame Ipl para IVC
IplImage_to_IVC(frame, ivcFrame);
//Descartar frames caso necessário
if (countJump == frameJump) //Vai analizar o frame
{
countJump = 0;
if (!firstFrame) {
blobsAnterior = vc_copy_blobs(blobsAtual, nlabels);
nlabelsOld = nlabels;
}
else
{
blobsAnterior = NULL;
}
//Passar frame em RGB para HSV
vc_copy_image_data(ivcFrame, hsvFrame);
vc_rgb_to_hsv(hsvFrame);
//Passar frame do video para binario
frame_to_binary(ivcFrame, hsvFrame, binaryFrame);
blobsAtual = vc_binary_blob_labelling(binaryFrame, labels, &nlabels);
vc_binary_blob_info(labels, blobsAtual, nlabels);
if (nlabels > 0) {
analisa_blobs(hsvFrame, binaryFrame, blobsAtual, nlabels, blobsAnterior, nlabelsOld, frutas, &nFrutas);
}
}
else //Não vai analizar, "dropa" o frame
{
countJump++;
}
//Desenhar caixas delimitadoras
if (nlabels > 0) {
drawBoundingBox(ivcFrame, blobsAtual, &nlabels, newColor(0, 255, 255));
drawGravityCentre(ivcFrame, blobsAtual, &nlabels, newColor(0, 255, 255));
}
//Converter de IVC para IPL
IVC_to_IplImage(ivcFrame, frame);
/* Exemplo de inserção texto na frame */
sprintf(str, "RESOLUCAO: %dx%d", video.width, video.height);
cvPutText(frame, str, cvPoint(20, 20), &fontbkg, cvScalar(0, 0, 0));
cvPutText(frame, str, cvPoint(20, 20), &font, cvScalar(255, 255, 255));
sprintf(str, "TOTAL DE FRAMES: %d", video.ntotalframes);
cvPutText(frame, str, cvPoint(20, 40), &fontbkg, cvScalar(0, 0, 0));
cvPutText(frame, str, cvPoint(20, 40), &font, cvScalar(255, 255, 255));
sprintf(str, "FRAME RATE: %d", video.fps);
cvPutText(frame, str, cvPoint(20, 60), &fontbkg, cvScalar(0, 0, 0));
cvPutText(frame, str, cvPoint(20, 60), &font, cvScalar(255, 255, 255));
sprintf(str, "N. FRAME: %d", video.nframe);
cvPutText(frame, str, cvPoint(20, 80), &fontbkg, cvScalar(0, 0, 0));
cvPutText(frame, str, cvPoint(20, 80), &font, cvScalar(255, 255, 255));
sprintf(str, "Qtd. FRUTA: %d", nFrutas);
cvPutText(frame, str, cvPoint(20, 100), &fontbkg, cvScalar(0, 0, 0));
cvPutText(frame, str, cvPoint(20, 100), &font, cvScalar(255, 255, 255));
//esvaziar memoria
//free(blobs);
/* Exibe a frame */
cvShowImage("VC - TP2", frame);
/* Sai da aplicação, se o utilizador premir a tecla 'q' */
firstFrame = false;
key = cvWaitKey(1);
}
imprime_relatorio(frutas, nFrutas);
/* Fecha a janela */
cvDestroyWindow("VC - TP2");
/* Fecha o ficheiro de vídeo */
cvReleaseCapture(&capture);
vc_image_free(ivcFrame);
vc_image_free(labels);
getchar();
return 0;
}