Blame view

cpp/main.cpp 4.01 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
  
  
f12505fb   Laila Saadatifard   upload the ivote ...
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
90
91
92
93
  void ivote3(float* center, float* img, float std[], float phi, float d_phi, unsigned int r[], int iter, float t, unsigned int conn[], 
  			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];
  				}
  			}
  		}
  	}
  
  int main(int argc, char** argv){
  
  	//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");
  	//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();
  
  	unsigned int r[3] = { 9, 9, 5};
  	float sigma[3] = { 3, 3, 1.5};
  	unsigned int conn[3] = { 5, 5, 3};
  	float phi_deg = 20.1;
  	float phi = phi_deg * pi /180;
  	int iter = 2;
  	float d_phi = phi/(iter -1);
  	
  	std::string filename = Ifilename.str();
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
94
95
  	unsigned int bytes = x*y*z*sizeof(float);
  
f12505fb   Laila Saadatifard   upload the ivote ...
96
  	//allocate space on the cpu for the input data
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
97
98
  	float* cpuI = (float*) malloc(bytes);
  
f12505fb   Laila Saadatifard   upload the ivote ...
99
  	//load the input file into the cpuI
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
100
101
102
103
  	std::ifstream nissl(filename, std::ios::in | std::ios::binary);
  	nissl.read((char*)cpuI, bytes);
  	nissl.close();
  	
f12505fb   Laila Saadatifard   upload the ivote ...
104
105
106
  	invert_data(cpuI, x, y, z);
  	
  	//write a new file from the cpuI.
6ef1dab9   Laila Saadatifard   fix one bug in th...
107
  	std::ofstream original("output/original_invert--512.vol", std::ofstream::out | std::ofstream::binary);
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
108
109
  	original.write((char*)cpuI, bytes);
  	original.close();
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
110
  	
f12505fb   Laila Saadatifard   upload the ivote ...
111
112
  	//allocate space on the cpu for the output result
  	float* cpu_out = (float*) malloc(bytes);
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
113
  	
f12505fb   Laila Saadatifard   upload the ivote ...
114
115
  	// call the ivote function
  	ivote3(cpu_out, cpuI, sigma, phi, d_phi, r, iter, t, conn, x, y, z);
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
116
  	
f12505fb   Laila Saadatifard   upload the ivote ...
117
  	//write the blurred file from the cpuI.
6ef1dab9   Laila Saadatifard   fix one bug in th...
118
  	std::ofstream fblur("output/v1--512.vol", std::ofstream::out | std::ofstream::binary);
f12505fb   Laila Saadatifard   upload the ivote ...
119
120
121
122
123
124
125
  	fblur.write((char*)cpuI, bytes);
  	fblur.close();
  
  	//write the output file.
  	std::ofstream fo("output/" + OutName.str(), std::ofstream::out | std::ofstream::binary);
  	fo.write((char*)cpu_out, bytes);
  	fo.close();
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
126
127
  	
  	
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
128
  	
f12505fb   Laila Saadatifard   upload the ivote ...
129
  		cudaDeviceReset();       
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
130
  	
c9aba18a   Laila Saadatifard   ivote3 on the GPU...
131
  }