Blame view

cpp/main.cpp 5.36 KB
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
1
2
  #include <iostream>
  #include <fstream>
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
3
4
5
6
7
8
9
  #include <cuda_runtime.h>
  #include <stim/math/vector.h>
  #include <stim/parser/arguments.h>
  #include <stim/parser/filename.h>
  #include <stim/grids/image_stack.h>
  #include <stim/grids/grid.h>
  #include <stim/visualization/colormap.h>
f12505fb   Laila Saadatifard   upload the ivote ...
10
  #include <stim/image/image.h>
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
11
12
13
  #define pi	3.14159
  
  
94d437dd   Laila Saadatifard   ivote3 code compi...
14
  void ivote3(float* center, 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 ...
15
16
17
18
19
20
21
22
23
24
25
26
  			unsigned int x, unsigned int y, unsigned int z);
  
  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
102
103
104
105
106
107
  	//set the anisotropy
  	float anisotropy =  args["anisotropy"].as_float();
  	unsigned int rmax = 10 ;
  	unsigned int r[3] = { rmax, rmax, rmax};
  	float std = 5;
  	float sigma[3] = { std, std, std};
  	unsigned int nlmax = 5;
  	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.
89604e92   Laila Saadatifard   ivote3 run on the...
126
  	std::ofstream original("shared2D-v8/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
94d437dd   Laila Saadatifard   ivote3 code compi...
134
  	ivote3(cpu_out, cpuI, sigma, anisotropy, phi, d_phi, r, iter, t, conn, x, y, z);
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
135
  	
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.
89604e92   Laila Saadatifard   ivote3 run on the...
148
  	std::ofstream fo("shared2D-v8/" + OutName.str(), std::ofstream::out | std::ofstream::binary);
f12505fb   Laila Saadatifard   upload the ivote ...
149
150
  	fo.write((char*)cpu_out, bytes);
  	fo.close();
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
151
  	
94d437dd   Laila Saadatifard   ivote3 code compi...
152
153
  	// creat a file for saving the list centers
   
89604e92   Laila Saadatifard   ivote3 run on the...
154
  		std::ofstream list("shared2D-v8/center.txt");
94d437dd   Laila Saadatifard   ivote3 code compi...
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
  		if (list.is_open()){
  
  				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;
  							if (cpu_out[idx]==1){
  						list << ix << "\t" << iy << "\t"<< iz << '\n' ;
  					
  							}
  						}
  					}
  				}
  
  		list.close();
  		}
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
172
  	
f12505fb   Laila Saadatifard   upload the ivote ...
173
  		cudaDeviceReset();       
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
174
  	
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
175
  }