Blame view

cpp/main.cpp 5.65 KB
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
1
  #include <iostream>
46820b25   Laila Saadatifard   create an obj fil...
2
  #include <string>
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
3
  #include <fstream>
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
4
5
6
7
  #include <cuda_runtime.h>
  #include <stim/math/vector.h>
  #include <stim/parser/arguments.h>
  #include <stim/parser/filename.h>
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
8
  #include <stim/visualization/colormap.h>
f12505fb   Laila Saadatifard   upload the ivote ...
9
  #include <stim/image/image.h>
2558ee86   Laila Saadatifard   updating ivote3 t...
10
  #include <stim/math/constants.h>
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
11
  
2558ee86   Laila Saadatifard   updating ivote3 t...
12
13
14
15
16
17
18
19
  stim::arglist args;
  int iter;
  unsigned int rmax;
  unsigned int nlmax;
  float t;
  float sigma;
  float phi;
  size_t x, y, z;
1f55a874   Laila Saadatifard   upload the fixed ...
20
  
a744d027   Laila Saadatifard   upload the ivote3...
21
  
310a1698   Laila Saadatifard   update the ivote3...
22
23
24
25
26
27
28
29
30
31
32
  void ivote3(float* img, float std[], float phi, float d_phi, unsigned int r[], int iter, float t, unsigned int conn[],
  	size_t x, size_t y, size_t z);
  void lmax(float* center, float* vote, float t1, unsigned int conn[], size_t x, size_t y, size_t z);
  
  void invert_data(float* cpuI, size_t x, size_t y, size_t z) {
  	size_t idx;
  	for (size_t ix = 0; ix < x; ix++) {
  		for (size_t iy = 0; iy < y; iy++) {
  			for (size_t iz = 0; iz < z; iz++) {
  				idx = iz * x * y + iy * x + ix;
  				cpuI[idx] = 255 - cpuI[idx];
f12505fb   Laila Saadatifard   upload the ivote ...
33
34
35
  			}
  		}
  	}
310a1698   Laila Saadatifard   update the ivote3...
36
37
  }
  
f12505fb   Laila Saadatifard   upload the ivote ...
38
  
94d437dd   Laila Saadatifard   ivote3 code compi...
39
  
2558ee86   Laila Saadatifard   updating ivote3 t...
40
41
42
43
44
45
46
  void advertise() {
  	std::cout << std::endl << std::endl;
  	std::cout << "=========================================================================" << std::endl;
  	std::cout << "Thank you for using the ivote3 segmentation tool!" << std::endl;
  	std::cout << "Scalable Tissue Imaging and Modeling (STIM) Lab, University of Houston" << std::endl;
  	std::cout << "Developers: Laila Saadatifard and David Mayerich" << std::endl;
  	std::cout << "Source: https://git.stim.ee.uh.edu/segmentation/ivote3" << std::endl;
b448d0ad   David Mayerich   fixed AABB error
47
  	std::cout << "This version has been compiled on " << __DATE__ << " at " << __TIME__ << std::endl;
2558ee86   Laila Saadatifard   updating ivote3 t...
48
  	std::cout << "=========================================================================" << std::endl << std::endl;
f12505fb   Laila Saadatifard   upload the ivote ...
49
  
b448d0ad   David Mayerich   fixed AABB error
50
  
2558ee86   Laila Saadatifard   updating ivote3 t...
51
  }
f12505fb   Laila Saadatifard   upload the ivote ...
52
  
