Fraxinus  16.5.0-fx-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxImageAlgorithms.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #include "cxImageAlgorithms.h"
34 
35 #include <vtkImageData.h>
36 #include <vtkImageReslice.h>
37 #include <vtkMatrix4x4.h>
38 
39 #include <vtkImageResample.h>
40 #include <vtkImageClip.h>
41 
42 #include "cxImage.h"
43 #include "cxPatientModelService.h"
44 #include "cxUtilHelpers.h"
45 #include "cxImageTF3D.h"
46 #include "cxImageLUT2D.h"
48 
49 #include "cxTime.h"
50 #include "cxVolumeHelpers.h"
51 
52 
53 namespace cx
54 {
55 
60 {
61  //TODO: fix error:
62  // There is an error in the transfer functions of the returned image from this function
63 
64  // provide a resampled volume for algorithms requiring that (such as PickerRep)
65  vtkMatrix4x4Ptr orientatorMatrix = vtkMatrix4x4Ptr::New();
66  vtkImageReslicePtr orientator = vtkImageReslicePtr::New();
67  orientator->SetInputData(image->getBaseVtkImageData());
68  orientator->SetInterpolationModeToLinear();
69  orientator->SetOutputDimensionality(3);
70  orientator->SetResliceAxes(qMd.inv().getVtkMatrix());
71  orientator->AutoCropOutputOn();
72  orientator->Update();
73  vtkImageDataPtr rawResult = orientator->GetOutput();
74 
75 // rawResult->Update();
76 
77  QString uid = image->getUid() + "_or%1";
78  QString name = image->getName()+" or%1";
79 // ImagePtr oriented = dataManager->createDerivedImage(rawResult, uid, name, image);
80 
81  ImagePtr oriented = createDerivedImage(dataManager,
82  uid, name,
83  rawResult, image);
84 
85  oriented->get_rMd_History()->setRegistration(image->get_rMd() * qMd.inv());
86  oriented->mergevtkSettingsIntosscTransform();
87 
88  return oriented;
89 }
90 
94 ImagePtr resampleImage(PatientModelServicePtr dataManager, ImagePtr image, const Vector3D spacing, QString uid, QString name)
95 {
96  vtkImageResamplePtr resampler = vtkImageResamplePtr::New();
97  resampler->SetInputData(image->getBaseVtkImageData());
98  resampler->SetAxisOutputSpacing(0, spacing[0]);
99  resampler->SetAxisOutputSpacing(1, spacing[1]);
100  resampler->SetAxisOutputSpacing(2, spacing[2]);
101  resampler->Update();
102  vtkImageDataPtr rawResult = resampler->GetOutput();
103 
104  if (uid.isEmpty())
105  {
106  uid = image->getUid() + "_res%1";
107  name = image->getName()+" res%1";
108  }
109 
110  ImagePtr retval = createDerivedImage(dataManager,
111  uid, name,
112  rawResult, image);
113  return retval;
114 }
115 
120 {
121  Vector3D spacing(image->getBaseVtkImageData()->GetSpacing());
122  return resampleImage(dataManager, image, spacing, image->getUid()+"_copy%1", image->getName()+" copy%1");
123 }
124 
129 {
130  vtkImageClipPtr clip = vtkImageClipPtr::New();
131  clip->SetInputData(input);
132  clip->SetOutputWholeExtent(cropbox.begin());
133  clip->ClipDataOn();
134  clip->Update();
135  vtkImageDataPtr rawResult = clip->GetOutput();
136 
137 // rawResult->Update();
138 // rawResult->UpdateInformation();
139  rawResult->ComputeBounds();
140  return rawResult;
141 }
142 
147 {
148  DoubleBoundingBox3D bb = image->getCroppingBox();
149  double* sp = image->getBaseVtkImageData()->GetSpacing();
150  IntBoundingBox3D cropbox(
151  static_cast<int>(bb[0]/sp[0]+0.5), static_cast<int>(bb[1]/sp[0]+0.5),
152  static_cast<int>(bb[2]/sp[1]+0.5), static_cast<int>(bb[3]/sp[1]+0.5),
153  static_cast<int>(bb[4]/sp[2]+0.5), static_cast<int>(bb[5]/sp[2]+0.5));
154  vtkImageDataPtr rawResult = cropImage(image->getBaseVtkImageData(), cropbox);
155 
156  QString uid = image->getUid() + "_crop%1";
157  QString name = image->getName()+" crop%1";
158  ImagePtr result = createDerivedImage(dataManager,
159  uid, name,
160  rawResult, image);
161  result->mergevtkSettingsIntosscTransform();
162 
163  return result;
164 }
165 
168 QDateTime extractTimestamp(QString text)
169 {
170  // retrieve timestamp as
171  QRegExp tsReg("[0-9]{8}T[0-9]{6}");
172  if (tsReg.indexIn(text)>0)
173  {
174  QDateTime datetime = QDateTime::fromString(tsReg.cap(0), timestampSecondsFormat());
175  return datetime;
176  }
177  return QDateTime();
178 }
179 
180 
181 
182 
183 } // namespace cx
vtkSmartPointer< class vtkMatrix4x4 > vtkMatrix4x4Ptr
Definition: cxMathBase.h:58
ImagePtr resampleImage(PatientModelServicePtr dataManager, ImagePtr image, Transform3D qMd)
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
vtkImageDataPtr cropImage(vtkImageDataPtr input, IntBoundingBox3D cropbox)
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
QString timestampSecondsFormat()
Definition: cxTime.cpp:39
ImagePtr duplicateImage(PatientModelServicePtr dataManager, ImagePtr image)
ImagePtr createDerivedImage(PatientModelServicePtr dataManager, QString uid, QString name, vtkImageDataPtr raw, ImagePtr parent)
vtkSmartPointer< class vtkImageReslice > vtkImageReslicePtr
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
Representation of an integer bounding box in 3D. The data are stored as {xmin,xmax,ymin,ymax,zmin,zmax}, in order to simplify communication with vtk.
Representation of a floating-point bounding box in 3D. The data are stored as {xmin,xmax,ymin,ymax,zmin,zmax}, in order to simplify communication with vtk.
vtkSmartPointer< class vtkImageResample > vtkImageResamplePtr
vtkSmartPointer< class vtkImageClip > vtkImageClipPtr
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
QDateTime extractTimestamp(QString text)
vtkSmartPointer< class vtkImageData > vtkImageDataPtr