-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
210 lines (171 loc) · 6.88 KB
/
main.cpp
File metadata and controls
210 lines (171 loc) · 6.88 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
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
//-----------------------------------------------------------------------------
// Function: WebcamSMGetSM
//-----------------------------------------------------------------------------
void webcamSSMSaliencyCore(videoInput &vi, int dev_id, saliencyMap *SM, // input
IplImage *& inputFrame, IplImage *& SMout) // output
{
// generate saliency map from the designated frame of the input video
bool retinal_mode = true;
CvMat * SMout = SM->SMGetSMFromVideoFrameWebcam(vi, dev_id, inputFrame, retinal_mode);
}
void WebcamSSMSaliency()
{
// create a videoInput object
videoInput vi;
// count the number of devices available
int num_devs = vi.listDevices();
// short cut (currently, we assume 2 devices at most)
int dev0 = 0;
//int dev1 = 1;
// setup the devices
vi.setupDevice(dev0, DEFAULT_CAPTURE_WIDTH, DEFAULT_CAPTURE_HEIGHT);
//vi.setupDevice(dev1, 640, 480);
// Read the video's frame size
CvSize frame_size;
frame_size.width = (int)vi.getWidth (dev0);
frame_size.height = (int)vi.getHeight(dev0);
// Create windows for visualizing the output.
char * window_name_input = "Input Video";
char * window_name_saliency = "Saliency Video";
char * window_name_stochastic = "Eye Focusing Density Video";
cvNamedWindow(window_name_input, CV_WINDOW_AUTOSIZE);
cvNamedWindow(window_name_saliency, CV_WINDOW_AUTOSIZE);
cvNamedWindow(window_name_stochastic, CV_WINDOW_AUTOSIZE);
// deterministic and stochastic saliency maps
saliencyMap * SM = new saliencyMap (frame_size.height, frame_size.width);
// create retinal filter mask
SM->createRetinalFilterMask(frame_size);
// loop
while(1){
// attention estimation
IplImage * inputFrame = NULL;
IplImage * outputSM = NULL;
webcamSSMSaliencyCore(vi, dev0, SM, inputFrame, outputSM);
// show input and output on display
cvShowImage(window_name_input, inputFrame);
cvShowImage(window_name_saliency, outputSM );
// wait for key pressing
int key = cvWaitKey(10); // Please see the key table: http://homepage1.nifty.com/kabayan/java2/data01/apt053.html
if(key==27){ // ESC --> abort
break;
}
else if(key==32){ // SPACE --> reset
// delete
delete SM;
// deterministic and stochastic saliency maps
saliencyMap * SM = new saliencyMap (frame_size.height, frame_size.width);
// create retinal filter mask
SM->createRetinalFilterMask(frame_size);
}
// memory cleanup
cvReleaseImage(&inputFrame);
cvReleaseImage(&outputSM);
}
// release
vi.stopDevice(dev0);
// vi.stopDevice(dev1);
delete SM;
return;
}
void FileSMGetSM(char * input_video_name, char * output_video_name, char * output_data_name)
{
// open the input video
CvCapture* input_video = cvCaptureFromAVI(input_video_name);
if(input_video == NULL) myError("Could not load video file");
// read the video's frame size
CvSize frame_size;
frame_size.height = (int)cvGetCaptureProperty(input_video, CV_CAP_PROP_FRAME_HEIGHT);
frame_size.width = (int)cvGetCaptureProperty(input_video, CV_CAP_PROP_FRAME_WIDTH);
//// determine the number of frames in the AVI.
cvSetCaptureProperty( input_video, CV_CAP_PROP_POS_AVI_RATIO, 1. );
long number_of_frames = (int) cvGetCaptureProperty(input_video, CV_CAP_PROP_POS_FRAMES);
// get FPS of input videop
int fps = (int)cvGetCaptureProperty(input_video, CV_CAP_PROP_FPS);
// return to the beginning
cvSetCaptureProperty(input_video, CV_CAP_PROP_POS_FRAMES, 0);
// initalize video writer setting
CvVideoWriter * output_video_writer = cvCreateVideoWriter(output_video_name, CV_FOURCC('I','4','2','0') /* raw video */, fps, frame_size, 1);
// create windows for visualizing the output.
char * window_name_input = "Input Video";
char * window_name_saliency = "Saliency Video";
cvNamedWindow(window_name_input, CV_WINDOW_AUTOSIZE);
cvNamedWindow(window_name_saliency, CV_WINDOW_AUTOSIZE);
// initalize starting frame no. for extraction
long frameNo = 0;
long start_frameNo = 10;
// saliency maps
saliencyMap * SM = new saliencyMap(frame_size.height, frame_size.width);
// create retinal filter mask
SM->createRetinalFilterMask(frame_size);
long totalTime = 0;
// open the file for log output
ofstream logfile;
logfile.open(output_data_name);
// insert dummy frames
IplImage * frame_for_write = cvCreateImage(frame_size, IPL_DEPTH_8U, 3);
cvSetZero(frame_for_write);
for(frameNo=0; frameNo<start_frameNo; frameNo++){
// write dummy video frames
cvWriteFrame(output_video_writer, frame_for_write);
}
// loop (frames)
for(frameNo=start_frameNo; frameNo<number_of_frames; frameNo++){
// begin time tracking clock
timeBeginPeriod(1);
t1 = timeGetTime();
// generate saliency map from the designated frame of the input video
bool retinal_mode = true;
IplImage *inputFrame = NULL;
CvMat * SMout = SM->SMGetSMFromVideoFrame(input_video, inputFrame, frameNo, retinal_mode);
t2 = timeGetTime();
logfile << "Get a saliency map... " << (t2-t1) << " ms" << endl;
// print the score and time to the console
t2 = timeGetTime();
logfile << "Completed saliecny extraction for frame#" << frameNo;
logfile << "..." << (t2-t1) << "ms" << endl << endl;
totalTime += t2-t1;
timeEndPeriod(1);
// show input and output on display
cvShowImage(window_name_input, inputFrame);
cvShowImage(window_name_saliency, SMout );
cvWaitKey(5);
// write output video frames
IplImage tmp_header;
IplImage *outputSM_8U = cvCreateImage(frame_size, IPL_DEPTH_8U, 1);
cvConvertScale(cvGetImage(SMout, &tmp_header), outputSM_8U, 256);
IplImage * outputSM_8U_flip = cvCreateImage(frame_size, IPL_DEPTH_8U, 1);
cvConvertImage(outputSM_8U, outputSM_8U_flip, CV_CVTIMG_FLIP); // I am not sure why we have to flip it
cvCvtColor(outputSM_8U_flip, frame_for_write, CV_GRAY2BGR);
cvWriteFrame(output_video_writer, frame_for_write);
// memory cleanup
cvReleaseImage(&inputFrame);
cvReleaseMat(&SMout);
cvReleaseImage(&outputSM_8U);
cvReleaseImage(&outputSM_8U_flip);
logfile.flush();
}
logfile << "\n FINAL: " << "\t avg FPS: " << (number_of_frames-START_FRAME_INDENT)*1000/(double)totalTime << endl;
// release input capture and output files
cvReleaseImage(&frame_for_write);
cvReleaseCapture(&input_video);
cvReleaseVideoWriter(&output_video_writer);
delete SM;
return;
}
int main( int argc, char* argv[] ){
// mode selection
char * APPLICATION_MODE = argv[1];
if(strcmp(APPLICATION_MODE, "WEBCAM_MODE")==0){ // webcam input
WebcamSMGetSM();
}
else if(strcmp(APPLICATION_MODE,"FILE_MODE")==0){ // video file input
char * input_video_name = argv[2];
char * output_video_name = argv[3];
char * output_data_name = argv[4];
FileSMGetSM(input_video_name, output_video_name, output_data_name);
}
else {
perror("Mode error.");
}
return 0;
}