Medical Imaging Interaction Toolkit  2016.11.0
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
18 
20 
21 #include <mitkImageCast.h>
22 
24 {
26  int count = 0;
27  while ( ! maskIter.IsAtEnd() )
28  {
29  if (maskIter.GetVoxel() > 0)
30  ++count;
31  ++maskIter;
32  }
33  return count;
34 }
35 
36 Eigen::MatrixXd mitk::DCUtilities::DC3dDToMatrixXd(mitk::DataCollection::Pointer dc, std::string name, std::string mask)
37 {
38  std::vector<std::string> names;
39  names.push_back(name);
40  return DC3dDToMatrixXd(dc, names, mask);
41 }
42 
43 Eigen::MatrixXd mitk::DCUtilities::DC3dDToMatrixXd(mitk::DataCollection::Pointer dc, const std::vector<std::string> &names, std::string mask)
44 {
46 
47  int numberOfVoxels = DCUtilities::VoxelInMask(dc,mask);
48  int numberOfNames = names.size();
49 
51  std::vector<DataIterType> dataIter;
52 
53  for (int i = 0; i < numberOfNames; ++i)
54  {
55  DataIterType iter(dc, names[i]);
56  dataIter.push_back(iter);
57  }
58 
59  Eigen::MatrixXd result(numberOfVoxels, names.size());
60  int row = 0;
61  while ( ! maskIter.IsAtEnd() )
62  {
63  if (maskIter.GetVoxel() > 0)
64  {
65  for (int col = 0; col < numberOfNames; ++col)
66  {
67  result(row,col) = dataIter[col].GetVoxel();
68  }
69  ++row;
70  }
71  for (int col = 0; col < numberOfNames; ++col)
72  {
73  ++(dataIter[col]);
74  }
75  ++maskIter;
76  }
77  return result;
78 }
79 
80 Eigen::MatrixXi mitk::DCUtilities::DC3dDToMatrixXi(mitk::DataCollection::Pointer dc, std::string name, std::string mask)
81 {
82  std::vector<std::string> names;
83  names.push_back(name);
84  return DC3dDToMatrixXi(dc, names, mask);
85 }
86 
87 Eigen::MatrixXi mitk::DCUtilities::DC3dDToMatrixXi(mitk::DataCollection::Pointer dc, const std::vector<std::string> &names, std::string mask)
88 {
90 
91  int numberOfVoxels = DCUtilities::VoxelInMask(dc,mask);
92  int numberOfNames = names.size();
93 
95  std::vector<DataIterType> dataIter;
96 
97  for (int i = 0; i < numberOfNames; ++i)
98  {
99  DataIterType iter(dc, names[i]);
100  dataIter.push_back(iter);
101  }
102 
103  Eigen::MatrixXi result(numberOfVoxels, names.size());
104  result.setZero();
105  int row = 0;
106  while ( ! maskIter.IsAtEnd() )
107  {
108  if (maskIter.GetVoxel() > 0)
109  {
110  for (int col = 0; col < numberOfNames; ++col)
111  {
112  result(row,col) = dataIter[col].GetVoxel();
113  }
114  ++row;
115  }
116  for (int col = 0; col < numberOfNames; ++col)
117  {
118  ++(dataIter[col]);
119  }
120  ++maskIter;
121  }
122  return result;
123 }
124 
125 void mitk::DCUtilities::MatrixToDC3d(const Eigen::MatrixXd &matrix, mitk::DataCollection::Pointer dc, const std::vector<std::string> &names, std::string mask)
126 {
128 
129  int numberOfNames = names.size();
130 
132  std::vector<DataIterType> dataIter;
133 
134  for (int i = 0; i < numberOfNames; ++i)
135  {
136  EnsureDoubleImageInDC(dc,names[i],mask);
137  DataIterType iter(dc, names[i]);
138  dataIter.push_back(iter);
139  }
140 
141  int row = 0;
142  while ( ! maskIter.IsAtEnd() )
143  {
144  if (maskIter.GetVoxel() > 0)
145  {
146  for (int col = 0; col < numberOfNames; ++col)
147  {
148  dataIter[col].SetVoxel(matrix(row,col));
149  }
150  ++row;
151  }
152  for (int col = 0; col < numberOfNames; ++col)
153  {
154  ++(dataIter[col]);
155  }
156  ++maskIter;
157  }
158 }
159 
160 void mitk::DCUtilities::MatrixToDC3d(const Eigen::MatrixXi &matrix, mitk::DataCollection::Pointer dc, const std::vector<std::string> &names, std::string mask)
161 {
163 
164  int numberOfNames = names.size();
165 
167  std::vector<DataIterType> dataIter;
168 
169  for (int i = 0; i < numberOfNames; ++i)
170  {
171  EnsureUCharImageInDC(dc,names[i],mask);
172  DataIterType iter(dc, names[i]);
173  dataIter.push_back(iter);
174  }
175 
176  int row = 0;
177  while ( ! maskIter.IsAtEnd() )
178  {
179  if (maskIter.GetVoxel() > 0)
180  {
181  for (int col = 0; col < numberOfNames; ++col)
182  {
183  (dataIter[col]).SetVoxel(matrix(row,col));
184  }
185  ++row;
186  }
187  for (int col = 0; col < numberOfNames; ++col)
188  {
189  ++(dataIter[col]);
190  }
191  ++maskIter;
192  }
193 }
194 
195 void mitk::DCUtilities::MatrixToDC3d(const Eigen::MatrixXd &matrix, mitk::DataCollection::Pointer dc, const std::string &name, std::string mask)
196 {
197  std::vector<std::string> names;
198  names.push_back(name);
199  return MatrixToDC3d(matrix, dc, names, mask);
200 }
201 
202 void mitk::DCUtilities::MatrixToDC3d(const Eigen::MatrixXi &matrix, mitk::DataCollection::Pointer dc, const std::string &name, std::string mask)
203 {
204  std::vector<std::string> names;
205  names.push_back(name);
206  return MatrixToDC3d(matrix, dc, names, mask);
207 }
208 
209 void mitk::DCUtilities::EnsureUCharImageInDC(mitk::DataCollection::Pointer dc, std::string name, std::string origin)
210 {
211  typedef itk::Image<unsigned char, 3> FeatureImage;
212  typedef itk::Image<unsigned char, 3> LabelImage;
213 
215  while (!iter.IsAtEnd())
216  {
217  ++iter;
218  }
219 
220  if (dc->HasElement(origin))
221  {
222  LabelImage::Pointer originImage = dynamic_cast<LabelImage*>(dc->GetData(origin).GetPointer());
223  // = dynamic_cast<LabelImage*>(dc->GetItkImage<LabelImage>(origin).GetPointer());
224  if (!dc->HasElement(name) && originImage.IsNotNull())
225  {
226  MITK_INFO << "New unsigned char image necessary";
228  image->SetRegions(originImage->GetLargestPossibleRegion());
229  image->SetSpacing(originImage->GetSpacing());
230  image->SetOrigin(originImage->GetOrigin());
231  image->SetDirection(originImage->GetDirection());
232  image->Allocate();
233  image->FillBuffer(0);
234 
235  dc->AddData(dynamic_cast<itk::DataObject*>(image.GetPointer()),name,"");
236  }
237  }
238  for (std::size_t i = 0; i < dc->Size();++i)
239  {
240  mitk::DataCollection* newCol = dynamic_cast<mitk::DataCollection*>(dc->GetData(i).GetPointer());
241  if (newCol != 0)
242  {
243  EnsureUCharImageInDC(newCol, name, origin);
244  }
245  }
246 }
247 
248 void mitk::DCUtilities::EnsureDoubleImageInDC(mitk::DataCollection::Pointer dc, std::string name, std::string origin)
249 {
250  typedef itk::Image<double, 3> FeatureImage;
251  typedef itk::Image<unsigned char, 3> LabelImage;
252 
254  while (!iter.IsAtEnd())
255  {
256  ++iter;
257  }
258 
259  if (dc->HasElement(origin))
260  {
261  LabelImage::Pointer originImage = dynamic_cast<LabelImage*>(dc->GetData(origin).GetPointer());
262  if (!dc->HasElement(name) && originImage.IsNotNull())
263  {
264  MITK_INFO << "New double image necessary";
266  image->SetRegions(originImage->GetLargestPossibleRegion());
267  image->SetSpacing(originImage->GetSpacing());
268  image->SetOrigin(originImage->GetOrigin());
269  image->SetDirection(originImage->GetDirection());
270  image->Allocate();
271 
272  dc->AddData(dynamic_cast<itk::DataObject*>(image.GetPointer()),name,"");
273  }
274  }
275  for (std::size_t i = 0; i < dc->Size();++i)
276  {
277  mitk::DataCollection* newCol = dynamic_cast<mitk::DataCollection*>(dc->GetData(i).GetPointer());
278  if (newCol != 0)
279  {
280  EnsureDoubleImageInDC(newCol, name, origin);
281  }
282  }
283 }
itk::Image< mitk::ScalarType, 3 > FeatureImage
itk::SmartPointer< Self > Pointer
static void EnsureDoubleImageInDC(mitk::DataCollection::Pointer dc, std::string name, std::string origin)
#define MITK_INFO
Definition: mitkLogMacros.h:22
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)
static Eigen::MatrixXd DC3dDToMatrixXd(mitk::DataCollection::Pointer dc, std::string names, std::string mask)
static int VoxelInMask(mitk::DataCollection::Pointer dc, std::string mask)
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.