1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
19 #include "vvToolResample.h"
21 #include "vvToolCreator.h"
24 #include <QMessageBox>
26 #define COLUMN_IMAGE_NAME 7
28 ADD_TOOL(vvToolResample);
30 //------------------------------------------------------------------------------
31 vvToolResample::vvToolResample(vvMainWindowBase * parent, Qt::WindowFlags f):
32 vvToolWidgetBase(parent,f),
33 vvToolBase<vvToolResample>(parent),
36 Ui_vvToolResample::setupUi(mToolWidget);
38 mFilter = clitk::ImageResampleGenericFilter::New();
41 mInputFileFormat = "";
44 mCurrentSlicerManager=NULL;
47 mInputSpacing.resize(0);
48 mInputOrigin.resize(0);
49 mOutputSize.resize(0);
50 mOutputSpacing.resize(0);
52 xSizeLineEdit->setText("");
53 ySizeLineEdit->setText("");
54 zSizeLineEdit->setText("");
55 xSpacingLineEdit->setText("");
56 ySpacingLineEdit->setText("");
57 zSpacingLineEdit->setText("");
58 scaleSizeLineEdit->setText("");
59 scaleSpacingLineEdit->setText("");
61 sizeRadioButton->setChecked(0);
62 scaleSizeRadioButton->setChecked(0);
63 isoSizeRadioButton->setChecked(0);
64 spacingRadioButton->setChecked(0);
65 scaleSpacingRadioButton->setChecked(0);
66 isoSpacingRadioButton->setChecked(0);
68 gaussianFilterCheckBox->setCheckState(Qt::Unchecked);
70 xGaussianLineEdit->hide();
71 yGaussianLineEdit->hide();
72 zGaussianLineEdit->hide();
73 gaussianFilterLabel->hide();
76 bSplineOrderSpinBox->hide();
77 bLUTFactorLabel->hide();
82 qPalette.setColor(QPalette::Foreground, QColor(Qt::blue));
83 mInputFormatLabel->setPalette(qPalette);
84 mInputDimLabel->setPalette(qPalette);
85 mInputPixelTypeLabel->setPalette(qPalette);
86 mInputSizeLabel->setPalette(qPalette);
87 mInputSpacingLabel->setPalette(qPalette);
89 // Set how many inputs are needed for this tool
90 AddInputSelector("Select an image to resample", mFilter);
92 // Connect signals & slots
94 connect(sizeRadioButton, SIGNAL(clicked()), this, SLOT(UpdateControlSizeAndSpacing()));
95 connect(scaleSizeRadioButton, SIGNAL(clicked()), this, SLOT(UpdateControlSizeAndSpacing()));
96 connect(isoSizeRadioButton, SIGNAL(clicked()), this, SLOT(UpdateControlSizeAndSpacing()));
97 connect(spacingRadioButton, SIGNAL(clicked()), this, SLOT(UpdateControlSizeAndSpacing()));
98 connect(scaleSpacingRadioButton, SIGNAL(clicked()), this, SLOT(UpdateControlSizeAndSpacing()));
99 connect(isoSpacingRadioButton, SIGNAL(clicked()), this, SLOT(UpdateControlSizeAndSpacing()));
101 connect(xSizeLineEdit, SIGNAL(textEdited(QString)),this,SLOT(ComputeNewSpacingFromSize()));
102 connect(ySizeLineEdit, SIGNAL(textEdited(QString)),this,SLOT(ComputeNewSpacingFromSize()));
103 connect(zSizeLineEdit, SIGNAL(textEdited(QString)),this,SLOT(ComputeNewSpacingFromSize()));
104 connect(xSpacingLineEdit, SIGNAL(textEdited(QString)),this,SLOT(ComputeNewSizeFromSpacing()));
105 connect(ySpacingLineEdit, SIGNAL(textEdited(QString)),this,SLOT(ComputeNewSizeFromSpacing()));
106 connect(zSpacingLineEdit, SIGNAL(textEdited(QString)),this,SLOT(ComputeNewSizeFromSpacing()));
107 connect(scaleSizeLineEdit,SIGNAL(textEdited(QString)),this,SLOT(ComputeNewSizeFromScale()));
108 connect(scaleSpacingLineEdit,SIGNAL(textEdited(QString)),this,SLOT(ComputeNewSpacingFromScale()));
109 connect(isoSizeLineEdit,SIGNAL(textEdited(QString)),this,SLOT(ComputeNewSizeFromIso()));
110 connect(isoSpacingLineEdit,SIGNAL(textEdited(QString)),this,SLOT(ComputeNewSpacingFromIso()));
112 connect(gaussianFilterCheckBox,SIGNAL(stateChanged(int)),this,SLOT(UpdateGaussianFilter()));
113 connect(interpolationComboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(UpdateInterpolation()));
115 //------------------------------------------------------------------------------
118 //------------------------------------------------------------------------------
119 void vvToolResample::Initialize()
121 SetToolName("Resample Image");
122 SetToolMenuName("Resample");
123 SetToolIconFilename(":/common/icons/resample.png");
124 SetToolTip("Resample image with various interpolation methods.");
126 //------------------------------------------------------------------------------
129 //------------------------------------------------------------------------------
130 void vvToolResample::InputIsSelected(vvSlicerManager* m)
133 mCurrentSlicerManager = m;
134 mCurrentImage = mCurrentSlicerManager->GetSlicer(0)->GetImage();
135 if (mCurrentImage.IsNull()) return;
136 mInputFileName = mCurrentSlicerManager->GetFileName().c_str();
138 // Set current information
139 mPixelType = mCurrentImage->GetScalarTypeAsITKString().c_str();
140 //ds ComponentType = mCurrentImageGetNumberOfScalarComponents();
141 mDimension = mCurrentImage->GetNumberOfDimensions();
143 // Copy size, spacing ...
144 mInputOrigin.resize(mDimension);
145 mInputSpacing.resize(mDimension);
146 mInputSize.resize(mDimension);
147 for (int i = 0; i < mDimension; i++) {
148 mInputOrigin[i] = mCurrentImage->GetOrigin()[i];
149 mInputSpacing[i] = mCurrentImage->GetSpacing()[i];
150 mInputSize[i] = mCurrentImage->GetSize()[i];
154 mInputFileFormat = itksys::SystemTools::GetFilenameLastExtension(mInputFileName.toStdString()).c_str();
157 mInputFormatLabel->setText(mInputFileFormat);
158 mInputSizeLabel->setText(GetVectorIntAsString(mInputSize));
159 mInputDimLabel->setText(QString::number(mDimension)+"D");
160 mInputSpacingLabel->setText(GetVectorDoubleAsString(mInputSpacing));
161 mInputPixelTypeLabel->setText(mPixelType);
162 mInputMemoryLabel->setText(GetSizeInBytes(mInputSize));
165 scaleSizeRadioButton->setChecked(true);
166 UpdateControlSizeAndSpacing();
167 scaleSizeLineEdit->setText("100");
168 ComputeNewSizeFromScale();
173 //------------------------------------------------------------------------------
176 //------------------------------------------------------------------------------
177 void vvToolResample::UpdateOutputInfo()
179 mOutputSizeLabel->setText(GetVectorIntAsString(mOutputSize));
180 mOutputSpacingLabel->setText(GetVectorDoubleAsString(mOutputSpacing));
181 mOutputMemoryLabel->setText(GetSizeInBytes(mOutputSize));
183 //------------------------------------------------------------------------------
186 //------------------------------------------------------------------------------
187 QString vvToolResample::GetSizeInBytes(std::vector<int> & size)
190 for (unsigned int i=0; i<size.size(); i++) t *= size[i];
191 t *= mCurrentImage->GetScalarSize()*mCurrentImage->GetNumberOfScalarComponents();
192 QString result = QString::number(t);
193 result += " bytes (";
194 if (t > 1000000000) {
196 result += QString::number(t);
198 } else if (t > 1000000) {
200 result += QString::number(t);
202 } else if (t > 1000) {
204 result += QString::number(t);
206 } else result += ")";
209 //------------------------------------------------------------------------------
212 //------------------------------------------------------------------------------
213 QString vvToolResample::GetVectorDoubleAsString(std::vector<double> vectorDouble)
216 for (unsigned int i= 0; i<vectorDouble.size(); i++) {
219 result += QString::number(vectorDouble[i]);
223 //------------------------------------------------------------------------------
226 //------------------------------------------------------------------------------
227 QString vvToolResample::GetVectorIntAsString(std::vector<int> vectorInt)
230 for (unsigned int i= 0; i<vectorInt.size(); i++) {
233 result += QString::number(vectorInt[i]);
237 //------------------------------------------------------------------------------
240 //------------------------------------------------------------------------------
241 void vvToolResample::FillSizeEdit(std::vector<int> size)
243 xSizeLineEdit->setText(QString::number(size[0]));
244 ySizeLineEdit->setText(QString::number(size[1]));
246 zSizeLineEdit->setText(QString::number(size[2]));
248 //------------------------------------------------------------------------------
251 //------------------------------------------------------------------------------
252 void vvToolResample::FillSpacingEdit(std::vector<double> spacing)
254 xSpacingLineEdit->setText(QString::number(spacing[0]));
255 ySpacingLineEdit->setText(QString::number(spacing[1]));
256 if (spacing.size() > 2)
257 zSpacingLineEdit->setText(QString::number(spacing[2]));
259 //------------------------------------------------------------------------------
262 //------------------------------------------------------------------------------
263 void vvToolResample::UpdateOutputSizeAndSpacing()
265 mOutputSize.resize(mDimension);
266 mOutputSize = mInputSize;
267 mOutputSpacing.resize(mDimension);
268 mOutputSpacing = mInputSpacing;
269 mOutputSize[0] = (int)xSizeLineEdit->text().toDouble();
270 mOutputSize[1] = (int)ySizeLineEdit->text().toDouble();
272 mOutputSize[2] = (int)zSizeLineEdit->text().toDouble();
274 mOutputSpacing[0] = xSpacingLineEdit->text().toDouble();
275 mOutputSpacing[1] = ySpacingLineEdit->text().toDouble();
277 mOutputSpacing[2] = zSpacingLineEdit->text().toDouble();
281 //------------------------------------------------------------------------------
284 //------------------------------------------------------------------------------
285 void vvToolResample::UpdateControlSizeAndSpacing()
287 scaleSizeLineEdit->setText("");
288 scaleSpacingLineEdit->setText("");
289 isoSizeLineEdit->setText("");
290 isoSpacingLineEdit->setText("");
292 xSizeLineEdit->setReadOnly(1);
293 ySizeLineEdit->setReadOnly(1);
294 zSizeLineEdit->setReadOnly(1);
295 scaleSizeLineEdit->setReadOnly(1);
296 isoSizeLineEdit->setReadOnly(1);
298 xSpacingLineEdit->setReadOnly(1);
299 ySpacingLineEdit->setReadOnly(1);
300 zSpacingLineEdit->setReadOnly(1);
301 scaleSpacingLineEdit->setReadOnly(1);
302 isoSpacingLineEdit->setReadOnly(1);
304 if (sizeRadioButton->isChecked()) {
305 xSizeLineEdit->setReadOnly(0);
306 ySizeLineEdit->setReadOnly(0);
308 zSizeLineEdit->setReadOnly(0);
310 if (spacingRadioButton->isChecked()) {
311 xSpacingLineEdit->setReadOnly(0);
312 ySpacingLineEdit->setReadOnly(0);
314 zSpacingLineEdit->setReadOnly(0);
315 } else if (scaleSizeRadioButton->isChecked())
316 scaleSizeLineEdit->setReadOnly(0);
317 else if (scaleSpacingRadioButton->isChecked())
318 scaleSpacingLineEdit->setReadOnly(0);
319 else if (isoSizeRadioButton->isChecked())
320 isoSizeLineEdit->setReadOnly(0);
321 else if (isoSpacingRadioButton->isChecked())
322 isoSpacingLineEdit->setReadOnly(0);
325 //------------------------------------------------------------------------------
328 //------------------------------------------------------------------------------
329 void vvToolResample::ComputeNewSpacingFromSize()
331 double newSpacing = mInputSpacing[0]*mInputSize[0];
332 xSpacingLineEdit->setText(QString::number(newSpacing/xSizeLineEdit->text().toDouble()));
333 newSpacing = mInputSpacing[1]*mInputSize[1];
334 ySpacingLineEdit->setText(QString::number(newSpacing/ySizeLineEdit->text().toDouble()));
335 if (mDimension > 2) {
336 newSpacing = mInputSpacing[2]*mInputSize[2];
337 zSpacingLineEdit->setText(QString::number(newSpacing/zSizeLineEdit->text().toDouble()));
339 UpdateOutputSizeAndSpacing();
341 //------------------------------------------------------------------------------
344 //------------------------------------------------------------------------------
345 void vvToolResample::ComputeNewSizeFromSpacing()
347 double newSize = mInputSpacing[0]*mInputSize[0];
348 xSizeLineEdit->setText(QString::number(newSize/xSpacingLineEdit->text().toDouble()));
349 newSize = mInputSpacing[1]*mInputSize[1];
350 ySizeLineEdit->setText(QString::number(newSize/ySpacingLineEdit->text().toDouble()));
351 if (mDimension > 2) {
352 newSize = mInputSpacing[2]*mInputSize[2];
353 zSizeLineEdit->setText(QString::number(newSize/zSpacingLineEdit->text().toDouble()));
355 UpdateOutputSizeAndSpacing();
357 //------------------------------------------------------------------------------
360 //------------------------------------------------------------------------------
361 void vvToolResample::ComputeNewSpacingFromScale()
363 xSpacingLineEdit->setText(QString::number(mInputSpacing[0]*scaleSpacingLineEdit->text().toDouble()/100));
364 ySpacingLineEdit->setText(QString::number(mInputSpacing[1]*scaleSpacingLineEdit->text().toDouble()/100));
366 zSpacingLineEdit->setText(QString::number(mInputSpacing[2]*scaleSpacingLineEdit->text().toDouble()/100));
367 ComputeNewSizeFromSpacing();
369 //------------------------------------------------------------------------------
372 //------------------------------------------------------------------------------
373 void vvToolResample::ComputeNewSizeFromScale()
375 xSizeLineEdit->setText(QString::number(mInputSize[0]*scaleSizeLineEdit->text().toDouble()/100));
376 ySizeLineEdit->setText(QString::number(mInputSize[1]*scaleSizeLineEdit->text().toDouble()/100));
378 zSizeLineEdit->setText(QString::number(mInputSize[2]*scaleSizeLineEdit->text().toDouble()/100));
379 ComputeNewSpacingFromSize();
381 //------------------------------------------------------------------------------
384 //------------------------------------------------------------------------------
385 void vvToolResample::ComputeNewSpacingFromIso()
387 xSpacingLineEdit->setText(QString::number(isoSpacingLineEdit->text().toDouble()));
388 ySpacingLineEdit->setText(QString::number(isoSpacingLineEdit->text().toDouble()));
390 zSpacingLineEdit->setText(QString::number(isoSpacingLineEdit->text().toDouble()));
391 ComputeNewSizeFromSpacing();
393 //------------------------------------------------------------------------------
396 //------------------------------------------------------------------------------
397 void vvToolResample::ComputeNewSizeFromIso()
399 xSizeLineEdit->setText(QString::number(isoSizeLineEdit->text().toDouble()));
400 ySizeLineEdit->setText(QString::number(isoSizeLineEdit->text().toDouble()));
402 zSizeLineEdit->setText(QString::number(isoSizeLineEdit->text().toDouble()));
403 ComputeNewSpacingFromSize();
405 //------------------------------------------------------------------------------
408 //------------------------------------------------------------------------------
409 void vvToolResample::UpdateInterpolation()
411 if (interpolationComboBox->currentText() == "BSpline") {
412 bSplineLabel->show();
413 bSplineOrderSpinBox->show();
414 bLUTFactorLabel->hide();
416 } else if (interpolationComboBox->currentText() == "Blut (faster BSpline)") {
417 bSplineLabel->show();
418 bSplineOrderSpinBox->show();
419 bLUTFactorLabel->show();
422 bSplineLabel->hide();
423 bSplineOrderSpinBox->hide();
424 bLUTFactorLabel->hide();
428 //------------------------------------------------------------------------------
431 //------------------------------------------------------------------------------
432 void vvToolResample::UpdateGaussianFilter()
434 if (gaussianFilterCheckBox->isChecked()) {
435 gaussianFilterLabel->show();
436 xGaussianLineEdit->show();
437 yGaussianLineEdit->show();
439 zGaussianLineEdit->show();
441 gaussianFilterLabel->hide();
442 xGaussianLineEdit->hide();
443 yGaussianLineEdit->hide();
444 zGaussianLineEdit->hide();
447 //------------------------------------------------------------------------------
450 //------------------------------------------------------------------------------
451 void vvToolResample::apply()
454 // Get resampler options
455 std::vector<double> sigma;
456 sigma.push_back(xGaussianLineEdit->text().toDouble());
457 sigma.push_back(yGaussianLineEdit->text().toDouble());
458 if (mDimension > 2) sigma.push_back(zGaussianLineEdit->text().toDouble());
459 if (mDimension == 4) sigma.push_back(0.01); //FIXME Don't filter along the temporal direction
461 mFilter->SetOutputSize(mOutputSize);
462 mFilter->SetOutputSpacing(mOutputSpacing);
463 mFilter->SetInterpolationName(interpolationComboBox->currentText().toLower().toStdString());
465 if (interpolationComboBox->currentText() == "BSpline")
466 mFilter->SetBSplineOrder(bSplineOrderSpinBox->value());
467 else if (interpolationComboBox->currentText() == "Blut (faster BSpline)") {
468 mFilter->SetInterpolationName("blut");
469 mFilter->SetBSplineOrder(bSplineOrderSpinBox->value());
470 mFilter->SetBLUTSampling(bLUTSpinBox->value());
472 if (gaussianFilterCheckBox->isChecked())
473 mFilter->SetGaussianSigma(sigma);
474 // mFilter->SetOutputFileName(OutputFileName.toStdString());
475 mFilter->SetDefaultPixelValue(defaultPixelValueLineEdit->text().toDouble());
476 mFilter->SetInputVVImage(mCurrentImage);
480 mOutput = mFilter->GetOutputVVImage();
481 AddImage(mOutput,GetOutputFileName());
484 //------------------------------------------------------------------------------
487 //------------------------------------------------------------------------------
488 std::string vvToolResample::GetOutputFileName()
490 QFileInfo info(QString(mCurrentSlicerManager->GetFileName().c_str()));
491 return (info.path().toStdString() + "/resampled_" + info.fileName().toStdString());
493 //------------------------------------------------------------------------------