CustusX  15.3.4-beta
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 #include "vtkImageShiftScale.h"
42 
43 #include "cxImage.h"
44 #include "cxPatientModelService.h"
45 #include "cxUtilHelpers.h"
46 #include "cxImageTF3D.h"
47 #include "cxImageLUT2D.h"
49 
50 #include "cxTime.h"
51 #include "cxVolumeHelpers.h"
52 
53 
54 namespace cx
55 {
56 
61 {
62  //TODO: fix error:
63  // There is an error in the transfer functions of the returned image from this function
64 
65  // provide a resampled volume for algorithms requiring that (such as PickerRep)
66  vtkMatrix4x4Ptr orientatorMatrix = vtkMatrix4x4Ptr::New();
67  vtkImageReslicePtr orientator = vtkImageReslicePtr::New();
68  orientator->SetInputData(image->getBaseVtkImageData());
69  orientator->SetInterpolationModeToLinear();
70  orientator->SetOutputDimensionality(3);
71  orientator->SetResliceAxes(qMd.inv().getVtkMatrix());
72  orientator->AutoCropOutputOn();
73  orientator->Update();
74  vtkImageDataPtr rawResult = orientator->GetOutput();
75 
76 // rawResult->Update();
77 
78  QString uid = image->getUid() + "_or%1";
79  QString name = image->getName()+" or%1";
80 // ImagePtr oriented = dataManager->createDerivedImage(rawResult, uid, name, image);
81 
82  ImagePtr oriented = createDerivedImage(dataManager,
83  uid, name,
84  rawResult, image);
85 
86  oriented->get_rMd_History()->setRegistration(image->get_rMd() * qMd.inv());
87  oriented->mergevtkSettingsIntosscTransform();
88 
89  return oriented;
90 }
91 
95 ImagePtr resampleImage(PatientModelServicePtr dataManager, ImagePtr image, const Vector3D spacing, QString uid, QString name)
96 {
97  vtkImageResamplePtr resampler = vtkImageResamplePtr::New();
98  resampler->SetInputData(image->getBaseVtkImageData());
99  resampler->SetAxisOutputSpacing(0, spacing[0]);
100  resampler->SetAxisOutputSpacing(1, spacing[1]);
101  resampler->SetAxisOutputSpacing(2, spacing[2]);
102  resampler->Update();
103  vtkImageDataPtr rawResult = resampler->GetOutput();
104 
105  if (uid.isEmpty())
106  {
107  uid = image->getUid() + "_res%1";
108  name = image->getName()+" res%1";
109  }
110 
111  ImagePtr retval = createDerivedImage(dataManager,
112  uid, name,
113  rawResult, image);
114  return retval;
115 }
116 
121 {
122  Vector3D spacing(image->getBaseVtkImageData()->GetSpacing());
123  return resampleImage(dataManager, image, spacing, image->getUid()+"_copy%1", image->getName()+" copy%1");
124 }
125 
130 {
131  vtkImageClipPtr clip = vtkImageClipPtr::New();
132  clip->SetInputData(input);
133  clip->SetOutputWholeExtent(cropbox.begin());
134  clip->ClipDataOn();
135  clip->Update();
136  vtkImageDataPtr rawResult = clip->GetOutput();
137 
138 // rawResult->Update();
139 // rawResult->UpdateInformation();
140  rawResult->ComputeBounds();
141  return rawResult;
142 }
143 
148 {
149  DoubleBoundingBox3D bb = image->getCroppingBox();
150  double* sp = image->getBaseVtkImageData()->GetSpacing();
151  IntBoundingBox3D cropbox(
152  static_cast<int>(bb[0]/sp[0]+0.5), static_cast<int>(bb[1]/sp[0]+0.5),
153  static_cast<int>(bb[2]/sp[1]+0.5), static_cast<int>(bb[3]/sp[1]+0.5),
154  static_cast<int>(bb[4]/sp[2]+0.5), static_cast<int>(bb[5]/sp[2]+0.5));
155  vtkImageDataPtr rawResult = cropImage(image->getBaseVtkImageData(), cropbox);
156 
157  QString uid = image->getUid() + "_crop%1";
158  QString name = image->getName()+" crop%1";
159  ImagePtr result = createDerivedImage(dataManager,
160  uid, name,
161  rawResult, image);
162  result->mergevtkSettingsIntosscTransform();
163 
164  return result;
165 }
166 
169 QDateTime extractTimestamp(QString text)
170 {
171  // retrieve timestamp as
172  QRegExp tsReg("[0-9]{8}T[0-9]{6}");
173  if (tsReg.indexIn(text)>0)
174  {
175  QDateTime datetime = QDateTime::fromString(tsReg.cap(0), timestampSecondsFormat());
176  return datetime;
177  }
178  return QDateTime();
179 }
180 
181 
182 
183 
184 } // 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