Blame view

cpp/main.cpp 5.32 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
40
41
42
43
  
  	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);
  	}
  	
  	
  	
f12505fb   Laila Saadatifard   upload the ivote ...
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  	//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...
65
66
  	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 ...
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
  	//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...
100
101
102
103
104
105
106
107
108
  	//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 ...
109
  	float phi = phi_deg * pi /180;
94d437dd   Laila Saadatifard   ivote3 code compi...
110
111
  	int iter = 8;
  	float d_phi = phi/(iter+2);
f12505fb   Laila Saadatifard   upload the ivote ...
112
113
  	
  	std::string filename = Ifilename.str();
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
114
115
  	unsigned int bytes = x*y*z*sizeof(float);
  
f12505fb   Laila Saadatifard   upload the ivote ...
116
  	//allocate space on the cpu for the input data
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
117
118
  	float* cpuI = (float*) malloc(bytes);
  
f12505fb   Laila Saadatifard   upload the ivote ...
119
  	//load the input file into the cpuI
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
120
121
122
  	std::ifstream nissl(filename, std::ios::in | std::ios::binary);
  	nissl.read((char*)cpuI, bytes);
  	nissl.close();
94d437dd   Laila Saadatifard   ivote3 code compi...
123
124
  	if(args["invert"].is_set())
  		invert_data(cpuI, x, y, z);
f12505fb   Laila Saadatifard   upload the ivote ...
125
126
  	
  	//write a new file from the cpuI.
94d437dd   Laila Saadatifard   ivote3 code compi...
127
  	std::ofstream original("std5.5-r10.10-v8/inv-128.vol", std::ofstream::out | std::ofstream::binary);
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
128
129
  	original.write((char*)cpuI, bytes);
  	original.close();
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
130
  	
f12505fb   Laila Saadatifard   upload the ivote ...
131
  	//allocate space on the cpu for the output result
94d437dd   Laila Saadatifard   ivote3 code compi...
132
  	float* cpu_out = (float*) malloc(bytes*3);
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
133
  	
f12505fb   Laila Saadatifard   upload the ivote ...
134
  	// call the ivote function
94d437dd   Laila Saadatifard   ivote3 code compi...
135
  	ivote3(cpu_out, cpuI, sigma, anisotropy, phi, d_phi, r, iter, t, conn, x, y, z);
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
136
  	
f12505fb   Laila Saadatifard   upload the ivote ...
137
  	//write the blurred file from the cpuI.
94d437dd   Laila Saadatifard   ivote3 code compi...
138
  	std::ofstream fblur("vote-check/vote8.vol", std::ofstream::out | std::ofstream::binary);
f12505fb   Laila Saadatifard   upload the ivote ...
139
140
  	fblur.write((char*)cpuI, bytes);
  	fblur.close();
94d437dd   Laila Saadatifard   ivote3 code compi...
141
142
143
144
145
146
147
  	/*
  	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 ...
148
  	//write the output file.
94d437dd   Laila Saadatifard   ivote3 code compi...
149
  	std::ofstream fo("std5.5-r10.10-v8/" + OutName.str(), std::ofstream::out | std::ofstream::binary);
f12505fb   Laila Saadatifard   upload the ivote ...
150
151
  	fo.write((char*)cpu_out, bytes);
  	fo.close();
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
152
  	
94d437dd   Laila Saadatifard   ivote3 code compi...
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
  	// creat a file for saving the list centers
   
  		std::ofstream list("std5.5-r10.10-v8/center.txt");
  		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...
173
  	
f12505fb   Laila Saadatifard   upload the ivote ...
174
  		cudaDeviceReset();       
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
175
  	
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
176
  }