-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cu
114 lines (97 loc) · 3.94 KB
/
main.cu
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
#include <cstdlib>
#include <cstdio>
#include <cstdio>
#include <chrono>
#include <fstream>
#include <vector>
#include "Image.h"
#include "PPM.h"
#include "MorphableOperator.h"
int main (int argc, char **argv){
std::string path_input = "/home/user/myfolder/";
std::string path_results = path_input;
std::vector<std::string> filename = {"logitech_bill_clinton_bin.ppm", "apple_adam_bin.ppm", "micropro_wordstar_bin.ppm", "two_bytes_better_bin.ppm"};
std::vector<double> times[filename.size() * 6];
Image_t *input_img, *img, *output;
std::string name;
std::chrono::high_resolution_clock::time_point t_start, t_end;
std::chrono::duration<double> time_span;
for(auto file = filename.begin(); file != filename.end(); ++file){
input_img = PPM_import((path_input + *file).c_str());
printf("\nLoaded %s (%dx%d) \n", file->c_str(), input_img->width, input_img->height);
// 0.0 -> BLACK ; 1.0 -> WHITE
StructElem* se = new DiamondShape_SE(3);
// Extract only the first channel.
float *input = input_img->data;
if(input_img->channels > 1) {
input = (float *) malloc(input_img->width * input_img->height * sizeof(float));
for (int r = 0; r < input_img->height; r += 1) {
for (int c = 0; c < input_img->width; c += 1) {
input[r * input_img->width + c] = input_img->data[r * input_img->width * 3 + c * 3];
}
}
}
img = Image_new(input_img->width, input_img->height, 1, input);
// ELABORATION STAGE
// EROSION
printf("Erosion...\t");
output = erosion(img, se, &time_span);
cudaDeviceSynchronize();
times->push_back(time_span.count());
PPM_export((*file +(std::string)"_eroded.ppm").c_str(), output);
Image_delete(output);
// DILATATION
printf("Dilatation...\t");
output = dilatation(img, se, &time_span);
cudaDeviceSynchronize();
times->push_back(time_span.count());
PPM_export((path_results + *file +(std::string)"dilatated.ppm").c_str(), output);
Image_delete(output);
// OPENING
printf("Opening...\t");
output = opening(img, se, &time_span);
cudaDeviceSynchronize();
times->push_back(time_span.count());
PPM_export((path_results + *file +(std::string)"_opened.ppm").c_str(), output);
Image_delete(output);
// CLOSING
printf("Closing...\t");
output = closing(img, se, &time_span);
cudaDeviceSynchronize();
times->push_back(time_span.count());
PPM_export((path_results + *file +(std::string)"_closed.ppm").c_str(), output);
Image_delete(output);
// TOPHAT
printf("TOPHAT...\t");
output = topHat(img, se, &time_span);
cudaDeviceSynchronize();
times->push_back(time_span.count());
PPM_export((path_results + *file +(std::string)"_topHat.ppm").c_str(), output);
Image_delete(output);
// BOTTOMHAT
printf("BOTTOM HAT... \n");
output = bottomHat(img, se, &time_span);
cudaDeviceSynchronize();
times->push_back(time_span.count());
PPM_export((path_results + *file +(std::string)"_bottomHat.ppm").c_str(), output);
Image_delete(output);
free(input);
}
printf("Writing times on file...\n");
std::ofstream timings_file;
std::string fname = "timings_"+ std::to_string(TILE_WIDTH) + ".csv";
timings_file.open((path_results + fname).c_str());
auto it = times->begin();
for(auto file = filename.begin(); file != filename.end(); ++file){
timings_file << file->c_str() << "(TILE_WIDTH="<<TILE_WIDTH<<")"<< "\n";
timings_file << "EROSION;" << *it++ << "\n";
timings_file << "DILATATION;" <<*it++<<"\n";
timings_file << "OPENING;" << *it++ <<"\n";
timings_file << "CLOSING;" << *it++ <<"\n";
timings_file << "TOPHAT;" << *it++ <<"\n";
timings_file << "BOTTOMHAT;" << *it++ <<"\n";
}
timings_file.close();
printf("==== DONE ==== \n");
return 0;
}