-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStaticBackgroundCompressor.cpp
412 lines (365 loc) · 14.4 KB
/
StaticBackgroundCompressor.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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* File: StaticBackgroundCompressor.cpp
* Author: Marc
*
* Created on October 4, 2010, 1:06 PM
*
* (C) Marc Gershow; licensed under the Creative Commons Attribution Share Alike 3.0 United States License.
* To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to
* Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
*
*/
#include "highgui.h"
#include "StaticBackgroundCompressor.h"
#include <vector>
#include "BackgroundRemovedImage.h"
#include "cv.h"
#include "highgui.h"
#include "BackgroundRemovedImageLoader.h"
#include "StackReader.h"
#include "IplImageLoaderFixedWidth.h"
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
StaticBackgroundCompressor::StaticBackgroundCompressor() {
background = NULL;
bwbuffer = NULL;
buffer1 = buffer2 = NULL;
threshAboveBackground = threshBelowBackground = 0;
smallDimMinSize = lgDimMinSize = 1;
updateBackgroundFrameInterval = -1;
updateCount = 0;
imOrigin.x = imOrigin.y = 0;
}
StaticBackgroundCompressor::StaticBackgroundCompressor(const StaticBackgroundCompressor& orig) {
}
StaticBackgroundCompressor::~StaticBackgroundCompressor() {
cvReleaseImage(&background);
// cout << "size of imsToProcess is " << imsToProcess.size() << "\n";
for (vector<InputImT>::iterator it = imsToProcess.begin(); it != imsToProcess.end(); ++it) {
IplImage *im = it->first;
cvReleaseImage(&im);
if (it->second != NULL) {
delete it->second;
}
}
// cout << "size of bri is " << bri.size() << "\n";
for (vector<BackgroundRemovedImage *>::iterator it = bri.begin(); it != bri.end(); ++it) {
delete (*it);
*it = NULL;
}
}
void StaticBackgroundCompressor::calculateBackground() {
if (imsToProcess.empty()) {
return;
}
if (background != NULL) {
cvReleaseImage(&background);
}
// background = cvCloneImage(imsToProcess.front());
for (vector<InputImT>::iterator it = imsToProcess.begin(); it != imsToProcess.end(); ++it) {
// cvMin(*it, background, background);
updateBackground(it->first);
}
}
void StaticBackgroundCompressor::updateBackground(const IplImage* im) {
if (background == NULL) {
background = cvCloneImage(im);
} else {
cvMin(im, background, background);
}
}
void StaticBackgroundCompressor::addFrame(const IplImage* im, ImageMetaData *metadata) {
IplImage *imcpy = cvCloneImage(im);
addFrame(&imcpy, metadata);
}
void StaticBackgroundCompressor::addFrame(IplImage** im, ImageMetaData* metadata) {
imsToProcess.insert(imsToProcess.begin(), InputImT(*im,metadata));
if (updateBackgroundFrameInterval > 0 && updateCount == 0) {
updateBackground(*im);
updateCount = updateBackgroundFrameInterval;
}
--updateCount;
*im = NULL;
}
int StaticBackgroundCompressor::processFrame() {
if (imsToProcess.empty()) {
return 0;
}
if (background == NULL) {
return -1;
}
InputImT nextim = imsToProcess.back();
// IplImage *im = imsToProcess.back();
imsToProcess.pop_back();
IplImage *im = nextim.first;
ImageMetaData *metadata = nextim.second;
BackgroundRemovedImage *brim = new BackgroundRemovedImage(im, background, threshBelowBackground, threshAboveBackground, smallDimMinSize, lgDimMinSize, metadata);
bri.push_back(brim);
cvReleaseImage(&im);
//NB: we do NOT release metadata storage, as this is now background removed images problem
return imsToProcess.size();
}
void StaticBackgroundCompressor::changeBackground(const IplImage* newBackground) {
IplImage *oldbak;
oldbak = cvCloneImage(background);
cvMin(newBackground, oldbak, background);
BackgroundRemovedImage *bakim = new BackgroundRemovedImage(oldbak, background, threshBelowBackground, threshAboveBackground, smallDimMinSize, lgDimMinSize, NULL);
for (vector<BackgroundRemovedImage *>::iterator it = bri.begin(); it != bri.end(); ++it) {
// cout << "calling merge regions" << endl << flush;
(*it)->mergeRegions(bakim, cvMax);
// cout << "merge regions returned" << endl << flush;
}
cvReleaseImage(&oldbak);
delete bakim;
}
void StaticBackgroundCompressor::mergeStacks(std::vector<StaticBackgroundCompressor*> alreadyCompressedStacks) {
if (alreadyCompressedStacks.empty()) {
return;
}
IplImage *newbak = cvCloneImage(background);
// cout << "calculating background" << endl<< flush;
for (vector<StaticBackgroundCompressor*>::iterator it = alreadyCompressedStacks.begin(); it != alreadyCompressedStacks.end(); ++it) {
cvMin(newbak, (*it)->background, newbak);
}
// cout << "changing backgrounds" << endl << flush;
changeBackground(newbak);
for (vector<StaticBackgroundCompressor*>::iterator it = alreadyCompressedStacks.begin(); it != alreadyCompressedStacks.end(); ++it) {
(*it)->changeBackground(newbak);
bri.insert(bri.end(), (*it)->bri.begin(), (*it)->bri.end());
(*it)->bri.clear();
}
cvReleaseImage(&newbak);
}
void StaticBackgroundCompressor::processFrames() {
while (processFrame() > 0) {
//process Frame does all the work
;
}
}
void StaticBackgroundCompressor::toDisk(std::ofstream& os) {
HeaderInfoT hi;
hi.idcode = idCode();
hi.numframes = bri.size();
hi.headerSize = headerSizeInBytes;
std::ofstream::pos_type start_loc = os.tellp();
char zero[headerSizeInBytes] = {0};
os.write(zero, headerSizeInBytes);
cvResetImageROI(background);
background->roi = NULL;
writeIplImageToByteStream(os, background);
for (vector<BackgroundRemovedImage *>::iterator it = bri.begin(); it != bri.end(); ++it) {
(*it)->toDisk(os);
}
std::ofstream::pos_type end_loc = os.tellp();
hi.totalSize = end_loc - start_loc;
os.seekp(start_loc);
os.write((char *) &hi, sizeof(hi));
os.seekp(end_loc);
}
std::string StaticBackgroundCompressor::saveDescription() {
// cout << "entered sbc save description\n";
std::stringstream os;
os << "Stack of common background images, beginning with this header:\n" << headerDescription();
// cout << "Stack of common background images, beginning with this header:\n" << headerDescription();
os << "Then the background image, as an IplImage, starting with the " << sizeof (IplImage) << " byte image header, followed by the image data\n";
// cout << "Then the background image, as an IplImage, starting with the " << sizeof (IplImage) << " byte image header, followed by the image data\n";
os << "Then nframes background removed images containing only differences from the background, in this format:\n";
// cout << "Then nframes background removed images containing only differences from the background, in this format:\n";
if (bri.empty()) {
os << "<no background removed images in stack>\n";
} else {
// cout << "bri.front = " << (int) bri.front();
if (bri.front() == NULL) {
os << "<background removed image is a NULL pointer>\n";
} else {
os << bri.front()->saveDescription();
}
}
// cout << "ended sbc save description\n";
return os.str();
}
std::string StaticBackgroundCompressor::headerDescription() {
std::stringstream os;
os << headerSizeInBytes << " byte zero-padded header, with the following fields (all " << sizeof(int) << " byte ints, except idcode):\n";
os << sizeof(uint32_t) << " byte uint32_t idcode = " << hex << idCode() << dec << ", header size in bytes, total size of stack on disk, nframes: number of images in stack\n";
return os.str();
}
StaticBackgroundCompressor::HeaderInfoT StaticBackgroundCompressor::getHeaderInfo(std::ifstream& is) {
std::ifstream::pos_type start_loc = is.tellg();
HeaderInfoT hi;
is.read((char *) &hi, sizeof(hi));
is.seekg(start_loc);
return hi;
}
StaticBackgroundCompressor * StaticBackgroundCompressor::fromDisk(std::ifstream& is) {
std::ifstream::pos_type start_loc = is.tellg();
HeaderInfoT hi;
hi = getHeaderInfo(is);
// cout << "header size is " << hi.headerSize << " id code is " << hex << hi.idcode <<dec<< " numframes = " << hi.numframes << std::endl;
is.seekg(start_loc + (std::ifstream::pos_type) hi.headerSize);
StaticBackgroundCompressor *sbc = new StaticBackgroundCompressor();
// cout << "reading background" << endl;
sbc->background = readIplImageFromByteStream(is);
// cout << "background read in" << endl;
for (int j = 0; j < hi.numframes; ++j) {
//BackgroundRemovedImage *bri = BackgroundRemovedImage::fromDisk(is, sbc->background);
BackgroundRemovedImage *bri = BackgroundRemovedImageLoader::fromFile(is, sbc->background);
sbc->bri.push_back(bri);
}
return sbc;
}
//estimate, does not include metadata and maybe some other stuff
int StaticBackgroundCompressor::sizeInMemory() {
int totalBytes = sizeof(this);
if (background != NULL) {
totalBytes += (sizeof(IplImage) + background->imageSize) * (imsToProcess.size()+4);
}
for (vector<BackgroundRemovedImage *>::iterator it = bri.begin(); it != bri.end(); ++it) {
totalBytes += (*it)->sizeInMemory();
}
return totalBytes;
}
int StaticBackgroundCompressor::sizeOnDisk() {
int totalBytes = headerSizeInBytes + ((background != NULL) ? sizeof(IplImage) + background->imageSize : 0);
for (vector<BackgroundRemovedImage *>::iterator it = bri.begin(); it != bri.end(); ++it) {
totalBytes += (*it)->sizeOnDisk();
}
return totalBytes;
}
void StaticBackgroundCompressor::writeIplImageToByteStream(std::ofstream& os, const IplImage *src) {
assert(src != NULL);
ofstream::pos_type cloc = os.tellp();
os.write((char *) src, sizeof(IplImage));
os.write((char *) src->imageData, src->imageSize);
}
IplImage * StaticBackgroundCompressor::readIplImageFromByteStream(std::ifstream& is) {
return IplImageLoaderFixedWidth::loadIplImageFromByteStream(is);
}
void StaticBackgroundCompressor::playMovie(const char* windowName) {
if (windowName == NULL) {
windowName = "Movie of stack";
}
cvNamedWindow(windowName, 0);
IplImage *im = NULL;
for (vector<BackgroundRemovedImage *>::iterator it = bri.begin(); it != bri.end(); ++it) {
(*it)->restoreImage(&im);
cvShowImage(windowName, im);
cvWaitKey(50);
}
}
int StaticBackgroundCompressor::numProcessed() {
return bri.size();
}
int StaticBackgroundCompressor::numToProccess() {
return imsToProcess.size();
}
bool StaticBackgroundCompressor::framesWaitingToProcess() {
return !imsToProcess.empty();
}
void StaticBackgroundCompressor::reconstructFrame(int frameNum, IplImage** dst) {
if (frameNum < 0 || frameNum >= bri.size()) {
if (*dst != NULL) {
cvReleaseImage(dst);
}
*dst = NULL;
return;
}
BackgroundRemovedImage *brim = bri.at(frameNum);
brim->restoreImage(dst);
}
/*
const IplImage *StaticBackgroundCompressor::getBackground() {
return this->background;
}
* */
void StaticBackgroundCompressor::copyBackground(IplImage** dst) {
if (dst == NULL) {
return;
}
if (background == NULL) {
if (*dst != NULL) {
cvReleaseImage(dst);
*dst = NULL;
}
return;
}
setImageOriginFromBRI();
if (*dst == NULL || (*dst)->width != background->width + imOrigin.x || (*dst)->height != background->height + imOrigin.y || (*dst)->depth != background->depth || (*dst)->nChannels != background->nChannels) {
if (*dst != NULL) {
cvReleaseImage(dst);
}
*dst = cvCreateImage(cvSize(background->width + imOrigin.x, background->height+imOrigin.y), background->depth, background->nChannels);
}
cvSetZero(*dst);
CvRect r; r.x = imOrigin.x; r.y = imOrigin.y; r.width = background->width; r.height = background->height;
CvRect roi = cvGetImageROI(*dst);
cvSetImageROI(*dst, r);
cvCopyImage(background, *dst);
cvSetImageROI(*dst, roi);
}
void StaticBackgroundCompressor::annotatedFrame(int frameNum, IplImage** buffer, IplImage** annotatedImage) {
reconstructFrame(frameNum, buffer);
if (*buffer == NULL) {
if (*annotatedImage != NULL) {
cvReleaseImage(annotatedImage);
*annotatedImage = NULL;
return;
}
}
if (*annotatedImage == NULL || (*annotatedImage)->width != (*buffer)->width || (*annotatedImage)->height != (*buffer)->height) {
if (*annotatedImage != NULL) {
cvReleaseImage(annotatedImage);
}
*annotatedImage = cvCreateImage(cvGetSize(*buffer), (*buffer)->depth, 3);
}
cvConvertImage(*buffer, *annotatedImage,0);
BackgroundRemovedImage *brim = bri.at(frameNum);
brim->annotateImage(*annotatedImage);
}
const ImageMetaData *StaticBackgroundCompressor::getMetaData(int frameNumber) {
BackgroundRemovedImage *brim = bri.at(frameNumber);
if (brim == NULL) {
return NULL;
}
return brim->getMetaData();
}
int StaticBackgroundCompressor::numRegionsInFrame(int frameNum) const {
if (frameNum < 0 || frameNum >= bri.size()) {
return -1;
}
const BackgroundRemovedImage *brim = bri.at(frameNum);
return brim->numRegions();
}
void StaticBackgroundCompressor::setImageOriginFromBRI() {
if (bri.empty()) {
return;
}
imOrigin = bri.front()->getImageOrigin();
}
CvSize StaticBackgroundCompressor::getFrameSize() {
setImageOriginFromBRI();
if (background == NULL) {
return cvSize(0,0);
}
CvSize sz = cvGetSize(background);
// cout << endl << "sz.width = " << sz.width << ", sz.height = " << sz.height << endl;
IplImage *im = background;
// cout << "background params: w= " << im->width << ", h= " << im->height << ", nchannels = " << im->nChannels << ", width step = " << im->widthStep << "imageSize = " << im->imageSize << endl;
sz.width += imOrigin.x;
sz.height += imOrigin.y;
//cout << endl << "imorigin.x= " << imOrigin.x << ", imorigin.y= " << imOrigin.y << ", sz.width = " << sz.width << ", sz.height = " << sz.height << endl;
return sz;
}
CvRect StaticBackgroundCompressor::getValidRoi() {
CvRect r;
r.x = r.y = r.width = r.height = 0;
if (background == NULL) {
return r;
}
setImageOriginFromBRI();
CvSize sz = cvGetSize(background);
r.x = imOrigin.x; r.y = imOrigin.y; r.width = sz.width; r.height = sz.height;
return r;
}