f1402849
dmayerich
renewed commit
|
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
161
162
163
164
165
166
167
168
|
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkRAWImageIO.h"
#include "itkImageRegionIteratorWithIndex.h"
typedef itk::Image<unsigned char, 3> VOLType;
VOLType::Pointer rts_itkNewVOL(int x, int y, int z)
{
//Create a new image and allocate the desired amount of space
//typedef itk::Image<T, 3> ImageType;
VOLType::Pointer result = VOLType::New();
VOLType::RegionType region;
VOLType::SizeType size = {x, y, z};
region.SetSize(size);
VOLType::IndexType index = {0, 0, 0};
region.SetIndex(index);
result->SetRegions(region);
result->Allocate();
return result;
}
VOLType::Pointer rts_itkLoadVOL(const char* filename, unsigned int max_slices = 512)
{
//first load the header information
ifstream infile(filename, ios::in | ios::binary); //create the files stream
if(!infile)
return VOLType::New();
unsigned int size_x, size_y, size_z; //create variables to store the size of the data set
//load the dimensions of the data set
infile.read((char*)&size_x, sizeof(int)); //load the file header
infile.read((char*)&size_y, sizeof(int));
infile.read((char*)&size_z, sizeof(int));
//close the file
infile.close();
VOLType::Pointer volume;
//create the reader
typedef itk::ImageFileReader<VOLType> ReaderType;
typedef itk::RawImageIO<unsigned char, 3> RawType;
ReaderType::Pointer reader = ReaderType::New();
RawType::Pointer rawIO = RawType::New();
reader->SetImageIO(rawIO);
reader->SetFileName(filename);
//set the raw IO parameters
rawIO->SetFileTypeToBinary();
//set the format of the RAW file
rawIO->SetHeaderSize(12);
rawIO->SetDimensions(0, size_x);
rawIO->SetDimensions(1, size_y);
if(size_z < max_slices)
rawIO->SetDimensions(2, size_z);
else
rawIO->SetDimensions(2, max_slices);
//read the file
try
{
reader->Update();
}
catch(itk::ExceptionObject & exp)
{
std::cout<<exp<<std::endl;
}
volume = reader->GetOutput();
return volume;
}
VOLType::Pointer rts_itkLoadRAW(const char* filename, unsigned int size_x, unsigned int size_y, unsigned int size_z)
{
//first load the header information
ifstream infile(filename, ios::in | ios::binary); //create the files stream
if(!infile)
return VOLType::New();
//close the file
infile.close();
VOLType::Pointer volume;
//create the reader
typedef itk::ImageFileReader<VOLType> ReaderType;
typedef itk::RawImageIO<unsigned char, 3> RawType;
ReaderType::Pointer reader = ReaderType::New();
RawType::Pointer rawIO = RawType::New();
reader->SetImageIO(rawIO);
reader->SetFileName(filename);
//set the raw IO parameters
rawIO->SetFileTypeToBinary();
//set the format of the RAW file
rawIO->SetHeaderSize(0);
rawIO->SetDimensions(0, size_x);
rawIO->SetDimensions(1, size_y);
rawIO->SetDimensions(2, size_z);
//read the file
try
{
reader->Update();
}
catch(itk::ExceptionObject & exp)
{
std::cout<<exp<<std::endl;
}
volume = reader->GetOutput();
return volume;
}
void rts_itkSaveVOL(VOLType::Pointer itk_image, const char* filename)
{
typedef itk::ImageRegionIteratorWithIndex<VOLType> IteratorType;
IteratorType i(itk_image, itk_image->GetLargestPossibleRegion());
//create variables for size and position
VOLType::IndexType index;
VOLType::SizeType size;
//get the volume size
size = itk_image->GetLargestPossibleRegion().GetSize();
unsigned char* buffer = new unsigned char[size[0]*size[1]*size[2]];
for(i.GoToBegin(); !i.IsAtEnd(); ++i)
{
index = i.GetIndex();
buffer[index[0] + size[0] * (index[1] + index[2] * size[1])] = i.Get();
}
ofstream outfile(filename, ios::out | ios::binary); //create the binary file stream
//write the volume size to the file
outfile.write((char*)&size[0], sizeof(int));
outfile.write((char*)&size[1], sizeof(int));
outfile.write((char*)&size[2], sizeof(int));
outfile.write((char*)buffer, sizeof(char)*size[0]*size[1]*size[2]);
outfile.close();
}
|