File: image.cpp

package info (click to toggle)
odin 2.0.5-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,224 kB
  • sloc: cpp: 62,639; sh: 4,541; makefile: 779
file content (264 lines) | stat: -rw-r--r-- 7,024 bytes parent folder | download | duplicates (5)
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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include "image.h"
#include "data.h"
#include <odinpara/sample.h>



Image::Image(const STD_string& label) : LDRblock(label) {
  magnitude.set_label("magnitude");
  magnitude.set_filemode(compressed);

  append_all_members();
}


Image& Image::operator = (const Image& i) {
  LDRblock::operator = (i);
  geo=i.geo;
  magnitude=i.magnitude;
  append_all_members();
  return *this;
}


unsigned int Image::size(axis ax) const {
  ndim nn=get_magnitude().get_extent();
  int index=nn.size()-1-ax;
  if(index>=0) return nn[index];
  return 1;
}



Image& Image::transpose_inplane(bool reverse_read, bool reverse_phase) {

  unsigned int d=magnitude.dim();
  if(d<2) return *this;

  geo.transpose_inplane(reverse_read,reverse_phase);

  // transpose image data
  farray magnitude_copy(magnitude);
  ndim shape=magnitude.get_extent();
  STD_swap(shape[d-1],shape[d-2]);
  magnitude.redim(shape);
  for(unsigned int i=0; i<magnitude.length(); i++) {
    ndim index(magnitude_copy.create_index(i));
    if(reverse_read) index[d-1]=shape[d-1]-1-index[d-1];
    if(reverse_phase) index[d-2]=shape[d-2]-1-index[d-2];
    STD_swap(index[d-1],index[d-2]);
    magnitude(index)=magnitude_copy[i];
  }

  return *this;
}




void  Image::append_all_members() {
  LDRblock::clear();
  LDRblock::merge(geo);
  append_member(magnitude);
}


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

ImageSet::ImageSet(const STD_string& label) : LDRblock(label) {
  Content.set_label("Content");

  append_all_members();
}


ImageSet::ImageSet(const Sample& smp) {
  Log<OdinData> odinlog(this,"ImageSet(Sample)");

  float min_fov=100.0;
  int min_size=64;


  farray sddata(smp.get_spinDensity());
  STD_string maplabel="Spin Density";
  sddata.normalize();
  ODINLOG(odinlog,normalDebug) << "sddata.get_extent()=" << sddata.get_extent() << STD_endl;


  float xFOV=smp.get_FOV(xAxis);
  float yFOV=smp.get_FOV(yAxis);
  float zFOV=smp.get_FOV(zAxis);
  float maxFOV=maxof3(xFOV,yFOV,zFOV);
  if(maxFOV<min_fov) maxFOV=min_fov;
  ODINLOG(odinlog,normalDebug) << "maxFOV/xFOV/yFOV/zFOV=" << maxFOV << "/" << xFOV << "/" << yFOV << "/" << zFOV << STD_endl;

  int nx=sddata.size(xDim);
  int ny=sddata.size(yDim);
  int nz=sddata.size(zDim);
  int nfreq=sddata.size(freqDim);
  int maxsize=int(maxof3(nx,ny,nz));
  if(maxsize<min_size) maxsize=min_size;
  ODINLOG(odinlog,normalDebug) << "maxsize/nx/ny/nz/nfreq=" << maxsize << "/" << nx << "/" << ny << "/" << nz << "/" << nfreq << STD_endl;

  STD_string dirlabel;

  // Sagittal & Coronal

  Geometry iasagcor;
  for(int ichan=0; ichan<n_directions; ichan++)  iasagcor.set_FOV(direction(ichan), maxFOV);

  farray data(maxsize,maxsize);

  dvector rpsvec(3);
  dvector xyzvec(3);

  for(int idir=sagittal; idir<=coronal; idir++) {

    data=0.0;

    iasagcor.set_orientation(sliceOrientation(idir));

    ODINLOG(odinlog,normalDebug) << "gradrotmatrix[" << idir << "]=" << iasagcor.get_gradrotmatrix().print() << STD_endl;

    for(int j=0; j<maxsize; j++) {
      for(int i=0; i<maxsize; i++) {

        rpsvec[0]=((i+0.5)/float(maxsize)-0.5)*maxFOV; // read
        rpsvec[1]=((j+0.5)/float(maxsize)-0.5)*maxFOV; // phase
        rpsvec[2]=0.0;
        xyzvec=iasagcor.transform(rpsvec);

        int ix=int(nx*(xyzvec[0]/xFOV+0.5)-0.5); // x
        int iy=int(ny*(xyzvec[1]/yFOV+0.5)-0.5); // y
        int iz=int(nz*(xyzvec[2]/zFOV+0.5)-0.5); // z

        if(ix>=0 && ix<nx && iy>=0 && iy<ny && iz>=0 && iz<nz) data(j,i)=sddata(0,0,iz,iy,ix);
      }
    }

    if(idir==sagittal) dirlabel="Sagittal";
    if(idir==coronal) dirlabel="Coronal";


    Image imgsagcor(maplabel+"("+dirlabel+")");
    iasagcor.set_label(dirlabel);
    imgsagcor.set_geometry(iasagcor);
    imgsagcor.set_magnitude(data);
    imgsagcor.transpose_inplane(); // for correct display in geoedit
    append_image(imgsagcor);
  }



  // Axial as multi slice
  dirlabel="Axial";
  Geometry ia(dirlabel);
  ia.set_FOV(readDirection, smp.get_FOV(xAxis));
  ia.set_FOV(phaseDirection,smp.get_FOV(yAxis));
  ODINLOG(odinlog,normalDebug) << "gradrotmatrix[axial]=" << ia.get_gradrotmatrix().print() << STD_endl;

  ia.set_nSlices(nz);
  float fovz=smp.get_FOV(zAxis);
  float dz=secureDivision(fovz,nz);
  ia.set_sliceThickness(dz);
  ia.set_sliceDistance(dz);

  Image img(maplabel+"("+dirlabel+")");
  img.set_geometry(ia);
  sddata.autosize();
  img.set_magnitude(sddata);
  append_image(img);
}




