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
#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::writeAsFloat(const string& fname)
{
Time_Tracer ts(string("Volume::writeAsFloat" + fname).c_str());
const ColumnVector& outputvol = *this;
FSLIO* OP = FslOpen(fname.c_str(), "wb");
FslSetCalMinMax(OP,Minimum(),Maximum());
FslSetDim(OP,volinfo.x, volinfo.y, volinfo.z, 1);
FslSetVoxDim(OP,volinfo.vx, volinfo.vy, volinfo.vz, 0);
FslSetDataType(OP, DT_FLOAT);
FslSetIntent(OP, volinfo.intent_code, volinfo.intent_p1, volinfo.intent_p2,
volinfo.intent_p3);
int sizeVol = outputvol.Nrows();
float *qv = new float[sizeVol];
for (int i=1; i<=sizeVol; i++)
{
qv[i-1] = outputvol(i);
}
}
void Volume::writeAsInt(const string& fname)
{
Time_Tracer ts("Volume::writeAsInt");
const ColumnVector& outputvol = *this;
FSLIO* OP = FslOpen(fname.c_str(), "wb");
FslSetCalMinMax(OP,Minimum(),Maximum());
FslSetDim(OP, volinfo.x, volinfo.y, volinfo.z, 1);
FslSetVoxDim(OP, volinfo.vx, volinfo.vy, volinfo.vz, 0);
FslSetDataType(OP, DT_SIGNED_SHORT);
FslSetIntent(OP, volinfo.intent_code, volinfo.intent_p1, volinfo.intent_p2,
volinfo.intent_p3);
int sizeVol = outputvol.Nrows();
short *qv = new short[sizeVol];
for (int i=1; i<=sizeVol; i++)
{
qv[i-1] = (short)outputvol(i);
}
}
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);
FslGetIntent(IP, &(volinfo.intent_code), &(volinfo.intent_p1), &(volinfo.intent_p2),
&(volinfo.intent_p3));
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;
volinfo.miscinfo = FslInit();
FslCloneHeader(volinfo.miscinfo,IP);
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: