-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHost.cpp
More file actions
executable file
·168 lines (130 loc) · 6.58 KB
/
Host.cpp
File metadata and controls
executable file
·168 lines (130 loc) · 6.58 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
#define _CRT_SECURE_NO_WARNINGS
#define PROGRAM_FILE "../Kernels/Kernels.cl"
#define KERNEL_FUNC "convol_ker"
#define AEDAT_FOLDER "../TestFiles/"
#define FRAMES_L "Frames_L_Moving_Bar-2018_03_06_17_04_05.aedat"
#define EVENTS_L "Events_L_Moving_Bar-2018_03_06_17_04_05.aedat"
#define FRAMES_R "Frames_R_Moving_Bar-2018_03_06_17_04_05.aedat"
#define EVENTS_R "Events_R_Moving_Bar-2018_03_06_17_04_05.aedat"
#define VERBOSE 1 //set to 0 di disable, 1 (or any other integer) to enable
#define XDIM 240 //Davis horizontal resolution
#define YDIM 180 //Davis vertical resolution
#define LGN_RF_SIZE 3
#define FRAMEPOS 50
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <iostream>
#include <fstream>
#include <string>
#include <Davisloading.hpp>
#include <opencv2/opencv.hpp>
#include <Dataplotting.hpp>
#include <Filters.hpp>
#include <Ocl.hpp>
#ifdef MAC
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif
int main() {
/* OpenCL data structures */
cl_device_id device;
cl_context context;
cl_command_queue queue;
cl_program program;
cl_kernel kernel;
cl_int err;
/* Data and buffers */
int framepos;
unsigned int numPhases = 8;
unsigned int numOrientations = 8;
unsigned int t_halfspan = 1000;
/* Array containing all filters for edge detection and phase coding */
//TODO use the same equation and parameters in the Parvo Path too
int gaborSize = 3;
double sigma = 3;
double freq = 4;
double phase, theta;
/* It cycles through all orientations and then it changes phase */
cv::Mat_<float> Filters[numPhases*numOrientations];
for(unsigned int i=0; i<numPhases; i++){
for(unsigned int j=0; j<numOrientations; j++){
phase = (double)-90 + 180/numPhases * i;
theta = (double)180/numPhases * j;
Filters[j+i*numPhases] = gaborFilter(gaborSize, sigma, theta, freq, phase);
}
}
/* How to check if the implemented filters are what we want */
printOnOffImages("First Filter", Filters[0], -Filters[0], 50);
printOnOffImages("Fourth Filter", Filters[4], -Filters[4], 50);
printOnOffImages("Seventh Filter", Filters[7], -Filters[7], 50);
/* Buffers carrying result and input image for the convolutions */
cl_mem conv_buffers[3];
/* Array to temporary store the results */
float result_on[XDIM*YDIM], result_off[XDIM*YDIM];
/* Array of Mat holding the oriented bordered of the image */
cv::Mat_<float> BoundaryResults_on[numOrientations];
cv::Mat_<float> BoundaryResults_off[numOrientations];
std::cout<<std::endl<<"* Loading Davis Data *"<<std::endl<<std::endl;
/* Loading Davis Data */
DAVISFrames frames_l(std::string(AEDAT_FOLDER) + std::string(FRAMES_L), XDIM, YDIM, VERBOSE);
std::cout<< frames_l.frames.size()<<" extracted Frames from the left camera."<<std::endl;
DAVISEvents events_l(std::string(AEDAT_FOLDER) + std::string(EVENTS_L), VERBOSE);
std::cout<< events_l.polarity.size()<<" extracted Events from the left camera."<<std::endl;
DAVISFrames frames_r(std::string(AEDAT_FOLDER) + std::string(FRAMES_R), XDIM, YDIM, VERBOSE);
std::cout<< frames_r.frames.size()<<" extracted Frames from the left camera."<<std::endl;
DAVISEvents events_r(std::string(AEDAT_FOLDER) + std::string(EVENTS_R), VERBOSE);
std::cout<< events_r.polarity.size()<<" extracted Events from the left camera."<<std::endl;
/* Davis cameras at the time of writing cannot sync frame capure and exposure time
for this reason i need to sync the recordings selecting only the more synchronised couple of left and right frames. */
sync_frames(frames_l, frames_r);
std::cout<<"Left frames number after sync: "<<frames_l.frames.size()<<" Right frames number after sync: "<<frames_r.frames.size()<<std::endl;
framepos = FRAMEPOS;
//std::cout<<"Waiting your input for the frame position: ";
//std::cin>>framepos;
printDavisStereo(framepos, frames_l, frames_r, events_l, events_r, t_halfspan, XDIM, YDIM, VERBOSE);
/* */
/* Simple BCS (Boundary Countour System) computation */
/* */
std::cout<<std::endl<<"* Setting up OpenCl and starting BCS *"<<std::endl<<std::endl;
convolution(device, context, queue, program, kernel, Filters[0], frames_r.frames[framepos],
BoundaryResults_on[0], BoundaryResults_off[0], CL_TRUE, CL_FALSE, conv_buffers);
auto Boundaries = BoundaryResults_on[0] + BoundaryResults_off[0];
/* This overrided convolution is used to avoid to build again the buffers */
for(unsigned int i = 1; i<numOrientations-1; i++){
convolution(device, context, queue, program, kernel, Filters[i],
BoundaryResults_on[i], BoundaryResults_off[i], CL_FALSE, conv_buffers);
Boundaries = Boundaries + BoundaryResults_on[i] + BoundaryResults_off[i];
}
/* The last concolution have the parameter to deallocate memory set to CL_TRUE */
convolution(device, context, queue, program, kernel, Filters[numOrientations-1],
BoundaryResults_on[numOrientations-1], BoundaryResults_off[numOrientations-1], CL_TRUE, conv_buffers);
Boundaries = Boundaries + BoundaryResults_on[numOrientations-1] + BoundaryResults_off[numOrientations-1];
/* Hard tresholding the result */
// TODO find a more elegant solution
Boundaries = Boundaries - 0.3;
std::cout<<std::endl<<"* BCS computation ended *"<<std::endl<<std::endl;
/* Print Results */
printImage("Result ON of frames : " + std::to_string(framepos), BoundaryResults_on[0], 1);
printImage("Result OFF of frames : " + std::to_string(framepos), BoundaryResults_off[0], 1);
printImage("Result of BCS", Boundaries/5, 1);
for(unsigned int i = 0; i<numOrientations; i++){
printOnOffImages("Mixed result" + std::to_string(i+1), BoundaryResults_on[i], BoundaryResults_off[i], 1);
}
/* Deallocate resources */
clReleaseKernel(kernel);
clReleaseCommandQueue(queue);
clReleaseProgram(program);
clReleaseContext(context);
/* Check pixels
std::cout<<std::endl<<tmp_off.at<float>(118,7)<<" "<<tmp_off.at<float>(118,8)<<" "<<tmp_off.at<float>(118,9)<<" "<<std::endl;
std::cout<<std::endl<<tmp_off.at<float>(119,7)<<" "<<tmp_off.at<float>(119,8)<<" "<<tmp_off.at<float>(119,9)<<" "<<std::endl;
std::cout<<std::endl<<tmp_off.at<float>(120,7)<<" "<<tmp_off.at<float>(120,8)<<" "<<tmp_off.at<float>(120,9)<<" "<<std::endl;
*/
/* Save Results
save_results("../Result_r_OFF.png", tmp_off);
*/
cv::waitKey(0);
return 0;
}