Blame view

cpp/main.cpp 5.95 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
12
  #define pi	3.14159
  
  
46820b25   Laila Saadatifard   create an obj fil...
13
  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 ...
14
  			unsigned int x, unsigned int y, unsigned int z);
46820b25   Laila Saadatifard   create an obj fil...
15
  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 ...
16
17
18
19
20
21
22
23
24
25
26
  
  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...
27
  	
f12505fb   Laila Saadatifard   upload the ivote ...
28
29
  int main(int argc, char** argv){
  
94d437dd   Laila Saadatifard   ivote3 code compi...
30
31
32
33
34
35
36
37
38
39
  
  	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...
40
  		printf("shared memory per block: %lu\n", prop.sharedMemPerBlock);
94d437dd   Laila Saadatifard   ivote3 code compi...
41
  	}
89604e92   Laila Saadatifard   ivote3 run on the...
42
  		
f12505fb   Laila Saadatifard   upload the ivote ...
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  	//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...
64
65
  	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 ...
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
  	//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...
99
100
101
  	//set the anisotropy
  	float anisotropy =  args["anisotropy"].as_float();
  	unsigned int rmax = 10 ;
46820b25   Laila Saadatifard   create an obj fil...
102
  	unsigned int r[3] = { 12, rmax, rmax};
94d437dd   Laila Saadatifard   ivote3 code compi...
103
104
  	float std = 5;
  	float sigma[3] = { std, std, std};
46820b25   Laila Saadatifard   create an obj fil...
105
  	unsigned int nlmax = 1;
94d437dd   Laila Saadatifard   ivote3 code compi...
106
107
  	unsigned int conn[3] = { nlmax, nlmax, nlmax};
  	float phi_deg = 25.0;
f12505fb   Laila Saadatifard   upload the ivote ...
108
  	float phi = phi_deg * pi /180;
94d437dd   Laila Saadatifard   ivote3 code compi...
109
110
  	int iter = 8;
  	float d_phi = phi/(iter+2);
f12505fb   Laila Saadatifard   upload the ivote ...
111
112
  	
  	std::string filename = Ifilename.str();
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
113
114
  	unsigned int bytes = x*y*z*sizeof(float);
  
f12505fb   Laila Saadatifard   upload the ivote ...
115
  	//allocate space on the cpu for the input data
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
116
117
  	float* cpuI = (float*) malloc(bytes);
  
f12505fb   Laila Saadatifard   upload the ivote ...
118
  	//load the input file into the cpuI
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
119
120
121
  	std::ifstream nissl(filename, std::ios::in | std::ios::binary);
  	nissl.read((char*)cpuI, bytes);
  	nissl.close();
94d437dd   Laila Saadatifard   ivote3 code compi...
122
123
  	if(args["invert"].is_set())
  		invert_data(cpuI, x, y, z);
f12505fb   Laila Saadatifard   upload the ivote ...
124
125
  	
  	//write a new file from the cpuI.
46820b25   Laila Saadatifard   create an obj fil...
126
  	std::ofstream original("shared2D-v1/inv-128.vol", std::ofstream::out | std::ofstream::binary);
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
127
128
  	original.write((char*)cpuI, bytes);
  	original.close();
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
129
  	
f12505fb   Laila Saadatifard   upload the ivote ...
130
  	//allocate space on the cpu for the output result
89604e92   Laila Saadatifard   ivote3 run on the...
131
  	float* cpu_out = (float*) malloc(bytes);
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
132
  	
f12505fb   Laila Saadatifard   upload the ivote ...
133
  	// call the ivote function
46820b25   Laila Saadatifard   create an obj fil...
134
135
  	//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 ...
136
  	//write the blurred file from the cpuI.
89604e92   Laila Saadatifard   ivote3 run on the...
137
  	std::ofstream fblur("shared2D-v8/vote8.vol", std::ofstream::out | std::ofstream::binary);
f12505fb   Laila Saadatifard   upload the ivote ...
138
139
  	fblur.write((char*)cpuI, bytes);
  	fblur.close();
94d437dd   Laila Saadatifard   ivote3 code compi...
140
141
142
143
144
145
146
  	/*
  	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 ...
147
  	//write the output file.
46820b25   Laila Saadatifard   create an obj fil...
148
149
150
151
152
153
154
  	//for (int t0=2000; t0<=2500; t0+=100){
  		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);
  			std::ofstream fo("shared2D-v8/" + OutName.str()+std::to_string(t0)+".vol", std::ofstream::out | std::ofstream::binary);
  			fo.write((char*)cpu_out, bytes);
  			fo.close();
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
155
  	
94d437dd   Laila Saadatifard   ivote3 code compi...
156
  	// creat a file for saving the list centers
46820b25   Laila Saadatifard   create an obj fil...
157
158
159
160
  	
  		std::ofstream list("shared2D-v8/" + OutName.str()+std::to_string(t0)+".obj");
  		// set the number of detected cells to zero.
  		int nod = 0;
94d437dd   Laila Saadatifard   ivote3 code compi...
161
162
  		if (list.is_open()){
  
46820b25   Laila Saadatifard   create an obj fil...
163
  				for (int iz=0; iz<z; iz++){
94d437dd   Laila Saadatifard   ivote3 code compi...
164
  					for (int iy=0; iy<y; iy++){
46820b25   Laila Saadatifard   create an obj fil...
165
  						for (int ix=0; ix<x; ix++){
94d437dd   Laila Saadatifard   ivote3 code compi...
166
167
168
  
  							int idx = iz * x * y + iy * x + ix;
  							if (cpu_out[idx]==1){
46820b25   Laila Saadatifard   create an obj fil...
169
170
  								nod++;
  								list << "v" << "\t" << ix << "\t" << iy << "\t"<< iz << '\n' ;
94d437dd   Laila Saadatifard   ivote3 code compi...
171
172
173
174
175
  					
  							}
  						}
  					}
  				}
46820b25   Laila Saadatifard   create an obj fil...
176
177
178
179
  				list << "p" << "\t";
  				for (unsigned int i_nod =1 ; i_nod <=nod; i_nod++){
  					list << i_nod << "\t";
  				}
94d437dd   Laila Saadatifard   ivote3 code compi...
180
181
182
  
  		list.close();
  		}
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
183
  	
f12505fb   Laila Saadatifard   upload the ivote ...
184
  		cudaDeviceReset();       
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
185
  	
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
186
  }