Newer
Older
/* Volume.cc
Mark Woolrich, FMRIB Image Analysis Group
Copyright (C) 1999-2000 University of Oxford */
/* CCOPYRIGHT */
#include "newmat/newmatap.h"
#include "newmat/newmatio.h"
int sizeVolUnthresholded = getUnthresholdSize();
int sizeVolThresholded = getVolumeSize();
// Increase this to required size and set to 0
Release();
ColumnVector X=*this;
ReSize(sizeVolUnthresholded);
ColumnVector::operator=(0);
// Loop through volume restoring non thresholded values:
for(int i = 1; i <= sizeVolThresholded; i++)
{
void Volume::unthreshold(const VolumeSeries::Dims& pdims,const ColumnVector& in)
{
dims = pdims;
preThresholdPositions = in;
unthreshold();
}
void Volume::threshold()
{
Tracer ts("Volume::threshold");
int sizeVol = preThresholdPositions.Nrows();
ColumnVector X(sizeVol);
// Loop through pulling out non thresholded values
for(int i = 1; i <= sizeVol; i++)
{
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
}
(*this) = X;
}
void Volume::threshold(float thresh)
{
Tracer ts("Volume::threshold");
int size = getVolumeSize();
int j = 0;
preThresholdPositions.ReSize(size);
float m = 0;
for(int i = 1; i <= size; i++)
{
m = (*this)(i);
if(m > thresh)
{
j++;
preThresholdPositions(j) = i;
(*this)(j) = (*this)(i);
}
}
// Discard rest:
*this = Rows(1,j);
preThresholdPositions = preThresholdPositions.Rows(1,j);
}
void Volume::writeAsFloat(const string& fname)
{
Tracer ts("Volume::writeAsFloat");
float fmin, fmax;
const ColumnVector& outputvol = *this;
AVW* OP = AvwOpen(fname.c_str(), "wc");
AvwSetDim(OP,dims.x, dims.y, dims.z, 1);
AvwSetVoxDim(OP,dims.vx, dims.vy, dims.vz, 0);
AvwSetDataType(OP, DT_FLOAT);
int sizeVol = outputvol.Nrows();
float *qv = new float[sizeVol];
fmin = outputvol(1);
fmax = outputvol(1);
for (int i=1; i<=sizeVol; i++)
{
if(outputvol(i) < fmin)
fmin = outputvol(i);
else if(outputvol(i) > fmax)
fmax = outputvol(i);
qv[i-1] = outputvol(i);
}
AvwSetMinMax(OP, (short)fmin, (short)fmax);
// fwrite(qv,sizeVol*sizeof(float),1,OP->imgfp);
//fwrite(&OP->header,sizeof(OP->header),1,OP->hdrfp);
AvwWriteVolumes(OP, qv, 1);
delete [] qv;
AvwClose(OP);
}
void Volume::writeAsInt(const string& fname)
{
Tracer ts("Volume::writeAsInt");
int fmin, fmax;
const ColumnVector& outputvol = *this;
AVW* OP = AvwOpen(fname.c_str(), "wc");
AvwSetDim(OP, dims.x, dims.y, dims.z, 1);
AvwSetVoxDim(OP, dims.vx, dims.vy, dims.vz, 0);
AvwSetDataType(OP, DT_SIGNED_SHORT);
int sizeVol = outputvol.Nrows();
short *qv = new short[sizeVol];
fmin = (int)outputvol(1);
fmax = (int)outputvol(1);
for (int i=1; i<=sizeVol; i++)
{
if(outputvol(i) < fmin)
fmin = (int)outputvol(i);
else if(outputvol(i) > fmax)
fmax = (int)outputvol(i);
qv[i-1] = (short)outputvol(i);
}
AvwSetMinMax(OP, (short)fmin, (short)(fmax+0.9999));
AvwWriteVolumes(OP, qv, 1);
delete [] qv;
AvwClose(OP);
}
void Volume::read(const string& fname)
{
Tracer ts("Volume::read");
AVW* IP = AvwOpen(fname.c_str(), "r");
ColumnVector& output = *this;
short x,y,z,v,type;
float vx,vy,vz,tr;
AvwGetDim(IP,&x,&y,&z,&v);
AvwGetVoxDim(IP,&vx,&vy,&vz,&tr);
dims.x = x; dims.y = y; dims.z = z; dims.v = v;
dims.vx = vx; dims.vy = vy; dims.vz = vz; dims.tr = tr;
size_t imagesize=x*y*z;
AvwGetDataType(IP,&type);
output.ReSize(x*y*z);
switch(type)
{
case DT_SIGNED_SHORT:
{
short* sbuffer=new short[imagesize];
AvwReadVolumes(IP,sbuffer,v);
for(size_t j = 1; j<=(size_t)x*y*z; j++)
{
output(j)=sbuffer[j-1];
}
delete[] sbuffer;
}
break;
case DT_FLOAT:
{
float* fbuffer=new float[imagesize];
AvwReadVolumes(IP,fbuffer,v);
for(size_t j = 1; j<=(size_t)x*y*z; j++)
{
output(j)=fbuffer[j-1];
}
delete[] fbuffer;
}
break;
case DT_UNSIGNED_CHAR:
{
unsigned char* cbuffer=new unsigned char[imagesize];
AvwReadVolumes(IP,cbuffer,v);
for(size_t j = 1; j<=(size_t)x*y*z; j++)
{
output(j)=cbuffer[j-1];
}
delete[] cbuffer;
}
break;
default:
perror("AvwRead: DT not supported");
}
AvwClose(IP);
return;
}
}