CustusX  18.04
An IGT application
cxResampleImageFilter.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 "cxResampleImageFilter.h"
13 
14 #include <QApplication>
15 
16 #include "cxImageAlgorithms.h"
17 #include "cxImage.h"
18 #include "vtkImageData.h"
20 #include "cxDoubleProperty.h"
21 #include "cxPatientModelService.h"
22 #include "cxVisServices.h"
23 
24 namespace cx
25 {
26 
28  FilterImpl(services)
29 {
30 }
31 
33 {
34  return "Resample";
35 }
36 
38 {
39  return "resample_image_filter";
40 }
41 
43 {
44  return "<html>"
45  "<h3>Resample.</h3>"
46  "<p><i>Resample the volume into the space of the reference volume. Also crop to the same volume.</i></p>"
47  "</html>";
48 }
49 
51 {
52  return DoubleProperty::initialize("Margin", "",
53  "mm Margin added to ref image bounding box",
54  5.0, DoubleRange(0, 50, 1), 1, root);
55 }
56 
58 {
59  mOptionsAdapters.push_back(this->getMarginOption(mOptions));
60 }
61 
63 {
65 
66  temp = StringPropertySelectImage::New(mServices->patient());
67  temp->setValueName("Input");
68  temp->setHelp("Select input to be resampled");
69  mInputTypes.push_back(temp);
70 
71  temp = StringPropertySelectImage::New(mServices->patient());
72  temp->setValueName("Reference");
73  temp->setHelp("Select reference. Resample input into this coordinate system and bounding box");
74  mInputTypes.push_back(temp);
75 }
76 
78 {
80 
81  temp = StringPropertySelectData::New(mServices->patient());
82  temp->setValueName("Output");
83  temp->setHelp("Output thresholded binary image");
84  mOutputTypes.push_back(temp);
85 }
86 
87 //bool ResampleImageFilter::preProcess()
88 //{
89 // return FilterImpl::preProcess();
90 //}
91 
98 {
99  ImagePtr input = this->getCopiedInputImage(0);
100  ImagePtr reference = this->getCopiedInputImage(1);
101  if (!input || !reference)
102  return false;
103 
104  DoublePropertyPtr marginOption = this->getMarginOption(mCopiedOptions);
105  double margin = marginOption->getValue();
106 
107  Transform3D refMi = reference->get_rMd().inv() * input->get_rMd();
108  ImagePtr oriented = resampleImage(mServices->patient(), input, refMi);//There is an error with the transfer functions in this image
109 
110  Transform3D orient_M_ref = oriented->get_rMd().inv() * reference->get_rMd();
111  DoubleBoundingBox3D bb_crop = transform(orient_M_ref, reference->boundingBox());
112 
113  // increase bb size by margin
114  bb_crop[0] -= margin;
115  bb_crop[1] += margin;
116  bb_crop[2] -= margin;
117  bb_crop[3] += margin;
118  bb_crop[4] -= margin;
119  bb_crop[5] += margin;
120 
121  oriented->setCroppingBox(bb_crop);
122 
123  ImagePtr cropped = cropImage(mServices->patient(), oriented);
124 
125  QString uid = input->getUid() + "_resample%1";
126  QString name = input->getName() + " resample%1";
127 
128  ImagePtr resampled = resampleImage(mServices->patient(), cropped, Vector3D(reference->getBaseVtkImageData()->GetSpacing()), uid, name);
129 
130  // important! move thread affinity to main thread - ensures signals/slots is still called correctly
131  resampled->moveThisAndChildrenToThread(QApplication::instance()->thread());
132 
133  mRawResult = resampled;
134  return true;
135 }
136 
138 {
139  if (!mRawResult)
140  return false;
141 
142  ImagePtr output = mRawResult;
143  mRawResult.reset();
144  mServices->patient()->insertData(output);
145 
146  // set output
147  mOutputTypes.front()->setValue(output->getUid());
148  return true;
149 }
150 
151 
152 
153 } // namespace cx
154 
DoubleBoundingBox3D transform(const Transform3D &m, const DoubleBoundingBox3D &bb)
ImagePtr resampleImage(PatientModelServicePtr dataManager, ImagePtr image, Transform3D qMd)
std::vector< SelectDataStringPropertyBasePtr > mInputTypes
Definition: cxFilterImpl.h:73
QDomElement mCopiedOptions
Definition: cxFilterImpl.h:80
boost::shared_ptr< class VisServices > VisServicesPtr
Definition: cxMainWindow.h:40
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
static StringPropertySelectDataPtr New(PatientModelServicePtr patientModelService, QString typeRegexp=".*")
vtkImageDataPtr cropImage(vtkImageDataPtr input, IntBoundingBox3D cropbox)
Utility class for describing a bounded numeric range.
Definition: cxDoubleRange.h:32
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:27
DoublePropertyPtr getMarginOption(QDomElement root)
virtual QString getName() const
std::vector< PropertyPtr > mOptionsAdapters
Definition: cxFilterImpl.h:75
VisServicesPtr mServices
Definition: cxFilterImpl.h:82
boost::shared_ptr< class SelectDataStringPropertyBase > SelectDataStringPropertyBasePtr
virtual QString getType() const
ImagePtr getCopiedInputImage(int index=0)
QDomElement mOptions
Definition: cxFilterImpl.h:76
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.
virtual QString getHelp() const
boost::shared_ptr< class DoubleProperty > DoublePropertyPtr
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:42
std::vector< SelectDataStringPropertyBasePtr > mOutputTypes
Definition: cxFilterImpl.h:74
static DoublePropertyPtr initialize(const QString &uid, QString name, QString help, double value, DoubleRange range, int decimals, QDomNode root=QDomNode())
static StringPropertySelectImagePtr New(PatientModelServicePtr patientModelService)
ResampleImageFilter(VisServicesPtr services)
Namespace for all CustusX production code.