Fraxinus  18.10
An IGT application
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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "cxImageAlgorithms.h"
13 
14 #include <vtkImageData.h>
15 #include <vtkImageReslice.h>
16 #include <vtkMatrix4x4.h>
17 
18 #include <vtkImageResample.h>
19 #include <vtkImageClip.h>
20 
21 #include "cxImage.h"
22 #include "cxPatientModelService.h"
23 #include "cxUtilHelpers.h"
24 #include "cxImageTF3D.h"
25 #include "cxImageLUT2D.h"
27 
28 #include "cxTime.h"
29 #include "cxVolumeHelpers.h"
30 
31 
32 namespace cx
33 {
34 
39 {
40  //TODO: fix error:
41  // There is an error in the transfer functions of the returned image from this function
42 
43  // provide a resampled volume for algorithms requiring that (such as PickerRep)
44  vtkMatrix4x4Ptr orientatorMatrix = vtkMatrix4x4Ptr::New();
45  vtkImageReslicePtr orientator = vtkImageReslicePtr::New();
46  orientator->SetInputData(image->getBaseVtkImageData());
47  orientator->SetInterpolationModeToLinear();
48  orientator->SetOutputDimensionality(3);
49  orientator->SetResliceAxes(qMd.inv().getVtkMatrix());
50  orientator->AutoCropOutputOn();
51  orientator->Update();
52  vtkImageDataPtr rawResult = orientator->GetOutput();
53 
54 // rawResult->Update();
55 
56  QString uid = image->getUid() + "_or%1";
57  QString name = image->getName()+" or%1";
58 // ImagePtr oriented = dataManager->createDerivedImage(rawResult, uid, name, image);
59 
60  ImagePtr oriented = createDerivedImage(dataManager,
61  uid, name,
62  rawResult, image);
63 
64  oriented->get_rMd_History()->setRegistration(image->get_rMd() * qMd.inv());
65  oriented->mergevtkSettingsIntosscTransform();
66 
67  return oriented;
68 }
69 
73 ImagePtr resampleImage(PatientModelServicePtr dataManager, ImagePtr image, const Vector3D spacing, QString uid, QString name)
74 {
75  vtkImageResamplePtr resampler = vtkImageResamplePtr::New();
76  resampler->SetInputData(image->getBaseVtkImageData());
77  resampler->SetAxisOutputSpacing(0, spacing[0]);
78  resampler->SetAxisOutputSpacing(1, spacing[1]);
79  resampler->SetAxisOutputSpacing(2, spacing[2]);
80  resampler->Update();
81  vtkImageDataPtr rawResult = resampler->GetOutput();
82 
83  if (uid.isEmpty())
84  {
85  uid = image->getUid() + "_res%1";
86  name = image->getName()+" res%1";
87  }
88 
89  ImagePtr retval = createDerivedImage(dataManager,
90  uid, name,
91  rawResult, image);
92  return retval;
93 }
94 
99 {
100  Vector3D spacing(image->getBaseVtkImageData()->GetSpacing());
101  return resampleImage(dataManager, image, spacing, image->getUid()+"_copy%1", image->getName()+" copy%1");
102 }
103 
108 {
109  vtkImageClipPtr clip = vtkImageClipPtr::New();
110  clip->SetInputData(input);
111  clip->SetOutputWholeExtent(cropbox.begin());
112  clip->ClipDataOn();
113  clip->Update();
114  vtkImageDataPtr rawResult = clip->GetOutput();
115 
116 // rawResult->Update();
117 // rawResult->UpdateInformation();
118  rawResult->ComputeBounds();
119  return rawResult;
120 }
121 
126 {
127  DoubleBoundingBox3D bb = image->getCroppingBox();
128  double* sp = image->getBaseVtkImageData()->GetSpacing();
129  IntBoundingBox3D cropbox(
130  static_cast<int>(bb[0]/sp[0]+0.5), static_cast<int>(bb[1]/sp[0]+0.5),
131  static_cast<int>(bb[2]/sp[1]+0.5), static_cast<int>(bb[3]/sp[1]+0.5),
132  static_cast<int>(bb[4]/sp[2]+0.5), static_cast<int>(bb[5]/sp[2]+0.5));
133  vtkImageDataPtr rawResult = cropImage(image->getBaseVtkImageData(), cropbox);
134 
135  QString uid = image->getUid() + "_crop%1";
136  QString name = image->getName()+" crop%1";
137  ImagePtr result = createDerivedImage(dataManager,
138  uid, name,
139  rawResult, image);
140  result->mergevtkSettingsIntosscTransform();
141 
142  return result;
143 }
144 
147 QDateTime extractTimestamp(QString text)
148 {
149  // retrieve timestamp as
150  QRegExp tsReg("[0-9]{8}T[0-9]{6}");
151  if (tsReg.indexIn(text)>0)
152  {
153  QDateTime datetime = QDateTime::fromString(tsReg.cap(0), timestampSecondsFormat());
154  return datetime;
155  }
156  return QDateTime();
157 }
158 
159 
160 
161 
162 } // namespace cx
vtkSmartPointer< class vtkMatrix4x4 > vtkMatrix4x4Ptr
Definition: cxMathBase.h:37
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:27
QString timestampSecondsFormat()
Definition: cxTime.cpp:18
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:42
QDateTime extractTimestamp(QString text)
vtkSmartPointer< class vtkImageData > vtkImageDataPtr
Namespace for all CustusX production code.