4 #include <vtkSmartPointer.h> 5 #include <vtkMetaImageReader.h> 6 #include <vtkImageData.h> 24 m_img = vtkSmartPointer<vtkImageData>::New();
27 m_transform = Matrix4::Zero();
86 return (T*)m_img->GetScalarPointer();
95 return (T*)m_img->GetScalarPointer();
107 const double p[3])
const 113 double offset_x, offset_y;
115 m_transform(0,0)*p[0] +
116 m_transform(1,0)*p[1] +
117 m_transform(2,0)*p[2];
120 m_transform(0,0)*m_transform(0,3) +
121 m_transform(1,0)*m_transform(1,3) +
122 m_transform(2,0)*m_transform(2,3);
125 m_transform(0,1)*p[0] +
126 m_transform(1,1)*p[1] +
127 m_transform(2,1)*p[2];
130 m_transform(0,1)*m_transform(0,3) +
131 m_transform(1,1)*m_transform(1,3) +
132 m_transform(2,1)*m_transform(2,3);
170 template<
typename Dt>
174 vector<pair<int,int> > ptstack;
184 ptstack.push_back(make_pair(imgx, imgy));
185 curpt = imagedata[imgx + imgy*
getXSize()];
187 ret.push_back(curpt);
190 vector<bool> visited;
192 std::fill(visited.begin(), visited.end(),
false);
195 while(!ptstack.empty())
197 cur = ptstack.back();
200 const int img_x = cur.first;
201 const int img_y = cur.second;
207 curpt = imagedata[img_x + img_y*
getXSize()];
212 if(visited.at(
makehash(img_x, img_y)) )
215 ret.push_back(curpt);
217 ptstack.insert(ptstack.end(),
218 { make_pair(img_x-1,img_y),
219 make_pair(img_x+1,img_y),
220 make_pair(img_x,img_y-1),
221 make_pair(img_x,img_y+1)});
223 visited.at(
makehash(img_x, img_y)) =
true;
257 vtkSmartPointer<vtkMetaImageReader> reader= vtkSmartPointer<vtkMetaImageReader>::New();
258 vtkSmartPointer<ErrorObserver> errorObserver = vtkSmartPointer<ErrorObserver>::New();
259 reader->AddObserver(vtkCommand::ErrorEvent,errorObserver);
263 ss << prefix << i <<
".mhd";
264 string filename = ss.str();
265 reader->SetFileName(filename.c_str());
268 if(!reader->CanReadFile(filename.c_str())){
269 cerr << filename.c_str() << endl;
270 reportError(
"ERROR: Could not read velocity data \n");
273 vector<MetaImage> *ret =
new vector<MetaImage>();
274 while(reader->CanReadFile(filename.c_str()))
277 ret->at(i).setIdx(i);
278 ret->at(i).m_img->DeepCopy(reader->GetOutput());
280 #if VTK_MAJOR_VERSION <= 5 281 ret->at(i).m_img->Update();
285 if (errorObserver->GetError())
287 reportError(
"ERROR: Could not read velocity data \n"+ errorObserver->GetErrorMessage());
289 if (errorObserver->GetWarning()){
290 cerr <<
"Caught warning while reading velocity data! \n " << errorObserver->GetWarningMessage();
293 if ( reader->GetFileDimensionality() != 2){
298 ret->at(i).m_xsize = reader->GetWidth();
299 ret->at(i).m_ysize = reader->GetHeight();
301 spacing = reader->GetPixelSpacing();
302 ret->at(i).m_xspacing = spacing[0];
303 ret->at(i).m_yspacing = spacing[1];
305 std::ifstream infile(filename);
309 while (std::getline(infile, line))
311 if(line.find(
"Offset")!=std::string::npos)
314 stringstream ss(line);
320 ret->at(i).m_transform(k++,3)=std::stod(buf);
322 ret->at(i).m_transform(3,3)=1;
324 if(found >=numToFind)
break;
326 }
else if(line.find(
"TransformMatrix")!=std::string::npos)
329 stringstream ss(line);
340 ret->at(i).m_transform(k++,j)=std::stod(buf);
343 if(found >=numToFind)
break;
348 ss << prefix << ++i <<
".mhd";
350 reader->SetFileName(filename.c_str());
372 vtkSmartPointer<vtkImageData> m_img;
381 #endif //METAIMAGE_HPP
Eigen::Matrix< double, 4, 4 > Matrix4
void reportError(std::string errMsg)