Newer
Older
/* volume.cc
Mark Woolrich, FMRIB Image Analysis Group
Copyright (C) 2002 University of Oxford */
/* CCOPYRIGHT */
#include <iostream>
#include <cstdlib>
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
#include "newmatap.h"
#include "newmatio.h"
#include "volume.h"
#include "utils/time_tracer.h"
using namespace NEWMAT;
using namespace Utilities;
namespace MISCMATHS {
void Volume::unthreshold()
{
Time_Tracer ts("Volume::unthreshold");
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++)
{
(*this)(int(preThresholdPositions(i)))=X(i);
}
}
void Volume::unthreshold(const VolumeInfo& pvolinfo,const ColumnVector& in)
{
volinfo = pvolinfo;
preThresholdPositions = in;
unthreshold();
}
void Volume::threshold()
{
Time_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++)
{
X(i)=(*this)(int(preThresholdPositions(i)));
}
(*this) = X;
}
void Volume::threshold(float thresh)
{
Time_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::threshold(const volume<int>& pmask)
// {
// Time_Tracer ts("Volume::threshold");
// int size = getVolumeSize();
// int j = 0;
// preThresholdPositions.ReSize(size);
// int i = 0;
// for(int x = 0; x < pmask.xsize(); x++)
// for(int y = 0; y < pmask.ysize(); y++)
// for(int z = 0; z < pmask.zsize(); z++)
// {
// i++;
// if(pmask(x,y,z))
// {
// 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)
{
Time_Tracer ts(string("Volume::writeAsFloat" + fname).c_str());
float fmin, fmax;
const ColumnVector& outputvol = *this;
FSLIO* OP = FslOpen(fname.c_str(), "wb");
FslSetDim(OP,volinfo.x, volinfo.y, volinfo.z, 1);
FslSetVoxDim(OP,volinfo.vx, volinfo.vy, volinfo.vz, 0);
FslSetDataType(OP, DT_FLOAT);
FslSetOriginator(OP, volinfo.originator);
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);
}
// fwrite(qv,sizeVol*sizeof(float),1,OP->imgfp);
//fwrite(&OP->header,sizeof(OP->header),1,OP->hdrfp);
}
void Volume::writeAsInt(const string& fname)
{
Time_Tracer ts("Volume::writeAsInt");
int fmin, fmax;
const ColumnVector& outputvol = *this;
FSLIO* OP = FslOpen(fname.c_str(), "wb");
FslSetDim(OP, volinfo.x, volinfo.y, volinfo.z, 1);
FslSetVoxDim(OP, volinfo.vx, volinfo.vy, volinfo.vz, 0);
FslSetDataType(OP, DT_SIGNED_SHORT);
FslSetOriginator(OP, volinfo.originator);
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);
}
FslSetMinMax(OP, (short)fmin, (short)(fmax+0.9999));
}
void Volume::read(const string& fname)
{
Time_Tracer ts(string("Volume::read" + fname).c_str());
FSLIO* IP = FslOpen(fname.c_str(), "rb");
ColumnVector& output = *this;
short x,y,z,v,type;
float vx,vy,vz,tr;
float slope, intercept;
int doscaling;
FslGetDim(IP,&x,&y,&z,&v);
FslGetVoxDim(IP,&vx,&vy,&vz,&tr);
FslGetOriginator(IP,volinfo.originator);
doscaling = FslGetIntensityScaling(IP,&slope,&intercept);
volinfo.x = x; volinfo.y = y; volinfo.z = z; volinfo.v = v;
volinfo.vx = vx; volinfo.vy = vy; volinfo.vz = vz; volinfo.tr = tr;
size_t imagesize=x*y*z;
output.ReSize(x*y*z);
switch(type)
{
case DT_SIGNED_SHORT:
{
short* sbuffer=new short[imagesize];
for(size_t j = 1; j<=(size_t)x*y*z; j++)
{
if (doscaling==0) { output(j)=sbuffer[j-1]; }
else { output(j)=(slope * sbuffer[j-1]) + intercept; }
}
delete[] sbuffer;
}
break;
case DT_FLOAT:
{
float* fbuffer=new float[imagesize];
for(size_t j = 1; j<=(size_t)x*y*z; j++)
{
if (doscaling==0) { output(j)=fbuffer[j-1]; }
else { output(j)=(slope * fbuffer[j-1]) + intercept; }
}
delete[] fbuffer;
}
break;
case DT_UNSIGNED_CHAR:
{
unsigned char* cbuffer=new unsigned char[imagesize];
for(size_t j = 1; j<=(size_t)x*y*z; j++)
{
if (doscaling==0) { output(j)=cbuffer[j-1]; }
else { output(j)=(slope * cbuffer[j-1]) + intercept; }
}
delete[] cbuffer;
}
break;
default: