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