This repository has been archived by the owner on Aug 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhistogram_based_recognition_colour.cc
374 lines (269 loc) · 11.2 KB
/
histogram_based_recognition_colour.cc
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Example : basic histogram based recognition from video / camera
// using all three R, G, B colour channels
// usage: prog {<video_name>}
// Author : Toby Breckon, toby.breckon@cranfield.ac.uk
// Copyright (c) 2009 School of Engineering, Cranfield University
// License : LGPL - http://www.gnu.org/licenses/lgpl.html
#include "cv.h" // open cv general include file
#include "highgui.h" // open cv GUI include file
#include <stdio.h> // standard C/C++ includes
#include <algorithm> // contains max() function (amongst others)
using namespace cv; // use c++ namespace so the timing stuff works consistently
/******************************************************************************/
// setup the cameras properly based on OS platform
// 0 in linux gives first camera for v4l
//-1 in windows gives first device or user dialog selection
#ifdef linux
#define CAMERA_INDEX 0
#else
#define CAMERA_INDEX -1
#endif
/******************************************************************************/
void printhelp(){
printf("\nControls: \n");
printf("\tspace = capture a sample image\n");
printf("\treturn = move to recognition mode (or m)\n");
printf("\tr = recognise current image\n");
printf("\tany key = clear recognition result\n");
printf("\tx = exit\n");
}
/******************************************************************************/
int main( int argc, char** argv )
{
IplImage* img = NULL; // image object
CvCapture* capture = NULL; // capture object
char const * windowName = "Histogram Based Recognition"; // window name
bool keepProcessing = true; // loop control flag
char key; // user input
int EVENT_LOOP_DELAY = 40; // delay for GUI window
// 40 ms equates to 1000ms/25fps = 40ms per frame
// histogram specific stuff
#define MAX_NUMBER_OF_SAMPLE_IMAGES 255
int hist_size = 256; // size of histogram (number of bins)
float range_0[]={0, float(hist_size)};
float* ranges[] = { range_0 };
CvHistogram* currentHistogramR =
cvCreateHist(1, &hist_size, CV_HIST_ARRAY, ranges, 1);
CvHistogram* currentHistogramG =
cvCreateHist(1, &hist_size, CV_HIST_ARRAY, ranges, 1);
CvHistogram* currentHistogramB =
cvCreateHist(1, &hist_size, CV_HIST_ARRAY, ranges, 1);
// data structures and matrices for histogram based recognition
IplImage* input[MAX_NUMBER_OF_SAMPLE_IMAGES];
CvHistogram* histogramR[MAX_NUMBER_OF_SAMPLE_IMAGES];
CvHistogram* histogramG[MAX_NUMBER_OF_SAMPLE_IMAGES];
CvHistogram* histogramB[MAX_NUMBER_OF_SAMPLE_IMAGES];
int imagesCollected = 0; // number of sample images collected
bool recognitionStage = false; // flag to determine when have started
// recognition
// if command line arguments are provided try to read image/video_name
// otherwise default to capture from attached H/W camera
if(
( argc == 2 && (capture = cvCreateFileCapture( argv[1] )) != 0 ) ||
( argc != 2 && (capture = cvCreateCameraCapture( CAMERA_INDEX )) != 0 )
)
{
// print user controls
printhelp();
// create window object (use flag=0 to allow resize, 1 to auto fix size)
cvNamedWindow(windowName, 0);
// set up font structure
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_PLAIN, 1, 1 , 0, 2, 8 );
// define required images for intermediate processing
// (if using a capture object we need to get a frame first to get the size)
if (capture) {
// cvQueryFrame s just a combination of cvGrabFrame
// and cvRetrieveFrame in one call.
img = cvQueryFrame(capture);
if(!img){
if (argc == 2){
printf("End of video file reached\n");
} else {
printf("ERROR: cannot get next fram from camera\n");
}
exit(0);
}
}
if (img->nChannels != 3)
{
printf("ERROR: input is not a colour image\n");
exit(1);
}
IplImage* channelR =
cvCreateImage(cvSize(img->width,img->height), img->depth, 1);
IplImage* channelG =
cvCreateImage(cvSize(img->width,img->height), img->depth, 1);
IplImage* channelB =
cvCreateImage(cvSize(img->width,img->height), img->depth, 1);
// start main loop
while (keepProcessing) {
int64 timeStart = getTickCount(); // get time at start of loop
// if capture object in use (i.e. video/camera)
// get image from capture object
if (capture) {
// cvQueryFrame is just a combination of cvGrabFrame
// and cvRetrieveFrame in one call.
img = cvQueryFrame(capture);
if(!img){
if (argc == 2){
printf("End of video file reached\n");
} else {
printf("ERROR: cannot get next fram from camera\n");
}
exit(0);
}
}
// display image in window (with text)
if (!recognitionStage){
cvPutText(img, "SAMPLE COLLECTION",
cvPoint(10,img->height - 20), &font, CV_RGB(0, 255,0));
} else {
cvPutText(img, "RECOGNITION",
cvPoint(10,img->height - 20), &font, CV_RGB(255, 0 ,0));
}
cvShowImage( windowName, img );
// start event processing loop (very important,in fact essential for GUI)
// 40 ms roughly equates to 1000ms/25fps = 4ms per frame
// here we take account of processing time for the loop by subtracting the time
// taken in ms. from this (1000ms/25fps = 40ms per frame) value whilst ensuring
// we get a +ve wait time
key = cvWaitKey((int) std::max(2.0, EVENT_LOOP_DELAY -
(((getTickCount() - timeStart) / getTickFrequency()) * 1000)));
if (key == 'x'){
// if user presses "x" then exit
printf("Keyboard exit requested : exiting now - bye!\n");
keepProcessing = false;
} else if (key == ' '){
// if user presses " " then capture a sample image
if (!recognitionStage) {
if (imagesCollected < MAX_NUMBER_OF_SAMPLE_IMAGES)
{
// split image into R, G and B channels and
// store histograms (rememeber - BGR!)
cvSplit(img, channelB, channelG, channelR, NULL);
// copy image + build/store image histograms
input[imagesCollected] = cvCloneImage(img);
histogramR[imagesCollected] =
cvCreateHist(1, &hist_size, CV_HIST_ARRAY, ranges, 1);
cvCalcHist( &channelR, histogramR[imagesCollected], 0, NULL );
cvNormalizeHist(histogramR[imagesCollected], 1);
histogramG[imagesCollected] =
cvCreateHist(1, &hist_size, CV_HIST_ARRAY, ranges, 1);
cvCalcHist( &channelG, histogramG[imagesCollected], 0, NULL );
cvNormalizeHist(histogramG[imagesCollected], 1);
histogramB[imagesCollected] =
cvCreateHist(1, &hist_size, CV_HIST_ARRAY, ranges, 1);
cvCalcHist( &channelB, histogramB[imagesCollected], 0, NULL );
cvNormalizeHist(histogramB[imagesCollected], 1);
imagesCollected++;
printf("Sample image collected - %i\n", imagesCollected);
} else {
printf("ERROR: Maximum sample images (%d) collected.\n",
imagesCollected);
}
}
} else if ((key == '\n') || (key == 'm')) { // use "m" in windows
// if user presses return then move into recognition mode
printf("Entering recognition mode - histogram models stored\n\n");
recognitionStage = true;
if (!(imagesCollected > 0)) {
printf("ERROR: not enough samples images caputured\n");
}
} else if (key == 'r'){
// if user presses "r" then do recognition
// get the colour channels (rememeber - BGR!)
cvSplit(img, channelB, channelG, channelR, NULL);
// calc current image channel histograms
cvCalcHist( &channelR, currentHistogramR, 0, NULL );
cvNormalizeHist( currentHistogramR, 1);
cvCalcHist( &channelG, currentHistogramG, 0, NULL );
cvNormalizeHist( currentHistogramG, 1);
cvCalcHist( &channelB, currentHistogramB, 0, NULL );
cvNormalizeHist( currentHistogramB, 1);
if (recognitionStage) {
// for each histogram do comparison
double closestDistance = HUGE;
int closestImage = 0;
for (int i = 0; i < imagesCollected; i++)
{
// do histogram comparision here
// (here in a very quick and dirty approach we
// just add the measures for all three channels
// and divide by 3 - i.e. averaging them)
double correlation = ((
cvCompareHist(currentHistogramR, histogramR[i],CV_COMP_CORREL) +
cvCompareHist(currentHistogramG, histogramG[i],CV_COMP_CORREL) +
cvCompareHist(currentHistogramB, histogramB[i],CV_COMP_CORREL)) / 3.0);
double chisquared = ((
cvCompareHist(currentHistogramR, histogramR[i],CV_COMP_CHISQR) +
cvCompareHist(currentHistogramG, histogramG[i],CV_COMP_CHISQR) +
cvCompareHist(currentHistogramB, histogramB[i],CV_COMP_CHISQR)) / 3.0);
double intersect = ((
cvCompareHist(currentHistogramR, histogramR[i],CV_COMP_INTERSECT) +
cvCompareHist(currentHistogramG, histogramG[i],CV_COMP_INTERSECT) +
cvCompareHist(currentHistogramB, histogramB[i],CV_COMP_INTERSECT)) / 3.0);
double bhattacharyya = ((
cvCompareHist(currentHistogramR, histogramR[i],CV_COMP_BHATTACHARYYA) +
cvCompareHist(currentHistogramG, histogramG[i],CV_COMP_BHATTACHARYYA) +
cvCompareHist(currentHistogramB, histogramB[i],CV_COMP_BHATTACHARYYA)) / 3.0);
// here we just sum the differences of the measures
// (which as the histograms are all normalised are all
// measures in the range -1->0->1)
// N.B. For the OpenCV implementation:
// low correlation = large difference (so we invert it)
// low intersection = large difference (so we invert it)
// high chisquared = large differences
// high bhatt. = large difference
// - and vice versa
double diff = (1 - correlation) + chisquared
+ (1 - intersect) + bhattacharyya;
printf("Comparison image %i Corr: %.3f ChiSq: %.3f",
i, correlation, chisquared);
printf(" Intersect: %.3f Bhatt: %.3f Total Distance = %.3f\n",
intersect, bhattacharyya, diff);
if (diff < closestDistance){
closestDistance = diff;
closestImage = i;
}
}
printf("\n");
// output the result in a window
printf("Recognition - closest matching image = %d\n", closestImage);
printf("Press any key to clear. \n\n");
cvNamedWindow("Recognition Result", 1 );
cvShowImage("Recognition Result", input[closestImage]);
cvWaitKey(0);
cvDestroyWindow("Recognition Result");
} else {
printf("ERROR - need to enter recognition stage first.\n");
}
}
}
// destroy window objects
// (triggered by event loop *only* window is closed)
cvDestroyAllWindows();
// destroy image object (if it does not originate from a capture object)
if (!capture){
cvReleaseImage( &img );
}
cvReleaseImage(&channelB);
cvReleaseImage(&channelG);
cvReleaseImage(&channelR);
for (int i = 0; i < imagesCollected; i++)
{
cvReleaseImage( &(input[i]));
cvReleaseHist( &(histogramR[i]));
cvReleaseHist( &(histogramG[i]));
cvReleaseHist( &(histogramB[i]));
}
cvReleaseHist( ¤tHistogramR );
cvReleaseHist( ¤tHistogramG );
cvReleaseHist( ¤tHistogramB );
// all OK : main returns 0
return 0;
}
// not OK : main returns -1
return -1;
}
/******************************************************************************/