Medical Imaging Interaction Toolkit  2018.4.99-3e3f1a6e
Medical Imaging Interaction Toolkit
mitkDataCollectionUtilities.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
14 
16 
17 #include <mitkImageCast.h>
18 
20 {
22  int count = 0;
23  while ( ! maskIter.IsAtEnd() )
24  {
25  if (maskIter.GetVoxel() > 0)
26  ++count;
27  ++maskIter;
28  }
29  return count;
30 }
31 
32 Eigen::MatrixXd mitk::DCUtilities::DC3dDToMatrixXd(mitk::DataCollection::Pointer dc, std::string name, std::string mask)
33 {
34  std::vector<std::string> names;
35  names.push_back(name);
36  return DC3dDToMatrixXd(dc, names, mask);
37 }
38 
39 Eigen::MatrixXd mitk::DCUtilities::DC3dDToMatrixXd(mitk::DataCollection::Pointer dc, const std::vector<std::string> &names, std::string mask)
40 {
42 
43  int numberOfVoxels = DCUtilities::VoxelInMask(dc,mask);
44  int numberOfNames = names.size();
45 
47  std::vector<DataIterType> dataIter;
48 
49  for (int i = 0; i < numberOfNames; ++i)
50  {
51  DataIterType iter(dc, names[i]);
52  dataIter.push_back(iter);
53  }
54 
55  Eigen::MatrixXd result(numberOfVoxels, names.size());
56  int row = 0;
57  while ( ! maskIter.IsAtEnd() )
58  {
59  if (maskIter.GetVoxel() > 0)
60  {
61  for (int col = 0; col < numberOfNames; ++col)
62  {
63  result(row,col) = dataIter[col].GetVoxel();
64  }
65  ++row;
66  }
67  for (int col = 0; col < numberOfNames; ++col)
68  {
69  ++(dataIter[col]);
70  }
71  ++maskIter;
72  }
73  return result;
74 }
75 
76 Eigen::MatrixXi mitk::DCUtilities::DC3dDToMatrixXi(mitk::DataCollection::Pointer dc, std::string name, std::string mask)
77 {
78  std::vector<std::string> names;
79  names.push_back(name);
80  return DC3dDToMatrixXi(dc, names, mask);
81 }
82 
83 Eigen::MatrixXi mitk::DCUtilities::DC3dDToMatrixXi(mitk::DataCollection::Pointer dc, const std::vector<std::string> &names, std::string mask)
84 {
86 
87  int numberOfVoxels = DCUtilities::VoxelInMask(dc,mask);
88  int numberOfNames = names.size();
89 
91  std::vector<DataIterType> dataIter;
92 
93  for (int i = 0; i < numberOfNames; ++i)
94  {
95  DataIterType iter(dc, names[i]);
96  dataIter.push_back(iter);
97  }
98 
99  Eigen::MatrixXi result(numberOfVoxels, names.size());
100  result.setZero();
101  int row = 0;
102  while ( ! maskIter.IsAtEnd() )
103  {
104  if (maskIter.GetVoxel() > 0)
105  {
106  for (int col = 0; col < numberOfNames; ++col)
107  {
108  result(row,col) = dataIter[col].GetVoxel();
109  }
110  ++row;
111  }
112  for (int col = 0; col < numberOfNames; ++col)
113  {
114  ++(dataIter[col]);
115  }
116  ++maskIter;
117  }
118  return result;
119 }
120 
121 void mitk::DCUtilities::MatrixToDC3d(const Eigen::MatrixXd &matrix, mitk::DataCollection::Pointer dc, const std::vector<std::string> &names, std::string mask)
122 {
124 
125  int numberOfNames = names.size();
126 
128  std::vector<DataIterType> dataIter;
129 
130  for (int i = 0; i < numberOfNames; ++i)
131  {
132  EnsureDoubleImageInDC(dc,names[i],mask);
133  DataIterType iter(dc, names[i]);
134  dataIter.push_back(iter);
135  }
136 
137  int row = 0;
138  while ( ! maskIter.IsAtEnd() )
139  {
140  if (maskIter.GetVoxel() > 0)
141  {
142  for (int col = 0; col < numberOfNames; ++col)
143  {
144  dataIter[col].SetVoxel(matrix(row,col));
145  }
146  ++row;
147  }
148  for (int col = 0; col < numberOfNames; ++col)
149  {
150  ++(dataIter[col]);
151  }
152  ++maskIter;
153  }
154 }
155 
156 void mitk::DCUtilities::MatrixToDC3d(const Eigen::MatrixXi &matrix, mitk::DataCollection::Pointer dc, const std::vector<std::string> &names, std::string mask)
157 {
159 
160  int numberOfNames = names.size();
161 
163  std::vector<DataIterType> dataIter;
164 
165  for (int i = 0; i < numberOfNames; ++i)
166  {
167  EnsureUCharImageInDC(dc,names[i],mask);
168  DataIterType iter(dc, names[i]);
169  dataIter.push_back(iter);
170  }
171 
172  int row = 0;
173  while ( ! maskIter.IsAtEnd() )
174  {
175  if (maskIter.GetVoxel() > 0)
176  {
177  for (int col = 0; col < numberOfNames; ++col)
178  {
179  (dataIter[col]).SetVoxel(matrix(row,col));
180  }
181  ++row;
182  }
183  for (int col = 0; col < numberOfNames; ++col)
184  {
185  ++(dataIter[col]);
186  }
187  ++maskIter;
188  }
189 }
190 
191 void mitk::DCUtilities::MatrixToDC3d(const Eigen::MatrixXd &matrix, mitk::DataCollection::Pointer dc, const std::string &name, std::string mask)
192 {
193  std::vector<std::string> names;
194  names.push_back(name);
195  return MatrixToDC3d(matrix, dc, names, mask);
196 }
197 
198 void mitk::DCUtilities::MatrixToDC3d(const Eigen::MatrixXi &matrix, mitk::DataCollection::Pointer dc, const std::string &name, std::string mask)
199 {
200  std::vector<std::string> names;
201  names.push_back(name);
202  return MatrixToDC3d(matrix, dc, names, mask);
203 }
204 
205 void mitk::DCUtilities::EnsureUCharImageInDC(mitk::DataCollection::Pointer dc, std::string name, std::string origin)
206 {
207  typedef itk::Image<unsigned char, 3> FeatureImage;
208  typedef itk::Image<unsigned char, 3> LabelImage;
209 
211  while (!iter.IsAtEnd())
212  {
213  ++iter;
214  }
215 
216  if (dc->HasElement(origin))
217  {
218  LabelImage::Pointer originImage = dynamic_cast<LabelImage*>(dc->GetData(origin).GetPointer());
219  // = dynamic_cast<LabelImage*>(dc->GetItkImage<LabelImage>(origin).GetPointer());
220  if (!dc->HasElement(name) && originImage.IsNotNull())
221  {
222  MITK_INFO << "New unsigned char image necessary";
223  FeatureImage::Pointer image = FeatureImage::New();
224  image->SetRegions(originImage->GetLargestPossibleRegion());
225  image->SetSpacing(originImage->GetSpacing());
226  image->SetOrigin(originImage->GetOrigin());
227  image->SetDirection(originImage->GetDirection());
228  image->Allocate();
229  image->FillBuffer(0);
230 
231  dc->AddData(dynamic_cast<itk::DataObject*>(image.GetPointer()),name,"");
232  }
233  }
234  for (std::size_t i = 0; i < dc->Size();++i)
235  {
236  mitk::DataCollection* newCol = dynamic_cast<mitk::DataCollection*>(dc->GetData(i).GetPointer());
237  if (newCol != nullptr)
238  {
239  EnsureUCharImageInDC(newCol, name, origin);
240  }
241  }
242 }
243 
244 void mitk::DCUtilities::EnsureDoubleImageInDC(mitk::DataCollection::Pointer dc, std::string name, std::string origin)
245 {
246  typedef itk::Image<double, 3> FeatureImage;
247  typedef itk::Image<unsigned char, 3> LabelImage;
248 
250  while (!iter.IsAtEnd())
251  {
252  ++iter;
253  }
254 
255  if (dc->HasElement(origin))
256  {
257  LabelImage::Pointer originImage = dynamic_cast<LabelImage*>(dc->GetData(origin).GetPointer());
258  if (!dc->HasElement(name) && originImage.IsNotNull())
259  {
260  MITK_INFO << "New double image necessary";
261  FeatureImage::Pointer image = FeatureImage::New();
262  image->SetRegions(originImage->GetLargestPossibleRegion());
263  image->SetSpacing(originImage->GetSpacing());
264  image->SetOrigin(originImage->GetOrigin());
265  image->SetDirection(originImage->GetDirection());
266  image->Allocate();
267 
268  dc->AddData(dynamic_cast<itk::DataObject*>(image.GetPointer()),name,"");
269  }
270  }
271  for (std::size_t i = 0; i < dc->Size();++i)
272  {
273  mitk::DataCollection* newCol = dynamic_cast<mitk::DataCollection*>(dc->GetData(i).GetPointer());
274  if (newCol != nullptr)
275  {
276  EnsureDoubleImageInDC(newCol, name, origin);
277  }
278  }
279 }
static void EnsureDoubleImageInDC(mitk::DataCollection::Pointer dc, std::string name, std::string origin)
#define MITK_INFO
Definition: mitkLogMacros.h:18
static void EnsureUCharImageInDC(mitk::DataCollection::Pointer dc, std::string name, std::string origin)
static void MatrixToDC3d(const Eigen::MatrixXd &matrix, mitk::DataCollection::Pointer dc, const std::vector< std::string > &names, std::string mask)
static Eigen::MatrixXi DC3dDToMatrixXi(mitk::DataCollection::Pointer dc, std::string name, std::string mask)
mitk::Image::Pointer image
static Eigen::MatrixXd DC3dDToMatrixXd(mitk::DataCollection::Pointer dc, std::string names, std::string mask)
static int VoxelInMask(mitk::DataCollection::Pointer dc, std::string mask)
mitk::Image::Pointer mask