-
Notifications
You must be signed in to change notification settings - Fork 1
/
boxFilter.cpp
295 lines (228 loc) · 9.68 KB
/
boxFilter.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
// Download a Halide distribution from halide-lang.org and untar it in
// the current directory. Then you should be able to compile this
// file with:
//
//
//Using FITS images on os x:
// g++ Gaussian2D.cpp -g -I ../include -L ../bin -lHalide -lcfitsio `libpng-config --cflags --ldflags` -o Gaussian2D -std=c++11
// DYLD_LIBRARY_PATH=../bin ./Gaussian2D
//
//Using FITS images on linux:
// g++ Gaussian2D.cpp -g -I ../include -L ../bin -lHalide -lpthread -ldl -lcfitsio `libpng-config --cflags --ldflags` -o Gaussian2D -std=c++11
// LD_LIBRARY_PATH=../bin ./Gaussian2D
//
//
// You'll also need a multi-megapixel png image to run this on. Name
// it input.png and put it in this directory.
// Include the Halide language
// On os x:
// g++ boxFilter.cpp -g -I ./include -I DarwinX86/pex_policy/10.1+1/include/ -I DarwinX86/daf_persistence/10.1+1/include/ -I DarwinX86/utils/10.1+1/include/ -I DarwinX86/daf_base/10.1+2/include/ -I DarwinX86/base/10.1+1/include/ -I DarwinX86/ndarray/10.1+2/include/ -I DarwinX86/pex_exceptions/10.1+1/include/ -I DarwinX86/eigen/3.2.0/include/ -I DarwinX86/afw/10.1+1/include -L ./bin -L DarwinX86/afw/10.1+1/lib -L DarwinX86/daf_base/10.1+2/lib/ -L DarwinX86/daf_persistence/10.1+1/lib/ -L DarwinX86/boost/1.55.0.1.lsst2+3/lib/ -lHalide -lafw -ldaf_base -ldaf_persistence -lboost_system `libpng-config --cflags --ldflags` -o boxFilter -std=c++11
//
// DYLD_LIBRARY_PATH=./bin:DarwinX86/afw/10.1+1/lib/:DarwinX86/daf_persistence/10.1+1/lib/:DarwinX86/daf_base/10.1+2/lib/:DarwinX86/boost/1.55.0.1.lsst2+3/lib/:DarwinX86/xpa/2.1.15.lsst2/lib/:DarwinX86/pex_policy/10.1+1/lib/:DarwinX86/pex_logging/10.1+1/lib/:DarwinX86/utils/10.1+1/lib/:DarwinX86/pex_exceptions/10.1+1/lib/:DarwinX86/base/10.1+1/lib/ ./boxFilter
#include "lsst/afw/image.h"
#include <stdio.h>
#include "Halide.h"
#include <bitset>
#include "clock.h"
using namespace std;
using namespace Halide;
using Halide::Image;
namespace afwImage = lsst::afw::image;
namespace afwMath = lsst::afw::math;
int main(int argc, char **argv) {
auto im = afwImage::MaskedImage<float>("../calexp-004207-g3-0123.fits");
printf("Loaded: %d x %d\n", im.getWidth(), im.getHeight());
//store image data in img_var(x, y)
Image<float> image(im.getWidth(), im.getHeight());
Image<float> variance(im.getWidth(), im.getHeight());
Image<uint16_t> mask(im.getWidth(), im.getHeight());
//Read image in
for (int y = 0; y < im.getHeight(); y++) {
afwImage::MaskedImage<float, lsst::afw::image::MaskPixel, lsst::afw::image::VariancePixel>::x_iterator inPtr = im.x_at(0, y);
for (int x = 0; x < im.getWidth(); x++){
image(x, y) = (*inPtr).image();
variance(x, y) = (*inPtr).variance();
mask(x, y) = (*inPtr).mask();
inPtr++;
}
}
int boundingBox = 2;
Var x, y, c, y0, yi, i, j;
//Kernel #1
Func totalKernel;
float sigmaX1 = 200.0f;
float sigmaY1 = 200.0f;
float theta1 = 0.0f; //rotation of sigmaX axis
totalKernel(i, j) = (exp(-((i*cos(theta1) +j*sin(theta1))*(i*cos(theta1) +j*sin(theta1)))
/(2*sigmaX1*sigmaX1)) / (sqrtf(2*M_PI)*sigmaX1))
*(exp(-((j*cos(theta1) - i*sin(theta1))*(j*cos(theta1) - i*sin(theta1)))
/(2*sigmaY1*sigmaY1)) / (sqrtf(2*M_PI)*sigmaY1));
float norm = 0.0f;
float curKernelPos;
for(int i = -boundingBox; i <= boundingBox; i++){
for(int j = -boundingBox; j <= boundingBox; j++){
curKernelPos =
((exp(-((i*cos(theta1) +j*sin(theta1))*(i*cos(theta1) +j*sin(theta1)))
/(2*sigmaX1*sigmaX1)) / (sqrtf(2*M_PI)*sigmaX1))
*(exp(-((j*cos(theta1) - i*sin(theta1))*(j*cos(theta1) - i*sin(theta1)))
/(2*sigmaY1*sigmaY1)) / (sqrtf(2*M_PI)*sigmaY1)))
;
norm += curKernelPos;
//debugging
cout << curKernelPos << "\t";
}
//debugging
cout << endl;
}
cout << "Norm = " << norm << endl;
for(int i = -boundingBox; i <= boundingBox; i++){
for(int j = -boundingBox; j <= boundingBox; j++){
curKernelPos =
((exp(-((i*cos(theta1) +j*sin(theta1))*(i*cos(theta1) +j*sin(theta1)))
/(2*sigmaX1*sigmaX1)) / (sqrtf(2*M_PI)*sigmaX1))
*(exp(-((j*cos(theta1) - i*sin(theta1))*(j*cos(theta1) - i*sin(theta1)))
/(2*sigmaY1*sigmaY1)) / (sqrtf(2*M_PI)*sigmaY1)))
;
//debugging
cout << curKernelPos/norm << "\t";
}
//debugging
cout << endl;
}
Func image_bounded = BoundaryConditions::repeat_edge(image);
Func variance_bounded = BoundaryConditions::repeat_edge(variance);
//compute new Image values
Func blur_image;
Expr blur_image_help = 0.0f;
for(int i = -boundingBox; i <= boundingBox; i++){
for(int j = -boundingBox; j <= boundingBox; j++){
blur_image_help += image_bounded(x + i, y + j) * totalKernel(i, j);
}
}
blur_image_help = blur_image_help/norm;
blur_image(x, y) = blur_image_help;
//compute new Variance values
Func blur_variance;
Expr blur_variance_help = 0.0f;
for(int i = -boundingBox; i <= boundingBox; i++){
for(int j = -boundingBox; j <= boundingBox; j++){
blur_variance_help += variance_bounded(x + i, y + j)*totalKernel(i, j)/norm * totalKernel(i, j)/norm;
// blur_variance_help += variance_bounded(x + i, y + j)/(25*25);
// blur_variance_help += variance_bounded(x + i, y + j) * totalKernel(i, j)/norm;
}
}
blur_variance(x, y) = blur_variance_help;
// Func blur_variance;
// Expr blur_variance_help = 0.0f;
// for(int i = -boundingBox; i <= boundingBox; i++){
// for(int j = -boundingBox; j <= boundingBox; j++){
// blur_variance_help += totalKernel(i, j)/norm *
// (variance_bounded(x + i, y + j) + image_bounded(x + i, y + j)*image_bounded(x + i, y + j));
// }
// }
// blur_variance_help = blur_variance_help - blur_image_help*blur_image_help;
//
// blur_variance(x, y) = blur_variance_help;
//*****************************************************************************
// Func blur_variance;
// Expr curVarianceMean = 0.0f;
// for(int i = -boundingBox; i <= boundingBox; i++){
// for(int j = -boundingBox; j <= boundingBox; j++){
// curVarianceMean += totalKernel(i, j)/norm * variance_bounded(x + i, y + j);
// }
// }
// Expr varianceOfVariance = 0.0f;
// for(int i = -boundingBox; i <= boundingBox; i++){
// for(int j = -boundingBox; j <= boundingBox; j++){
// varianceOfVariance += totalKernel(i, j)/norm * (variance_bounded(x + i, y + j) - curVarianceMean)
// * (variance_bounded(x + i, y + j) - curVarianceMean);
// }
// }
// blur_variance(x, y) = varianceOfVariance;
//end variance
// Split the y coordinate of the consumer into strips of 16 scanlines:
blur_image.split(y, y0, yi, 30);
blur_variance.split(y, y0, yi, 30);
// Compute the strips using a thread pool and a task queue.
blur_image.parallel(y0);
blur_variance.parallel(y0);
// Vectorize across x by a factor of four.
blur_image.vectorize(x, 8);
blur_variance.vectorize(x, 8);
// for(int i = 0; i < boundingBox*boundingBox; i++)
// blur.update(i).vectorize(x, 8);
// Print out pseudocode for the pipeline.
//
// blur.compile_to_lowered_stmt("blur_image.html", {image}, HTML);
// Benchmark the pipeline.
Image<float> image_output(image.width(), image.height());
Image<float> variance_output(image.width(), image.height());
blur_image.realize(image_output);
blur_variance.realize(variance_output);
//Compute mask
Func mask_bounded = BoundaryConditions::repeat_edge(mask);
Func maskOut;
Expr maskOutHelp = 0;
for(int i = -boundingBox; i <= boundingBox; i++)
for(int j = -boundingBox; j <= boundingBox; j++){
//bitwise OR in following line seems to cast result to int32, so mask_output must be typed int32, is this a problem?
maskOutHelp = select(totalKernel(i, j) == 0.0f, maskOutHelp, maskOutHelp | mask_bounded(x + i, y + j));
//maskOutHelp = select(totalKernel(i, j) == 0.0f, maskOutHelp, maskOutHelp + mask_bounded(x + i, y + j));
}
maskOut(x, y) = maskOutHelp;
// Split the y coordinate of the consumer into strips of 16 scanlines:
maskOut.split(y, y0, yi, 30);
// Compute the strips using a thread pool and a task queue.
maskOut.parallel(y0);
// Vectorize across x by a factor of four.
maskOut.vectorize(x, 8);
//
Image<int32_t> mask_output(mask.width(), mask.height());
maskOut.realize(mask_output);
double t1 = current_time();
maskOut.realize(mask_output);
double t2 = current_time();
cout << "mask computation took " << t2-t1 << endl;
// blur_mask.realize(mask_output);
//write image out
auto imOut = afwImage::MaskedImage<float, lsst::afw::image::MaskPixel, lsst::afw::image::VariancePixel>(im.getWidth(), im.getHeight());
for (int y = 0; y < imOut.getHeight(); y++) {
afwImage::MaskedImage<float, lsst::afw::image::MaskPixel, lsst::afw::image::VariancePixel>::x_iterator inPtr = imOut.x_at(0, y);
for (int x = 0; x < imOut.getWidth(); x++){
afwImage::pixel::SinglePixel<float, lsst::afw::image::MaskPixel, lsst::afw::image::VariancePixel>
curPixel(image_output(x, y), mask_output(x, y), variance_output(x, y));
(*inPtr) = curPixel;
inPtr++;
}
}
imOut.writeFits("./halideBox.fits");
/*
double average = 0;
double min;
double max;
int numberOfRuns = 20;
for (int i = 0; i < numberOfRuns; i++) {
double t1 = current_time();
blur.realize(output);
double t2 = current_time();
// std::cout << "Time: " << (t2 - t1) << '\n';
double curTime = (t2-t1);
average += curTime;
if(i == 0){
min = curTime;
max = curTime;
}
else{
if(curTime < min)
min = curTime;
if(curTime > max)
max = curTime;
}
}
average = average/numberOfRuns;
std::cout << "Average Time: " << average << ", Min = " <<
min << ", Max = " << max << ", with " << numberOfRuns <<
" runs" << '\n';
*/
return 0;
}