]> Creatis software - clitk.git/blob - vv/vvToolBinarize.cxx
Add clitkImage2DicomSeries tool to write a Dicom Series from an image without a corre...
[clitk.git] / vv / vvToolBinarize.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://www.centreleonberard.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
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.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17   ===========================================================================**/
18
19 // vv
20 #include "vvToolBinarize.h"
21 #include "vvSlicerManager.h"
22 #include "vvSlicer.h"
23 #include "vvToolInputSelectorWidget.h"
24
25 // clitk
26 #include "clitkBinarizeImageGenericFilter.h"
27
28 // vtk
29 #include <vtkImageActor.h>
30 #include <vtkCamera.h>
31 #include <vtkImageClip.h>
32 #include <vtkRenderWindow.h>
33
34
35 //------------------------------------------------------------------------------
36 // Create the tool and automagically (I like this word) insert it in
37 // the main window menu.
38 ADD_TOOL(vvToolBinarize);
39 //------------------------------------------------------------------------------
40
41
42 //------------------------------------------------------------------------------
43 void vvToolBinarize::Initialize()
44
45   SetToolName("Binarize");
46   SetToolMenuName("Binarize");
47   SetToolIconFilename(":/common/icons/binarize.png");
48   SetToolTip("Image interactive binarization with thresholds.");
49 }
50 //------------------------------------------------------------------------------
51
52
53 //------------------------------------------------------------------------------
54 vvToolBinarize::vvToolBinarize(vvMainWindowBase * parent, Qt::WindowFlags f)
55   :vvToolWidgetBase(parent,f),
56    vvToolBase<vvToolBinarize>(parent),
57    Ui::vvToolBinarize()
58
59   // GUI Initialization
60   Ui_vvToolBinarize::setupUi(mToolWidget);
61   mInteractiveDisplayIsEnabled = mCheckBoxInteractiveDisplay->isChecked();
62
63   // Connect signals & slots
64   connect(mRadioButtonLowerThan, SIGNAL(toggled(bool)), this, SLOT(enableLowerThan(bool)));
65   connect(mCheckBoxUseFG, SIGNAL(toggled(bool)), this, SLOT(useFGBGtoggled(bool)));
66   connect(mCheckBoxUseBG, SIGNAL(toggled(bool)), this, SLOT(useFGBGtoggled(bool)));
67   connect(mCheckBoxInteractiveDisplay, SIGNAL(toggled(bool)), this, SLOT(InteractiveDisplayToggled(bool)));
68
69   // Initialize some widget
70   mThresholdSlider1->SetText("");
71   mThresholdSlider2->SetText("");
72   mFGSlider->SetText("Foreground value");
73   mBGSlider->SetText("Background value");
74
75   // Main filter
76   mFilter = clitk::BinarizeImageGenericFilter::New();
77
78   // Set how many inputs are needed for this tool
79   AddInputSelector("Select one image", mFilter);
80 }
81 //------------------------------------------------------------------------------
82
83
84 //------------------------------------------------------------------------------
85 vvToolBinarize::~vvToolBinarize()
86
87 }
88 //------------------------------------------------------------------------------
89
90
91 //------------------------------------------------------------------------------
92 void vvToolBinarize::InteractiveDisplayToggled(bool b)
93
94   mInteractiveDisplayIsEnabled = b;
95   if (!mInteractiveDisplayIsEnabled) {
96     RemoveVTKObjects();
97   } else {
98     for(unsigned int i=0; i<mImageContour.size(); i++) {
99       mImageContour[i]->ShowActors();
100       if (mRadioButtonLowerThan->isChecked())
101         mImageContourLower[i]->ShowActors();
102     }
103     if (mCurrentSlicerManager)
104       mCurrentSlicerManager->Render();
105   }
106 }
107 //------------------------------------------------------------------------------
108
109
110 //------------------------------------------------------------------------------
111 void vvToolBinarize::RemoveVTKObjects()
112
113   for(unsigned int i=0; i<mImageContour.size(); i++) {
114     mImageContour[i]->HideActors();
115     mImageContourLower[i]->HideActors();    
116   }
117   if (mCurrentSlicerManager)
118     mCurrentSlicerManager->Render();
119 }
120 //------------------------------------------------------------------------------
121
122
123 //------------------------------------------------------------------------------
124 bool vvToolBinarize::close()
125
126   // RemoveVTKObjects();
127   return vvToolWidgetBase::close();
128 }
129 //------------------------------------------------------------------------------
130
131
132 //------------------------------------------------------------------------------
133 void vvToolBinarize::closeEvent(QCloseEvent *event)
134
135   RemoveVTKObjects();
136   event->accept();
137 }
138 //------------------------------------------------------------------------------
139
140
141 //------------------------------------------------------------------------------
142 void vvToolBinarize::reject()
143
144   // DD("vvToolBinarize::reject");
145   RemoveVTKObjects();
146   return vvToolWidgetBase::reject();
147 }
148 //------------------------------------------------------------------------------
149
150
151 //------------------------------------------------------------------------------
152 void vvToolBinarize::enableLowerThan(bool b)
153
154   if (!b) {
155     mThresholdSlider1->resetMaximum();
156     for(unsigned int i=0; i<mImageContour.size(); i++) {
157       mImageContourLower[i]->HideActors();    
158     }
159     mCurrentSlicerManager->Render();
160   } else {
161     valueChangedT1(mThresholdSlider1->GetValue());
162     valueChangedT2(mThresholdSlider2->GetValue());
163     for(unsigned int i=0; i<mImageContour.size(); i++) {
164       mImageContourLower[i]->ShowActors();    
165     }
166     mCurrentSlicerManager->Render();
167   }
168 }
169 //------------------------------------------------------------------------------
170
171
172 //------------------------------------------------------------------------------
173 void vvToolBinarize::useFGBGtoggled(bool)
174
175   if (!mCheckBoxUseBG->isChecked() && !mCheckBoxUseFG->isChecked())
176     mCheckBoxUseBG->toggle();
177 }
178 //------------------------------------------------------------------------------
179
180
181 //------------------------------------------------------------------------------
182 // void vvToolBinarize::InputIsSelected(std::vector<vvSlicerManager *> & m) {
183 //   DD("vvToolBinarize::InputIsSelected vector in binarize");
184 //   DD(m.size());
185 // }
186 void vvToolBinarize::InputIsSelected(vvSlicerManager * m)
187
188   mCurrentSlicerManager = m;
189
190   // Specific for this gui
191   mThresholdSlider1->SetValue(0);
192   mThresholdSlider2->SetValue(0);
193   mThresholdSlider1->SetImage(mCurrentImage);
194   mThresholdSlider2->SetImage(mCurrentImage);
195   mFGSlider->SetImage(mCurrentImage);
196   mBGSlider->SetImage(mCurrentImage);
197   //  DD(mCurrentSlicerManager->GetFileName().c_str());
198   //  mFGSlider->SetMaximum(mCurrentImage->GetFirstVTKImageData()->GetScalarTypeMax());
199   //   mFGSlider->SetMinimum(mCurrentImage->GetFirstVTKImageData()->GetScalarTypeMin());
200   //   mBGSlider->SetMaximum(mCurrentImage->GetFirstVTKImageData()->GetScalarTypeMax());
201   //   mBGSlider->SetMinimum(mCurrentImage->GetFirstVTKImageData()->GetScalarTypeMin());
202
203   // Output is uchar ...
204   mFGSlider->SetMaximum(255);
205   mFGSlider->SetMinimum(0);
206   mBGSlider->SetMaximum(255);
207   mBGSlider->SetMinimum(0);
208
209   mFGSlider->SetValue(1);
210   mBGSlider->SetValue(0);
211   mFGSlider->SetSingleStep(1);
212   mBGSlider->SetSingleStep(1);
213
214   // VTK objects for interactive display
215   for(int i=0; i<mCurrentSlicerManager->GetNumberOfSlicers(); i++) {
216     mImageContour.push_back(vvImageContour::New());
217     mImageContour[i]->SetSlicer(mCurrentSlicerManager->GetSlicer(i));
218     mImageContour[i]->SetColor(1.0, 0.0, 0.0);
219     mImageContour[i]->SetDepth(0); // to be in front of (whe used with ROI tool)
220     mImageContourLower.push_back(vvImageContour::New());
221     mImageContourLower[i]->SetSlicer(mCurrentSlicerManager->GetSlicer(i));
222     mImageContourLower[i]->SetColor(0.0, 0.0, 1.0);
223     mImageContourLower[i]->SetDepth(100); // to be in front of (whe used with ROI tool)
224   }
225   valueChangedT1(mThresholdSlider1->GetValue());
226
227   connect(mThresholdSlider1, SIGNAL(valueChanged(double)), this, SLOT(valueChangedT1(double)));
228   connect(mThresholdSlider2, SIGNAL(valueChanged(double)), this, SLOT(valueChangedT2(double)));
229
230   connect(mCurrentSlicerManager,SIGNAL(UpdateSlice(int,int)),this,SLOT(UpdateSlice(int, int)));
231   connect(mCurrentSlicerManager,SIGNAL(UpdateTSlice(int,int, int)),this,SLOT(UpdateSlice(int, int, int)));
232   
233   connect(mCurrentSlicerManager,SIGNAL(UpdateOrientation(int,int)),this,SLOT(UpdateOrientation(int, int)));
234
235   //  connect(mCurrentSlicerManager, SIGNAL(LeftButtonReleaseSignal(int)), SLOT(LeftButtonReleaseEvent(int)));
236   InteractiveDisplayToggled(mInteractiveDisplayIsEnabled);
237 }
238 //------------------------------------------------------------------------------
239
240
241 //------------------------------------------------------------------------------
242 // void vvToolBinarize::LeftButtonReleaseEvent(int slicer) {
243 //   DD("LeftButtonReleaseEvent");
244 //   for(int i=0; i<mCurrentSlicerManager->GetNumberOfSlicers(); i++) {
245 //     if (i == slicer);
246 //     mCurrentSlicerManager->GetSlicer(i)->GetRenderWindow()->Render();
247 //   }
248 // }
249 //------------------------------------------------------------------------------
250
251
252 //------------------------------------------------------------------------------
253 void vvToolBinarize::UpdateOrientation(int slicer,int orientation)
254
255   Update(slicer);
256 }
257 //------------------------------------------------------------------------------
258
259
260 //------------------------------------------------------------------------------
261 void vvToolBinarize::UpdateSlice(int slicer,int slices, int code)
262
263   Update(slicer);
264 }
265 //------------------------------------------------------------------------------
266
267 //------------------------------------------------------------------------------
268 void vvToolBinarize::Update(int slicer)
269
270   if (!mInteractiveDisplayIsEnabled) return;
271   if (!mCurrentSlicerManager) close();
272   mImageContour[slicer]->Update(mThresholdSlider1->GetValue());
273   if (mRadioButtonLowerThan->isChecked()) 
274     mImageContourLower[slicer]->Update(mThresholdSlider2->GetValue());
275 }
276 //------------------------------------------------------------------------------
277
278 //------------------------------------------------------------------------------
279 void vvToolBinarize::GetArgsInfoFromGUI()
280
281
282   /* //KEEP THIS FOR READING GGO FROM FILE
283      int argc=1;
284      std::string a = "toto";
285      char * const* argv = new char*;
286      //a.c_str();
287      struct cmdline_parser_params p;
288      p.check_required = 0;
289      int good = cmdline_parser_ext(argc, argv, &args_info, &p);
290      DD(good);
291   */
292   cmdline_parser_clitkBinarizeImage_init(&mArgsInfo); // Initialisation to default
293   bool inverseBGandFG = false;
294
295   mArgsInfo.lower_given = 1;
296   mArgsInfo.lower_arg = mThresholdSlider1->GetValue();
297   if (mRadioButtonLowerThan->isChecked()) {
298     mArgsInfo.upper_given = 1;
299     mArgsInfo.upper_arg = mThresholdSlider2->GetValue();
300     if (mArgsInfo.upper_arg<mArgsInfo.lower_arg) {
301       mArgsInfo.upper_given = 0;
302       DD("TODO : lower thres greater than greater thres ! Ignoring ");
303     }
304   }
305
306   mArgsInfo.fg_arg = mFGSlider->GetValue();
307   mArgsInfo.bg_arg = mBGSlider->GetValue();
308
309   if (inverseBGandFG) {
310     mArgsInfo.fg_arg = mFGSlider->GetValue();
311     mArgsInfo.bg_arg = mBGSlider->GetValue();
312   }
313   mArgsInfo.fg_given = 1;
314   mArgsInfo.bg_given = 1;
315
316   if (mCheckBoxUseBG->isChecked()) {
317     if (mCheckBoxUseFG->isChecked()) mArgsInfo.mode_arg = (char*)"both";
318     else mArgsInfo.mode_arg = (char*)"BG";
319   } else mArgsInfo.mode_arg = (char*)"FG";
320
321   mArgsInfo.verbose_flag = false;
322
323   // // Required (even if not used)
324   // mArgsInfo.input_given = 0;
325   // mArgsInfo.output_given = 0;
326
327   // mArgsInfo.input_arg = new char;
328   // mArgsInfo.output_arg = new char;
329 }
330 //------------------------------------------------------------------------------
331
332
333 //------------------------------------------------------------------------------
334 void vvToolBinarize::apply()
335
336   if (!mCurrentSlicerManager) close();
337   QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
338   GetArgsInfoFromGUI();  
339   
340   // Main filter
341   clitk::BinarizeImageGenericFilter::Pointer filter =
342     clitk::BinarizeImageGenericFilter::New();
343   filter->SetInputVVImage(mCurrentImage);
344   filter->SetArgsInfo(mArgsInfo);
345   filter->Update();
346
347
348   // Output
349   vvImage::Pointer output = filter->GetOutputVVImage();  
350   std::ostringstream osstream;
351   osstream << "Binarized_" << mCurrentSlicerManager->GetSlicer(0)->GetFileName() << ".mhd";
352   AddImage(output,osstream.str());
353   QApplication::restoreOverrideCursor();
354   close();
355 }
356 //------------------------------------------------------------------------------
357
358
359 //------------------------------------------------------------------------------
360 void vvToolBinarize::valueChangedT2(double v)
361
362   //  DD("valueChangedT2");
363   if (mRadioButtonLowerThan->isChecked()) {
364     mThresholdSlider1->SetMaximum(v);
365     if (!mInteractiveDisplayIsEnabled) return;
366     for(int i=0;i<mCurrentSlicerManager->GetNumberOfSlicers(); i++) {
367       mImageContourLower[i]->Update(v);
368     }
369     mCurrentSlicerManager->Render();
370   }
371 }
372 //------------------------------------------------------------------------------
373
374
375 //------------------------------------------------------------------------------
376 void vvToolBinarize::valueChangedT1(double v)
377
378   //  DD("valueChangedT1");
379   if (!mCurrentSlicerManager) close();
380   mThresholdSlider2->SetMinimum(v);
381   //  int m1 = (int)lrint(v);
382   if (!mInteractiveDisplayIsEnabled) return;
383   for(int i=0;i<mCurrentSlicerManager->GetNumberOfSlicers(); i++) {
384     mImageContour[i]->Update(v);
385   }
386   mCurrentSlicerManager->Render();
387 }
388 //------------------------------------------------------------------------------