Blame view

cpp/main.cpp 8 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>
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
10
11
  #define pi	3.14159
  
a744d027   Laila Saadatifard   upload the ivote3...
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
  #define M_PI	3.14159
  #include <stim/math/circle.h>
  #include <stim/math/vec3.h>
  #include <stim/math/plane.h>
  #include <stim/math/vector.h>
  //#include <cuda.h>
  //#include <stim/cuda/cudatools.h>
  //#include <stim/cuda/cudatools/error.h>
  
  
  /*void test_3(float* gpu_out, float* gpu_grad, float rmax, float phi, int n, int x, int y, int z);
  
  int main(){
  	
  	int  n=20;
  	float rmax;
  	float phi_deg;
  	float phi;
  	rmax=4;
  	phi_deg = 15;
  	phi = phi_deg * pi/180;
  	int x,y,z;
  	x=y=z=1;
  	unsigned int size = x*y*z*sizeof (float);
  	float* cpu_grad = (float*) malloc(3*size);
  	float* gpu_grad;
  	cudaMalloc(&gpu_grad, 3*size);
  	cpu_grad[0]=1;
  	cpu_grad[1]=0;
  	cpu_grad[2]=-0.5;
  	cudaMemcpy(gpu_grad, cpu_grad, 3*size, cudaMemcpyHostToDevice);
  	float* cpu_out = (float*) malloc(3*size*(n+1));
  	float* gpu_out;
  	cudaMalloc(&gpu_out, 3*size*(n+1));
  	test_3(gpu_out, gpu_grad, rmax, phi, n, x, y, z);
  	cudaMemcpy(cpu_out, gpu_out, 3*size*(n+1), cudaMemcpyDeviceToHost);
  	std::ofstream list("circle_check_cuda1.txt");
  	if (list.is_open()){
  		for (int j=0; j<=n; ++j)
  			list << cpu_out[3*j] << '\t' << cpu_out[3*j +1] << '\t' << cpu_out[3*j + 2] << '\n';
  	}
  	list.close();
  	*/
  	/*
  	int  n=10;
  	stim::circle<float>  cir;
  	float* c0= (float*) malloc(3*sizeof(float));
  	c0[0] =-4;
  	c0[1]=0;
  	c0[2] = 3;
  	stim::vec3<float> c(c0[0],c0[1],c0[2]);
  	float len = c.len();
  	stim::vec3<float> norm(c0[0]/len,c0[1]/len,c0[2]/len);
  	std::cout<< len << '\n';
  	std::cout<< norm << '\n';
  	cir.center(c);
  	cir.normal(norm);
  	cir.scale(2);
  	stim::vec3<float> out = cir.p(45);
  	std::vector<stim::vec3<float>> out2 = cir.getPoints(n);
  	
  	std::cout<< out << '\n';
  	std::cout <<out[0] << '\t' << out[1] << '\t' << out[2] <<'\n';
  	std::cout<< c << '\n';
  	
  	for (std::vector<stim::vec3<float>>::const_iterator i = out2.begin(); i != out2.end(); ++i)
      std::cout << *i << '\n';
  	std::ofstream list("circle_check.txt");
  	if (list.is_open()){
  		for (std::vector<stim::vec3<float>>::const_iterator j = out2.begin(); j != out2.end(); ++j)
  			list << *j << '\n';
  	}
  	list.close();
  	std::cin >> n;
  
  }
  
  */
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
90
  
46820b25   Laila Saadatifard   create an obj fil...
91
  void ivote3(float* img, float std[], float anisotropy, float phi, float d_phi, unsigned int r[], int iter, float t, unsigned int conn[], 
f12505fb   Laila Saadatifard   upload the ivote ...
92
  			unsigned int x, unsigned int y, unsigned int z);
46820b25   Laila Saadatifard   create an obj fil...
93
  void lmax(float* center, float* vote, float t1, unsigned int conn[], unsigned int x, unsigned int y, unsigned int z);
f12505fb   Laila Saadatifard   upload the ivote ...
94
95
96
97
98
99
100
101
102
103
104
  
  void invert_data(float* cpuI, unsigned int x, unsigned int y, unsigned int z){
  		for(int ix = 0; ix < x; ix++){
  			for (int iy = 0; iy < y; iy++){
  				for (int iz = 0; iz < z; iz++){
  					int idx = iz * x * y + iy * x + ix;
  					cpuI[idx] = 255 - cpuI[idx];
  				}
  			}
  		}
  	}
