Welcome, guest | Sign In | My Account | Store | Cart

This code reads volume data from a file and puts it into a scalar data structure for further use , it can also save out different slices as png files

C++, 160 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 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
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <pngwriter.h>



using namespace std;
using std::vector;


#define HEIGHT 528
#define WIDTH 528
#define DEPTH 5
#define SLICE 3
#define MULT 3

string volume_data = "/home/zahari/Desktop/brainDATAsmall.vol";
string output_image = "mri.png";





void Tokenize(const string& str,
                      vector<string>& tokens,
                      const string& delimiters = " ")
{
    // Skip delimiters at beginning.
    string::size_type lastPos = str.find_first_not_of(delimiters, 0);
    // Find first "non-delimiter".
    string::size_type pos     = str.find_first_of(delimiters, lastPos);

    while (string::npos != pos || string::npos != lastPos)
    {
        // Found a token, add it to the vector.
        tokens.push_back(str.substr(lastPos, pos - lastPos));
        // Skip delimiters.  Note the "not_of"
        lastPos = str.find_first_not_of(delimiters, pos);
        // Find next "non-delimiter"
        pos = str.find_first_of(delimiters, lastPos);
    }
}



int main() {
	
	
	FILE *fp;
	long len;
	char *buf;
	fp=fopen(volume_data.c_str(),"rb");
	fseek(fp,0,SEEK_END); //go to end
	len=ftell(fp); //get position at end (length)
	fseek(fp,0,SEEK_SET); //go to beg.
	buf=(char *)malloc(len); //malloc buffer
	fread(buf,len,1,fp); //read into buffer
	fclose(fp);

	double dens_value;
	int X_index;
	int Y_index;
	int Z_index;
	cout.precision(15);
	int xcounter;
	int ycounter;
	pngwriter png(HEIGHT,WIDTH,0,output_image.c_str());
    
    
    /////////////////////////////////////////
    
    
    vector<vector<vector<double> > > array3D;

  
  array3D.resize(HEIGHT);
  for (int i = 0; i < HEIGHT; ++i) {
    array3D[i].resize(WIDTH);

    for (int j = 0; j < WIDTH; ++j)
      array3D[i][j].resize(DEPTH);
  }

  

    
    
    ///////////////////////////////////////
    
    

	
	
	
     
   std::string initial_string;
   std::vector<string> int_list;
   Tokenize(buf,int_list,";");
   
   
   std::vector<string> vallist;

   for (vector<string>::iterator i = int_list.begin();
                           i != int_list.end() - 1;
                           ++i)
{
	

    initial_string = *i;

    vector<string> tokens;

   Tokenize(initial_string, tokens);
     
           for (vector<string>::iterator i = tokens.begin();
          	  
                            i != tokens.end();
                            ++i) 
           {
           cout<< *i <<"  ";
          	   }
          	  dens_value = strtod(tokens[3].c_str(),NULL);
          	  X_index = atoi(tokens[0].c_str());
          	  Y_index = atoi(tokens[1].c_str());
          	  Z_index = atoi(tokens[2].c_str());
          	  array3D[X_index][Y_index][Z_index] = dens_value * MULT;
          	  
          	  cout<< " " <<"X: "<< X_index <<
                    " " <<"Y: "<< Y_index <<
          	          " " <<"Z: "<< Z_index <<
          	          " " <<"Value:"<< dens_value <<
          	          " " <<"               Array Value:"<< array3D[X_index][Y_index][Z_index] <<endl;

          	          
          	          
          	          
}


   for(ycounter = 1; ycounter < HEIGHT;ycounter++)
     {
     	     for(xcounter = 1; xcounter < WIDTH;xcounter++)
     	     {
     	             png.plot(xcounter,-1, 
     	             	      array3D[xcounter][ycounter][SLICE], 
     	             	      array3D[xcounter][ycounter][SLICE], 
     	             	      array3D[xcounter][ycounter][SLICE]);

     	
             }
     }

png.close();


cout << "---::::::: " << HEIGHT*WIDTH*DEPTH << " :::::::---  processed." << endl;
return 0;
}
Created by Zahari Dichev on Fri, 19 Aug 2011 (MIT)
C++ recipes (21)
Zahari Dichev's recipes (1)

Required Modules

Other Information and Tasks