-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathuseful_functions.cpp
More file actions
165 lines (133 loc) · 4.43 KB
/
Copy pathuseful_functions.cpp
File metadata and controls
165 lines (133 loc) · 4.43 KB
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
/* useful_functions.cpp
* various functions that are useful to the project but don't belong anywhere in particular
* Brian J Gravelle
* ix.cs.uoregon.edu/~gravelle
* gravelle@cs.uoregon.edu
* Apparently this is something I'm supposed to do:
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
* If by some miricale you find this software useful, thanks are accepted in
* the form of chocolate or introductions to potential employers.
*/
#include "useful_functions.h"
using namespace std;
using namespace cv;
/************************************** utilities **************************************/
int char_to_int(char* c) {
int i = 0;
int ret = 0;
int mult = 1;
while(c[i] !='\0')
i++;
i--;
for (i; i >= 0; i--) {
ret += mult * ((int)c[i] - (int)48);
mult *= 10;
}
return ret;
}
int str_to_int(string s) {
return atoi(s.c_str());
}
string int_to_str(int i){
stringstream ss;
ss << i;
return ss.str();
}
//@concatonates char* to a string
string char_cat(char* first, char* second) {
stringstream ss;
ss << first << second;
return ss.str();
}
//@copies a list of char* to a different list of char*
//@pre out should be empty
//@note you must delete the chars in the new list
void copy_char_list(char** in, int in_count, char** &out) {
int char_count = 0;
out = new char*[in_count];
for (int i = 0; i < in_count; i++) {
char_count = 0;
while(in[i][char_count++] != '\0'); //includes \0 in count
out[i] = new char[char_count];
for (int j = 0; j < char_count; j++) {
out[i][j] = in[i][j];
}
} //outer loop
} // copy_char_count
void delete_char_list(char** &list, int len) {
for (int i = 0; i < len; i++) {
delete list[i];
}
delete[] list;
}
//@displays window or crashes program (not really in use)
void display_window(string name, Mat& img, bool wait /*= false*/) {
if(img.empty()) {
cout << "Could not open or find the image" << std::endl ;
exit(1);
}
imshow(name, img);
resizeWindow(name, 512, 384);
waitKey(1);
if(wait)
waitKey();
}
/******************************* average for background ********************************/
void get_background(string vid_name, Mat& avg_frame) {
Mat next_frame;
VideoCapture capture;
int frame_count = 0;
bool success;
cout << "opening video" << endl;
capture.open(vid_name);
if(!capture.isOpened()){
cout<<"ERROR ACQUIRING VIDEO FEED\n";
getchar();
exit(1);
}
success = capture.read(avg_frame);
avg_frame.convertTo(avg_frame, CV_32F);
if(!success){
cout << endl << "ERROR: next frame failed to be read" << endl;
exit(1);
}
frame_count++;
cvtColor(avg_frame, avg_frame, COLOR_BGR2GRAY);
// display_window("Average", avg_frame);
success = capture.read(next_frame);
next_frame.convertTo(next_frame, CV_32F);
if(!success){
cout << endl << "ERROR: next frame failed to be read" << endl;
exit(1);
}
while( success ) {
// cout << "frame: " << frame_count << endl;
frame_count++;
cvtColor(next_frame, next_frame, COLOR_BGR2GRAY);
//avg_frame = ( ((frame_count-1)*avg_frame) + next_frame) / frame_count;
avg_frame = avg_frame + next_frame;
// display_window("Average", avg_frame);
success = capture.read(next_frame);
next_frame.convertTo(next_frame, CV_32F);
} // inner while loop
avg_frame = avg_frame / frame_count;
cout << "done processing." << endl;
avg_frame.convertTo(avg_frame, CV_8UC1);
//display_window("Average", avg_frame, true);
//release the capture before re-opening and looping again.
capture.release();
}