2558ee86   Laila Saadatifard   updating ivote3 t...
53
  void init_args(int argc, char* argv[]) {
f12505fb   Laila Saadatifard   upload the ivote ...
54
55
56
57
58
59
60
61
62
  #ifdef _WIN32
  	args.set_ansi(false);
  #endif
  
  	//add arduments
  	args.add("help", "prints this help");
  	args.add("x", "size of the dataset along X axis", "positive value");
  	args.add("y", "size of the dataset along Y axis", "positive value");
  	args.add("z", "size of the dataset along Z axis", "positive value");
310a1698   Laila Saadatifard   update the ivote3...
63
  	args.add("t", "threshold value for the final result", "0", "positive valu");
94d437dd   Laila Saadatifard   ivote3 code compi...
64
  	args.add("invert", "to invert the input data set", "string");
2558ee86   Laila Saadatifard   updating ivote3 t...
65
66
67
  	args.add("rmax", "maximum possible radius of the cells in the input image", "10", "[positive value]");
  	args.add("phi", "starting angle for the vote region (in degrees)", "25.0", "0 <= phi < 180");
  	args.add("iter", "number of iterations for voting", "8", "i > 0");
310a1698   Laila Saadatifard   update the ivote3...
68
  	args.add("sigma", "the gaussian blur standard deviation", "3", "s >=0 (s = 0, no blurring)");
2558ee86   Laila Saadatifard   updating ivote3 t...
69
  	args.add("conn", "the number of connected neighbors for calculating the local maxima", "5", "[positive value]");
f12505fb   Laila Saadatifard   upload the ivote ...
70
71
72
73
  	//parse the command line arguments.
  	args.parse(argc, argv);
  
  	//display the help text if requested
2558ee86   Laila Saadatifard   updating ivote3 t...
74
75
76
77
78
79
80
81
  	if (args["help"].is_set()) {
  		advertise();
  		std::cout << std::endl << "usage: ivote input_image output_list --option [A B C ...]" << std::endl;
  		std::cout << std::endl << std::endl
  			<< "examples: ivote blue.bmp list.txt " << std::endl;
  
  		std::cout << std::endl << std::endl;
  		std::cout << args.str() << std::endl;
f12505fb   Laila Saadatifard   upload the ivote ...
82
83
84
85
  		exit(1);
  	}
  
  	//if the input and output files aren't specified, throw an error and exit
2558ee86   Laila Saadatifard   updating ivote3 t...
86
87
  	if (args.nargs() < 3) {
  		std::cout << "ERROR: three files must be specified for segmentation, enter ivote --help for options." << std::endl << std::endl;
f12505fb   Laila Saadatifard   upload the ivote ...
88
89
90
  		exit(1);
  	}
  
2558ee86   Laila Saadatifard   updating ivote3 t...
91
92
93
94
95
96
97
  	//set the x, y, z.
  	x = (size_t)args["x"].as_int();
  	y = (size_t)args["y"].as_int();
  	z = (size_t)args["z"].as_int();
  	iter = args["iter"].as_int();
  	rmax = (unsigned int)args["rmax"].as_int();
  	nlmax = (unsigned int)args["conn"].as_int();
07e31b34   Laila Saadatifard   fix the bugs in i...
98
99
  	t = (float)args["t"].as_float();
  	sigma = (float)args["sigma"].as_float();
2558ee86   Laila Saadatifard   updating ivote3 t...
100
  	phi = (float)args["phi"].as_float() * (float)stim::PI / 180;
310a1698   Laila Saadatifard   update the ivote3...
101
  
2558ee86   Laila Saadatifard   updating ivote3 t...
102
  }