ImageSet& ImageSet::operator = (const ImageSet& is) {
  LDRblock::operator = (is);
  Content=is.Content;
  images=is.images;
  append_all_members();
  return *this;
}

ImageSet& ImageSet::append_image(const Image& img) {
  Log<OdinData> odinlog(this,"append_image");
  bool rename=false;
  if(img.get_label()=="" || LDRblock::parameter_exists(img.get_label())) rename=true;
  images.push_back(img);
  STD_list<Image>::iterator it=images.end();
  --it;
  if(rename) it->set_label("Image"+itos(images.size()-1));
  LDRblock::append(*it);

  unsigned int nimages=images.size();
  ODINLOG(odinlog,normalDebug) << "nimages=" << nimages << STD_endl;

  Content.resize(nimages);
  int index=0;
  for(it=images.begin(); it!=images.end(); ++it) {
    ODINLOG(odinlog,normalDebug) << "index/label=" << index << "/" << it->get_label() << STD_endl;
    Content[index]=it->get_label();
    index++;
  }
  return *this;
}


ImageSet& ImageSet::clear_images() {
  images.clear();
  Content.resize(0);
  return *this;
}


Image& ImageSet::get_image(unsigned int index) {
  Log<OdinData> odinlog(this,"get_image");
  unsigned int nimages=images.size();
  ODINLOG(odinlog,normalDebug) << "index/nimages=" << index << "/" << nimages << STD_endl;
  if(index>=nimages) return dummy;
  STD_list<Image>::iterator it=images.begin();
  for(unsigned int i=0; i<index; i++) ++it;
  return *it;
}


int ImageSet::load(const STD_string& filename, const LDRserBase& serializer) {
  Log<OdinData> odinlog(this,"load");

  clear_images();

  // check whether we have a whole set of images and determine the total num of images first
  int noiresult=Content.load(filename,serializer);
  svector contcopy=Content; // create copy because append_image will modify Content
  unsigned int nimages=contcopy.size();
  ODINLOG(odinlog,normalDebug) << "noiresult/nimages/Content=" << noiresult << "/" << nimages << "/" << contcopy.printbody() << STD_endl;

  int result=0;
  if(noiresult>0) {
    // create placeholders
    Image img;
    for(unsigned int i=0; i<nimages; i++) {
      img.set_label(contcopy[i]);
      append_image(img);
      ODINLOG(odinlog,normalDebug) << "Appended empty image for " << STD_string(contcopy[i]) << STD_endl;
    }
    ODINLOG(odinlog,normalDebug) << "Loading all images" << STD_endl;
    result=LDRblock::load(filename,serializer);
  } else {
    // try single image
    Image img;
    result=img.load(filename,serializer);
    if(result>0) {
      clear_images();
      append_image(img);
    }
  }
  return result;
}


void ImageSet::append_all_members() {
  LDRblock::clear();
  append_member(Content);
}