-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchFPGA.cpp
More file actions
269 lines (239 loc) · 8.15 KB
/
Copy pathbenchFPGA.cpp
File metadata and controls
269 lines (239 loc) · 8.15 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
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
#include "HLS/hls.h"
#include <cstdio>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
/*int kernelArray[4][3][3] = {{{-1,-1,-1},{2,2,2},{-1,-1,-1}},
{{-1,2,-1},{-1,2,-1},{-1,2,-1}},
{{-1,-1,2},{-1,2,-1},{2,-1,-1}},
{{2,-1,-1},{-1,2,-1},{-1,-1,2}}};*/
double gauss_blur3x3[3][3] = {{1,2,1},
{2,4,2},
{1,2,1}};
double edge[3][3] = {{1,1,1},
{1,-8,1},
{1,1,1}};
double gauss_blur5x5[5][5] = {{1, 4, 6, 4,1},
{4,16,24,16,4},
{6,24,36,24,6},
{4,16,24,16,4},
{1, 4, 6, 4,1}};
double scale = 16.0;
int kern_cols = 3;
int kern_length_offset = 1;
int kern_height_offset = 1;
#define ROWS 20
#define COLS 20
#define CHANNELS 3
#define STEP 60
component void grayscale (ihc::stream_in<uchar> &image, const int rows, const int cols, const int channels, const int step, ihc::stream_out<uchar> &grayImage)
{
uchar tmp;
for (int y = 0; y < ROWS; y++)
{
for (int x = 0; x < COLS; x++)
{
int red = (int) image.read();
int green = (int) image.read();
int blue = (int) image.read();
tmp = (uchar)(.3 * red) + (.59 * green) + (.11 * blue);
#pragma unroll
for (int k = 0; k < CHANNELS; k++)
{
grayImage.write (tmp);
}
}
}
return;
}
component void blur3x3 (uchar* __restrict__ image, int rows, int cols, int channels, int step, ihc::stream_out<uchar> &blurImage)
{
for (int y = 0; y < ROWS; y++)
{
for (int x = 0; x < COLS; x++)
{
double r_sum = 0, b_sum = 0, g_sum = 0;
if (x == 0 || y == 0 || x == COLS-1 || y == ROWS-1)
{
blurImage.write (r_sum);
blurImage.write (g_sum);
blurImage.write (b_sum);
continue;
}
#pragma unroll
for (int i = -1; i < 2; ++i)
{
#pragma unroll
for (int j = -1; j < 2; ++j)
{
double kern_val = gauss_blur3x3[i+1][j+1] / 16.0;
r_sum += kern_val*image[channels*(x+i) + step*(y+j) + 0];/*red*/
g_sum += kern_val*image[channels*(x+i) + step*(y+j) + 1];/*green*/
b_sum += kern_val*image[channels*(x+i) + step*(y+j) + 2];/*blue*/
}
}
blurImage.write (r_sum);
blurImage.write (g_sum);
blurImage.write (b_sum);
}
}
return;
}
component void edge3x3 (uchar* __restrict__ image, int rows, int cols, int channels, int step, ihc::stream_out<uchar> &edgedImage)
{
for (int y = 0; y < ROWS; y++)
{
for (int x = 0; x < COLS; x++)
{
double r_sum = 0, b_sum = 0, g_sum = 0;
if (x == 0 || y == 0 || x == COLS-1 || y == ROWS-1)
{
edgedImage.write (r_sum);
edgedImage.write (g_sum);
edgedImage.write (b_sum);
continue;
}
#pragma unroll
for (int i = -1; i < 2; ++i)
{
#pragma unroll
for (int j = -1; j < 2; ++j)
{
double kern_val = edge[i+1][j+1];
r_sum += kern_val*image[channels*(x+i) + step*(y+j) + 0];/*red*/
if (r_sum < 0) r_sum = 0;
g_sum += kern_val*image[channels*(x+i) + step*(y+j) + 1];/*green*/
if (g_sum < 0) g_sum = 0;
b_sum += kern_val*image[channels*(x+i) + step*(y+j) + 2];/*blue*/
if (b_sum < 0) b_sum = 0;
}
}
edgedImage.write (r_sum);
edgedImage.write (g_sum);
edgedImage.write (b_sum);
}
}
return;
}
/* JUST AN EXAMPLE OF kernel TO BUILD NEW ONE ON ITS BASIS
component void kernel(uchar* __restrict__ inputImage, int im_rows, int im_cols, int channels, int step, double* filter, int ft_rows, int ft_cols, uchar* __restrict__ outputImage) {
int kern_length_offset = ft_rows>>1;
int kern_height_offset = ft_cols>>1;
#pragma ivdep
for(int y = kern_height_offset; y < im_rows-kern_height_offset; y++)
{
#pragma unroll
for (int x = kern_length_offset; x < im_cols-kern_length_offset; x++)
{
double r_sum = 0,b_sum = 0,g_sum = 0;
for(int i=-kern_height_offset;i<=kern_height_offset;++i)
{
for(int j=-kern_length_offset;j<=kern_length_offset;++j)
{
double kern_val = filter[ft_cols*(i+kern_height_offset)+(j+kern_length_offset)];
r_sum += kern_val*inputImage[channels*(x+i) + step*(y+j)];
g_sum += kern_val*inputImage[channels*(x+i) + step*(y+j)+1];
b_sum += kern_val*inputImage[channels*(x+i) + step*(y+j)+2];
}
}
outputImage[channels*x + step*y] = (int) r_sum;
outputImage[channels*x + step*y+1] = (int) g_sum;
outputImage[channels*x + step*y+2] = (int) b_sum;
}
}
return;
}*/
int main (int argc, char** argv )
{
ihc::stream_in<uchar> in_image;
ihc::stream_out<uchar> out_image;
if ( argc < 3 )
{
printf("usage: DisplayImage.out <Image_Path>\n");
return -1;
}
Mat image;
printf ("%s \n", argv[1]);
image = imread (argv[1], 1);
if (image.empty())
{
printf("No image data \n");
return -1;
}
printf ("rows: %d; cols: %d; step: %d; channels: %d\n", image.rows, image.cols, (int)image.step, image.channels());
for (int i = 0; i < image.rows*image.cols*image.channels(); i++)
{
in_image.write (image.data[i]);
}
unsigned char* __restrict__ tempImageData = (uchar*) malloc (3*sizeof(uchar)*image.rows*image.cols);
for (int argi = 2; argi < argc; ++argi)
{
if (strcmp (argv[argi], "-g") == 0)
{
memset (tempImageData, 0, sizeof(uchar)*image.rows*image.cols);
grayscale (in_image, image.rows, image.cols, image.channels(), image.step, out_image);
for(int y = 0; y < image.rows; y++)
{
for (int x = 0; x < image.cols; x++)
{
for (int k = 0; k < image.channels(); k++)
{
tempImageData [x*image.channels()+k+image.cols*y*image.channels()] = out_image.read();
}
}
}
memcpy (image.data, tempImageData, 3*sizeof(uchar)*image.rows*image.cols);
}
else if (strcmp (argv[argi], "-b3") == 0)
{
memset (tempImageData, 25, 3*sizeof(uchar)*image.rows*image.cols);
blur3x3 (image.data, image.rows, image.cols, image.channels(), image.step, out_image);
for (int y = 0; y < image.rows; y++)
{
for (int x = 0; x < image.cols; x++)
{
for (int k = 0; k < image.channels(); k++)
{
tempImageData [x*image.channels()+k+image.cols*y*image.channels()] = out_image.read();
}
}
}
memcpy (image.data, tempImageData, 3*sizeof(uchar)*image.rows*image.cols);
}
else if (strcmp (argv[argi], "-e") == 0)
{
memset (tempImageData, 0, 3*sizeof(uchar)*image.rows*image.cols);
edge3x3 (image.data, image.rows, image.cols, image.channels(), image.step, out_image);
for (int y = 0; y < image.rows; y++)
{
for (int x = 0; x < image.cols; x++)
{
for (int k = 0; k < image.channels(); k++)
{
tempImageData [x*image.channels()+k+image.cols*y*image.channels()] = out_image.read();
}
}
}
memcpy (image.data, tempImageData, 3*sizeof(uchar)*image.rows*image.cols);
}
/*else if (strcmp(argv[argi], "-m") == 0) JUST FOR TESTING kernel COMPONENT
{
int kernel_rows = stoi(argv[++argi]);
int kernel_cols = stoi(argv[++argi]);
double* K = new double[kernel_rows*kernel_cols];
for(int i=0;i<kernel_rows*kernel_cols;++i)
{
K[i] = stod(argv[++argi]);
}
memset(tempImageData, 0, 3*sizeof(uchar)*image.rows*image.cols);
kernel(image.data, image.rows, image.cols, image.channels(), image.step, K, kernel_rows, kernel_cols, tempImageData);
image = Mat(image.rows, image.cols, CV_8UC3, tempImageData);
}*/
else
{
imwrite (strcat (argv[argi], ""), image);
image = imread (argv[1], 1 );
}
}
return 0;
}