94d437dd   Laila Saadatifard   ivote3 code compi...
105
  	
f12505fb   Laila Saadatifard   upload the ivote ...
106
107
  int main(int argc, char** argv){
  
94d437dd   Laila Saadatifard   ivote3 code compi...
108
109
110
111
112
113
114
115
116
117
  
  	cudaDeviceProp prop;
  	int count;
  	cudaGetDeviceCount(&count);
  	//printf("cudadevicecount: %i\n", count);
  	for (int i=0; i<count; i++){
  		cudaGetDeviceProperties(&prop, i);
  		printf("current device ID: %d\n", i);
  		printf("device name: %s\n", prop.name);
  		printf("total global mem: %lu\n", prop.totalGlobalMem);
89604e92   Laila Saadatifard   ivote3 run on the...
118
  		printf("shared memory per block: %lu\n", prop.sharedMemPerBlock);
94d437dd   Laila Saadatifard   ivote3 code compi...
119
  	}
89604e92   Laila Saadatifard   ivote3 run on the...
120
  		
f12505fb   Laila Saadatifard   upload the ivote ...
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
  	//output advertisement
  	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;
  	std::cout<<"========================================================================="<<std::endl<<std::endl;
  
  	stim::arglist args;
  
  #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");
  	args.add("t", "threshold value for the final result", "positive valu");
94d437dd   Laila Saadatifard   ivote3 code compi...
142
143
  	args.add("invert", "to invert the input data set", "string");
  	args.add("anisotropy", "anisotropy value of the imaging", "positive value");
f12505fb   Laila Saadatifard   upload the ivote ...
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
  	//parse the command line arguments.
  	args.parse(argc, argv);
  
  	//display the help text if requested
  	if(args["help"].is_set()){				
  		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;
  		exit(1);
  	}
  
  	//if the input and output files aren't specified, throw an error and exit
  	if(args.nargs() < 2){
  		std::cout<<"ERROR: two files must be specified for segmentation, enter ivote --help for options."<<std::endl<<std::endl;
  		exit(1);
  	}
  
  	//get the input image file
  	stim::filename Ifilename(args.arg(0));
  
  	//get the output file name
  	stim::filename OutName(args.arg(1));
  
  	//set the x, y, z.
  	int x = args["x"].as_int();
  	int y = args["y"].as_int();
  	int z = args["z"].as_int();
  
  	//set the threshold.
  	float t = args["t"].as_float();
94d437dd   Laila Saadatifard   ivote3 code compi...
177
178
  	//set the anisotropy
  	float anisotropy =  args["anisotropy"].as_float();
a744d027   Laila Saadatifard   upload the ivote3...
179
  	unsigned int rmax = 10;
46820b25   Laila Saadatifard   create an obj fil...
180
  	unsigned int r[3] = { 12, rmax, rmax};
94d437dd   Laila Saadatifard   ivote3 code compi...
181
182
  	float std = 5;
  	float sigma[3] = { std, std, std};
46820b25   Laila Saadatifard   create an obj fil...
183
  	unsigned int nlmax = 1;
94d437dd   Laila Saadatifard   ivote3 code compi...
184
185
  	unsigned int conn[3] = { nlmax, nlmax, nlmax};
  	float phi_deg = 25.0;
f12505fb   Laila Saadatifard   upload the ivote ...
186
  	float phi = phi_deg * pi /180;
94d437dd   Laila Saadatifard   ivote3 code compi...
187
188
  	int iter = 8;
  	float d_phi = phi/(iter+2);
f12505fb   Laila Saadatifard   upload the ivote ...
189
190
  	
  	std::string filename = Ifilename.str();
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
191
192
  	unsigned int bytes = x*y*z*sizeof(float);
  
f12505fb   Laila Saadatifard   upload the ivote ...
193
  	//allocate space on the cpu for the input data
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
194
195
  	float* cpuI = (float*) malloc(bytes);
  
f12505fb   Laila Saadatifard   upload the ivote ...
196
  	//load the input file into the cpuI
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
197
198
199
  	std::ifstream nissl(filename, std::ios::in | std::ios::binary);
  	nissl.read((char*)cpuI, bytes);
  	nissl.close();
94d437dd   Laila Saadatifard   ivote3 code compi...
200
201
  	if(args["invert"].is_set())
  		invert_data(cpuI, x, y, z);
f12505fb   Laila Saadatifard   upload the ivote ...
202
203
  	
  	//write a new file from the cpuI.
46820b25   Laila Saadatifard   create an obj fil...
204
  	std::ofstream original("shared2D-v1/inv-128.vol", std::ofstream::out | std::ofstream::binary);
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
205
206
  	original.write((char*)cpuI, bytes);
  	original.close();
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
207
  	
f12505fb   Laila Saadatifard   upload the ivote ...
208
  	//allocate space on the cpu for the output result
89604e92   Laila Saadatifard   ivote3 run on the...
209
  	float* cpu_out = (float*) malloc(bytes);
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
210
  	
f12505fb   Laila Saadatifard   upload the ivote ...
211
  	// call the ivote function
46820b25   Laila Saadatifard   create an obj fil...
212
213
  	//ivote3(cpu_out, cpuI, sigma, anisotropy, phi, d_phi, r, iter, t, conn, x, y, z);
  	ivote3(cpuI, sigma, anisotropy, phi, d_phi, r, iter, t, conn, x, y, z);
f12505fb   Laila Saadatifard   upload the ivote ...
214
  	//write the blurred file from the cpuI.
89604e92   Laila Saadatifard   ivote3 run on the...
215
  	std::ofstream fblur("shared2D-v8/vote8.vol", std::ofstream::out | std::ofstream::binary);
f12505fb   Laila Saadatifard   upload the ivote ...
216
217
  	fblur.write((char*)cpuI, bytes);
  	fblur.close();
a744d027   Laila Saadatifard   upload the ivote3...
218
219
220
221
222
223
224
  	
  	//stim::image<float>imgrad3;
  	//imgrad3.set_interleaved3(cpu_out, 128,128,128,3);
  	//std::ofstream fgx("syn/gx-128.vol", std::ofstream::out | std::ofstream::binary);
  	//fgx.write((char*)imgrad3.channel(0).data(), bytes);
  	//fgx.close();
  	
f12505fb   Laila Saadatifard   upload the ivote ...
225
  	//write the output file.
a744d027   Laila Saadatifard   upload the ivote3...
226
227
  	//for (int t0=0; t0<=5000; t0+=100){
  	//	float t1 = t0;
46820b25   Laila Saadatifard   create an obj fil...
228
229
230
  		int t0 = t;
  			lmax(cpu_out, cpuI, t, conn, x, y, z);
  			//std::ofstream fo("shared2D-v8/" + OutName.str(), std::ofstream::out | std::ofstream::binary);
a744d027   Laila Saadatifard   upload the ivote3...
231
  			std::ofstream fo( OutName.str()+std::to_string(t0)+".vol", std::ofstream::out | std::ofstream::binary);
46820b25   Laila Saadatifard   create an obj fil...
232
233
  			fo.write((char*)cpu_out, bytes);
  			fo.close();
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
234
  	
94d437dd   Laila Saadatifard   ivote3 code compi...
235
  	// creat a file for saving the list centers
46820b25   Laila Saadatifard   create an obj fil...
236
  	
a744d027   Laila Saadatifard   upload the ivote3...
237
238
239
240
  		//std::ofstream list("shared2D-v8/" + OutName.str()+std::to_string(t0)+".obj");
  		//// set the number of detected cells to zero.
  		//int nod = 0;
  		//if (list.is_open()){
94d437dd   Laila Saadatifard   ivote3 code compi...
241
  
a744d027   Laila Saadatifard   upload the ivote3...
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
  		//		for (int iz=0; iz<z; iz++){
  		//			for (int iy=0; iy<y; iy++){
  		//				for (int ix=0; ix<x; ix++){
  
  		//					int idx = iz * x * y + iy * x + ix;
  		//					if (cpu_out[idx]==1){
  		//						nod++;
  		//						list << "v" << "\t" << ix << "\t" << iy << "\t"<< iz << '\n' ;
  		//			
  		//					}
  		//				}
  		//			}
  		//		}
  		//		list << "p" << "\t";
  		//		for (unsigned int i_nod =1 ; i_nod <=nod; i_nod++){
  		//			list << i_nod << "\t";
  		//		}
  
  		//list.close();
  		//}
  	//}
f12505fb   Laila Saadatifard   upload the ivote ...
263
  		cudaDeviceReset();       
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
264
  	
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
265
  }