310a1698   Laila Saadatifard   update the ivote3...
103
  int main(int argc, char** argv) {
f12505fb   Laila Saadatifard   upload the ivote ...
104
  
f12505fb   Laila Saadatifard   upload the ivote ...
105
  
2558ee86   Laila Saadatifard   updating ivote3 t...
106
107
108
  	cudaDeviceProp prop;
  	int count;
  	cudaGetDeviceCount(&count);
310a1698   Laila Saadatifard   update the ivote3...
109
  	for (int i = 0; i<count; i++) {
2558ee86   Laila Saadatifard   updating ivote3 t...
110
111
112
  		cudaGetDeviceProperties(&prop, i);
  		printf("current device ID: %d\n", i);
  		printf("device name: %s\n", prop.name);
07e31b34   Laila Saadatifard   fix the bugs in i...
113
114
  		printf("total global mem: %zu\n", prop.totalGlobalMem);
  		printf("shared memory per block: %zu\n", prop.sharedMemPerBlock);
2558ee86   Laila Saadatifard   updating ivote3 t...
115
  	}
310a1698   Laila Saadatifard   update the ivote3...
116
  
2558ee86   Laila Saadatifard   updating ivote3 t...
117
  	init_args(argc, argv);
f12505fb   Laila Saadatifard   upload the ivote ...
118
  
310a1698   Laila Saadatifard   update the ivote3...
119
120
121
122
123
124
125
  	unsigned int r[3] = { rmax , rmax, rmax };
  
  	float sigma3[3] = { sigma, sigma, sigma };
  	unsigned int conn[3] = { nlmax, nlmax, nlmax };
  	float d_phi = phi / (iter);
  
  	size_t bytes = x*y*z * sizeof(float);
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
126
  
f12505fb   Laila Saadatifard   upload the ivote ...
127
  	//allocate space on the cpu for the input data
310a1698   Laila Saadatifard   update the ivote3...
128
  	float* cpuI = (float*)malloc(bytes);
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
129
  
f12505fb   Laila Saadatifard   upload the ivote ...
130
  	//load the input file into the cpuI
2558ee86   Laila Saadatifard   updating ivote3 t...
131
  	std::ifstream nissl(args.arg(0), std::ios::in | std::ios::binary);
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
132
133
  	nissl.read((char*)cpuI, bytes);
  	nissl.close();
310a1698   Laila Saadatifard   update the ivote3...
134
  	if (args["invert"].is_set())
94d437dd   Laila Saadatifard   ivote3 code compi...
135
  		invert_data(cpuI, x, y, z);
310a1698   Laila Saadatifard   update the ivote3...
136
  
f12505fb   Laila Saadatifard   upload the ivote ...
137
  	//write a new file from the cpuI.
2558ee86   Laila Saadatifard   updating ivote3 t...
138
  	std::ofstream original("0-inv-128.vol", std::ofstream::out | std::ofstream::binary);
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
139
140
  	original.write((char*)cpuI, bytes);
  	original.close();
310a1698   Laila Saadatifard   update the ivote3...
141
142
  
  
2558ee86   Laila Saadatifard   updating ivote3 t...
143
  	ivote3(cpuI, sigma3, phi, d_phi, r, iter, t, conn, x, y, z);			// call the ivote function
07e31b34   Laila Saadatifard   fix the bugs in i...
144
  	/*
310a1698   Laila Saadatifard   update the ivote3...
145
  	std::ofstream fvote("00-vote8_aabb.vol", std::ofstream::out | std::ofstream::binary);
2558ee86   Laila Saadatifard   updating ivote3 t...
146
147
  	fvote.write((char*)cpuI, bytes);
  	fvote.close();
07e31b34   Laila Saadatifard   fix the bugs in i...
148
  	*/
310a1698   Laila Saadatifard   update the ivote3...
149
  
2558ee86   Laila Saadatifard   updating ivote3 t...
150
151
152
  	//allocate space on the cpu for the output result
  	float* cpu_out = (float*)malloc(bytes * 3);
  
07e31b34   Laila Saadatifard   fix the bugs in i...
153
  	
310a1698   Laila Saadatifard   update the ivote3...
154
  	lmax(cpu_out, cpuI, t, conn, x, y, z);
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
155
  	
310a1698   Laila Saadatifard   update the ivote3...
156
157
158
159
  	std::ofstream fo(args.arg(1), std::ofstream::out | std::ofstream::binary);
  	fo.write((char*)cpu_out, bytes);
  	fo.close();
  
94d437dd   Laila Saadatifard   ivote3 code compi...
160
  	// creat a file for saving the list centers
310a1698   Laila Saadatifard   update the ivote3...
161
162
163
164
165
166
167
168
169
170
  
  	std::ofstream list(args.arg(2));
  	// set the number of detected cells to zero.
  	int nod = 0;
  	if (list.is_open()) {
  
  		for (int iz = 0; iz<z; iz++) {
  			for (int iy = 0; iy<y; iy++) {
  				for (int ix = 0; ix<x; ix++) {
  
07e31b34   Laila Saadatifard   fix the bugs in i...
171
  					size_t idx = iz * x * y + iy * x + ix;
310a1698   Laila Saadatifard   update the ivote3...
172
173
174
175
  					if (cpu_out[idx]>0) {
  						nod++;
  						list << ix << " " << iy << " " << iz << " " << cpu_out[idx] << '\n';
  
3f0e43ce   Laila Saadatifard   compute the local...
176
177
  					}
  				}
310a1698   Laila Saadatifard   update the ivote3...
178
  			}
3f0e43ce   Laila Saadatifard   compute the local...
179
180
  		}
  
310a1698   Laila Saadatifard   update the ivote3...
181
182
183
  		list.close();
  	}
  
310a1698   Laila Saadatifard   update the ivote3...
184
  	cudaDeviceReset();
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
185
  
310a1698   Laila Saadatifard   update the ivote3...
186
  }