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 ======================================================================-====*/
20 #include "vvToolCropImage.h"
24 #include "clitkCropImage_ggo.h"
25 #include "clitkCropImageGenericFilter.h"
26 #include "clitkExceptionObject.h"
31 #include <QApplication>
32 #include <QMessageBox>
35 #include <vtkImageClip.h>
36 #include <vtkImageTranslateExtent.h>
37 #include <vtkImageData.h>
38 #include <vtkSmartPointer.h>
40 //------------------------------------------------------------------------------
41 // Create the tool and automagically (I like this word) insert it in
42 // the main window menu.
43 ADD_TOOL(vvToolCropImage);
44 //------------------------------------------------------------------------------
47 //------------------------------------------------------------------------------
48 vvToolCropImage::vvToolCropImage(vvMainWindowBase * parent, Qt::WindowFlags f):
49 vvToolWidgetBase(parent, f),
50 vvToolBase<vvToolCropImage>(parent),
55 Ui_vvToolCropImage::setupUi(mToolWidget);
56 // mTLabel2->setHidden(false);
57 // tminSlider->setHidden(false);
58 // tmaxSlider->setHidden(false);
59 // spin_tmin->setHidden(false);
60 // spin_tmax->setHidden(false);
61 // mLabelTimeCropping->setHidden(false);
62 mTLabel1->setHidden(true);
63 mTLabel2->setHidden(true);
64 tminSlider->setHidden(true);
65 tmaxSlider->setHidden(true);
66 spin_tmin->setHidden(true);
67 spin_tmax->setHidden(true);
68 mLabelTimeCropping->setHidden(true);
70 // Set how many inputs are needed for this tool
71 AddInputSelector("Select one image");
73 //------------------------------------------------------------------------------
76 //------------------------------------------------------------------------------
77 vvToolCropImage::~vvToolCropImage()
81 //------------------------------------------------------------------------------
84 //------------------------------------------------------------------------------
85 void vvToolCropImage::closeEvent(QCloseEvent *event)
88 for(int i=0; i<mExtentSize; i++) mReducedExtent[i] = mInitialExtent[i];
92 //------------------------------------------------------------------------------
95 //------------------------------------------------------------------------------
96 bool vvToolCropImage::close()
98 return vvToolWidgetBase::close();
100 //------------------------------------------------------------------------------
103 //------------------------------------------------------------------------------
104 void vvToolCropImage::reject()
106 for(int i=0; i<mExtentSize; i++) mReducedExtent[i] = mInitialExtent[i];
108 return vvToolWidgetBase::reject();
110 //------------------------------------------------------------------------------
113 //------------------------------------------------------------------------------
114 void vvToolCropImage::sliderXMinValueChanged(int s)
116 xmaxSlider->setMinimum(xminSlider->value());
117 mReducedExtent[0] = xminSlider->value();
120 //------------------------------------------------------------------------------
123 //------------------------------------------------------------------------------
124 void vvToolCropImage::sliderXMaxValueChanged(int s)
126 xminSlider->setMaximum(xmaxSlider->value());
127 mReducedExtent[1] = xmaxSlider->value();
130 //------------------------------------------------------------------------------
133 //------------------------------------------------------------------------------
134 void vvToolCropImage::sliderYMinValueChanged(int s)
136 ymaxSlider->setMinimum(yminSlider->value());
137 mReducedExtent[2] = yminSlider->value();
140 //------------------------------------------------------------------------------
143 //------------------------------------------------------------------------------
144 void vvToolCropImage::sliderYMaxValueChanged(int s)
146 yminSlider->setMaximum(ymaxSlider->value());
147 mReducedExtent[3] = ymaxSlider->value();
150 //------------------------------------------------------------------------------
153 //------------------------------------------------------------------------------
154 void vvToolCropImage::sliderZMinValueChanged(int s)
156 zmaxSlider->setMinimum(zminSlider->value());
157 mReducedExtent[4] = zminSlider->value();
160 //------------------------------------------------------------------------------
163 //------------------------------------------------------------------------------
164 void vvToolCropImage::sliderZMaxValueChanged(int s)
166 zminSlider->setMaximum(zmaxSlider->value());
167 mReducedExtent[5] = zmaxSlider->value();
170 //------------------------------------------------------------------------------
173 //------------------------------------------------------------------------------
174 void vvToolCropImage::sliderTMinValueChanged(int s) {
175 tmaxSlider->setMinimum(tminSlider->value());
176 mReducedExtent[6] = tminSlider->value();
179 //------------------------------------------------------------------------------
182 //------------------------------------------------------------------------------
183 void vvToolCropImage::sliderTMaxValueChanged(int s) {
184 tminSlider->setMaximum(tmaxSlider->value());
185 mReducedExtent[7] = tmaxSlider->value();
188 //------------------------------------------------------------------------------
191 //------------------------------------------------------------------------------
192 void vvToolCropImage::UpdateExtent()
194 for(int i=0; i<mCurrentSlicerManager->GetNumberOfSlicers(); i++) {
195 mCurrentSlicerManager->GetSlicer(i)->SetReducedExtent(mReducedExtent);
196 mCurrentSlicerManager->GetSlicer(i)->ForceUpdateDisplayExtent();
197 mCurrentSlicerManager->GetSlicer(i)->Render();
200 //------------------------------------------------------------------------------
203 //------------------------------------------------------------------------------
204 void vvToolCropImage::InputIsSelected(vvSlicerManager * slicer)
207 // Change interface according to number of dimension
208 mExtentSize = 2*slicer->GetDimension();
209 if (slicer->GetDimension()<4) {
210 mTLabel1->setHidden(true);
211 mTLabel2->setHidden(true);
212 tminSlider->setHidden(true);
213 tmaxSlider->setHidden(true);
214 spin_tmin->setHidden(true);
215 spin_tmax->setHidden(true);
216 mLabelTimeCropping->setHidden(true);
218 if (slicer->GetDimension()<3) {
219 mZLabel1->setHidden(true);
220 mZLabel2->setHidden(true);
221 zminSlider->setHidden(true);
222 zmaxSlider->setHidden(true);
223 spin_zmin->setHidden(true);
224 spin_zmax->setHidden(true);
227 // Record initial extend
228 mReducedExtent = new int[mExtentSize];
229 mInitialExtent = new int[mExtentSize];
230 mReducedExtent = mCurrentSlicerManager->GetImage()->GetFirstVTKImageData()->GetWholeExtent();
231 for(int i=0; i<mExtentSize; i++) mInitialExtent[i] = mReducedExtent[i];
232 for(int i=0; i<mCurrentSlicerManager->GetNumberOfSlicers(); i++) {
233 mCurrentSlicerManager->GetSlicer(i)->EnableReducedExtent(true);
236 // Set initial sliders values
237 std::vector<int> imsize = mCurrentSlicerManager->GetImage()->GetSize();
238 xminSlider->setMaximum(imsize[0]-1);
239 xmaxSlider->setMaximum(imsize[0]-1);
240 xmaxSlider->setValue(imsize[0]-1);
242 yminSlider->setMaximum(imsize[1]-1);
243 ymaxSlider->setMaximum(imsize[1]-1);
244 ymaxSlider->setValue(imsize[1]-1);
246 if (slicer->GetDimension() >2) {
247 zminSlider->setMaximum(imsize[2]-1);
248 zmaxSlider->setMaximum(imsize[2]-1);
249 zmaxSlider->setValue(imsize[2]-1);
252 if (slicer->GetDimension() >3) {
253 tminSlider->setMaximum(imsize[3]-1);
254 tmaxSlider->setMaximum(imsize[3]-1);
255 tmaxSlider->setValue(imsize[3]-1);
258 spin_xmin->setMaximum(imsize[0]-1);
259 spin_xmax->setMaximum(imsize[0]-1);
260 spin_xmax->setValue(imsize[0]-1);
262 spin_ymin->setMaximum(imsize[1]-1);
263 spin_ymax->setMaximum(imsize[1]-1);
264 spin_ymax->setValue(imsize[1]-1);
266 if (slicer->GetDimension() >2) {
267 spin_zmin->setMaximum(imsize[2]-1);
268 spin_zmax->setMaximum(imsize[2]-1);
269 spin_zmax->setValue(imsize[2]-1);
272 if (slicer->GetDimension() >3) {
273 spin_tmin->setMaximum(imsize[3]-1);
274 spin_tmax->setMaximum(imsize[3]-1);
275 spin_tmax->setValue(imsize[3]-1);
279 connect(xminSlider, SIGNAL(valueChanged(int)), this, SLOT(sliderXMinValueChanged(int)));
280 connect(xmaxSlider, SIGNAL(valueChanged(int)), this, SLOT(sliderXMaxValueChanged(int)));
281 connect(yminSlider, SIGNAL(valueChanged(int)), this, SLOT(sliderYMinValueChanged(int)));
282 connect(ymaxSlider, SIGNAL(valueChanged(int)), this, SLOT(sliderYMaxValueChanged(int)));
283 connect(zminSlider, SIGNAL(valueChanged(int)), this, SLOT(sliderZMinValueChanged(int)));
284 connect(zmaxSlider, SIGNAL(valueChanged(int)), this, SLOT(sliderZMaxValueChanged(int)));
285 connect(tminSlider, SIGNAL(valueChanged(int)), this, SLOT(sliderTMinValueChanged(int)));
286 connect(tmaxSlider, SIGNAL(valueChanged(int)), this, SLOT(sliderTMaxValueChanged(int)));
288 // connect(mCurrentSlicerManager,SIGNAL(UpdateSlice(int,int)),this,SLOT(UpdateExtent()));
289 //connect(mCurrentSlicerManager,SIGNAL(UpdateTSlice(int,int)),this,SLOT(UpdateExtent()));
291 // connect(mIntensitySlider, SIGNAL(valueChanged(double)), this, SLOT(autoCropValueChanged(double)));
294 //------------------------------------------------------------------------------
297 //------------------------------------------------------------------------------
298 void vvToolCropImage::autoCropValueChanged(double v)
302 //------------------------------------------------------------------------------
305 //------------------------------------------------------------------------------
306 void vvToolCropImage::apply()
308 if (!mCurrentSlicerManager) close();
309 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
312 typedef args_info_clitkCropImage ArgsInfoType;
313 typedef clitk::CropImageGenericFilter CropFilterType;
316 ArgsInfoType mArgsInfo;
317 cmdline_parser_clitkCropImage_init(&mArgsInfo); // Initialisation to default
318 int n = mCurrentSlicerManager->GetDimension()*2; // 2D and 3D only
319 mArgsInfo.boundingBox_given = n;
320 mArgsInfo.boundingBox_arg = new int[n];
321 mArgsInfo.boundingBox_arg[0] = xminSlider->value();
322 mArgsInfo.boundingBox_arg[1] = xmaxSlider->value();
323 mArgsInfo.boundingBox_arg[2] = yminSlider->value();
324 mArgsInfo.boundingBox_arg[3] = ymaxSlider->value();
326 mArgsInfo.boundingBox_arg[4] = zminSlider->value();
327 mArgsInfo.boundingBox_arg[5] = zmaxSlider->value();
331 // mArgsInfo.boundingBox_arg[6] = tminSlider->value();
332 // mArgsInfo.boundingBox_arg[7] = tmaxSlider->value();
333 mArgsInfo.boundingBox_arg[6] = 0;
334 mArgsInfo.boundingBox_arg[7] = mCurrentImage->GetSize()[3]-1;
337 // We MUST reset initial extend to input image before using the
338 // filter to retrieve the correct image size
339 for(int i=0; i<mExtentSize; i++) {
340 mReducedExtent[i] = mInitialExtent[i];
341 // DD(mArgsInfo.boundingBox_arg[i]);
346 CropFilterType::Pointer filter = CropFilterType::New();
347 filter->SetInputVVImage(mCurrentImage);
348 filter->SetArgsInfo(mArgsInfo);
350 // Go ! (not threaded)
354 catch(clitk::ExceptionObject & e) {
356 QApplication::restoreOverrideCursor();
360 // Retrieve result and display it
361 vvImage::Pointer output = filter->GetOutputVVImage();
362 std::ostringstream osstream;
363 osstream << "Croped_" << mCurrentSlicerManager->GetSlicer(0)->GetFileName() << ".mhd";
364 AddImage(output,osstream.str());
367 QApplication::restoreOverrideCursor();
371 // OLD approach with VTK
373 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
374 vvImage::Pointer mResult=vvImage::New();
375 vvImage::Pointer image= mCurrentSlicerManager->GetImage();
376 for (std::vector<vtkImageData*>::const_iterator i=image->GetVTKImages().begin();
377 i!=image->GetVTKImages().end(); i++) {
378 vtkSmartPointer<vtkImageClip> filter=vtkSmartPointer<vtkImageClip>::New();
379 ///Vtk is very weird, you need to "translate the extent" to get the correct origin
380 //http://markmail.org/message/vndc2tr6kcabiakp#query:vtkImageClip%20origin+page:1+mid:6na7y57floutklvz+state:results
381 vtkSmartPointer<vtkImageTranslateExtent> translate=vtkSmartPointer<vtkImageTranslateExtent>::New();
382 filter->SetInput(*i);
383 filter->SetOutputWholeExtent(xminSlider->value(),xmaxSlider->value(),
384 yminSlider->value(),ymaxSlider->value(),
385 zminSlider->value(),zmaxSlider->value());
386 translate->SetTranslation(-xminSlider->value(),-yminSlider->value(),-zminSlider->value());
387 translate->SetInput(filter->GetOutput());
388 filter->ClipDataOn(); //Really create a cropped copy of the image
390 vtkImageData* output=vtkImageData::New();
391 output->ShallowCopy(translate->GetOutput());
392 mResult->AddImage(output);
394 QApplication::restoreOverrideCursor();
395 std::ostringstream osstream;
396 osstream << "Crop_" << mCurrentSlicerManager->GetSlicer(0)->GetFileName() << ".mhd";
397 AddImage(mResult, osstream.str());
402 //------------------------------------------------------------------------------