]> Creatis software - clitk.git/blob - vv/vvToolBinarize.cxx
Fusion windows level is now 4 decimals
[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://oncora1.lyon.fnclcc.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   DD("Const binarize");
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 = new clitk::BinarizeImageGenericFilter;
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   RemoveVTKObjects();
135   event->accept();
136 }
137 //------------------------------------------------------------------------------
138
139
140 //------------------------------------------------------------------------------
141 void vvToolBinarize::reject()
142 {
143   // DD("vvToolBinarize::reject");
144   RemoveVTKObjects();
145   return vvToolWidgetBase::reject();
146 }
147 //------------------------------------------------------------------------------
148
149
150 //------------------------------------------------------------------------------
151 void vvToolBinarize::enableLowerThan(bool b)
152 {
153   if (!b) {
154     mThresholdSlider1->resetMaximum();
155     for(unsigned int i=0; i<mImageContour.size(); i++) {
156       mImageContourLower[i]->HideActors();    
157     }
158     mCurrentSlicerManager->Render();
159   } else {
160     valueChangedT1(mThresholdSlider1->GetValue());
161     valueChangedT2(mThresholdSlider2->GetValue());
162     for(unsigned int i=0; i<mImageContour.size(); i++) {
163       mImageContourLower[i]->ShowActors();    
164     }
165     mCurrentSlicerManager->Render();
166   }
167 }
168 //------------------------------------------------------------------------------
169
170
171 //------------------------------------------------------------------------------
172 void vvToolBinarize::useFGBGtoggled(bool)
173 {
174   if (!mCheckBoxUseBG->isChecked() && !mCheckBoxUseFG->isChecked())
175     mCheckBoxUseBG->toggle();
176 }
177 //------------------------------------------------------------------------------
178
179
180 //------------------------------------------------------------------------------
181 // void vvToolBinarize::InputIsSelected(std::vector<vvSlicerManager *> & m) {
182 //   DD("vvToolBinarize::InputIsSelected vector in binarize");
183 //   DD(m.size());
184 // }
185 void vvToolBinarize::InputIsSelected(vvSlicerManager * m)
186 {
187   mCurrentSlicerManager = m;
188
189   // Specific for this gui
190   mThresholdSlider1->SetValue(0);
191   mThresholdSlider2->SetValue(0);
192   mThresholdSlider1->SetImage(mCurrentImage);
193   mThresholdSlider2->SetImage(mCurrentImage);
194   mFGSlider->SetImage(mCurrentImage);
195   mBGSlider->SetImage(mCurrentImage);
196   //  DD(mCurrentSlicerManager->GetFileName().c_str());
197   //  mFGSlider->SetMaximum(mCurrentImage->GetFirstVTKImageData()->GetScalarTypeMax());
198   //   mFGSlider->SetMinimum(mCurrentImage->GetFirstVTKImageData()->GetScalarTypeMin());
199   //   mBGSlider->SetMaximum(mCurrentImage->GetFirstVTKImageData()->GetScalarTypeMax());
200   //   mBGSlider->SetMinimum(mCurrentImage->GetFirstVTKImageData()->GetScalarTypeMin());
201
202   // Output is uchar ...
203   mFGSlider->SetMaximum(255);
204   mFGSlider->SetMinimum(0);
205   mBGSlider->SetMaximum(255);
206   mBGSlider->SetMinimum(0);
207
208   mFGSlider->SetValue(1);
209   mBGSlider->SetValue(0);
210   mFGSlider->SetSingleStep(1);
211   mBGSlider->SetSingleStep(1);
212
213   // VTK objects for interactive display
214   for(int i=0; i<mCurrentSlicerManager->NumberOfSlicers(); i++) {
215     mImageContour.push_back(new vvImageContour);
216     mImageContour[i]->SetSlicer(mCurrentSlicerManager->GetSlicer(i));
217     mImageContour[i]->SetColor(1.0, 0.0, 0.0);
218     mImageContourLower.push_back(new vvImageContour);
219     mImageContourLower[i]->SetSlicer(mCurrentSlicerManager->GetSlicer(i));
220     mImageContourLower[i]->SetColor(0.0, 0.0, 1.0);
221   }
222   valueChangedT1(mThresholdSlider1->GetValue());
223
224   connect(mThresholdSlider1, SIGNAL(valueChanged(double)), this, SLOT(valueChangedT1(double)));
225   connect(mThresholdSlider2, SIGNAL(valueChanged(double)), this, SLOT(valueChangedT2(double)));
226
227   connect(mCurrentSlicerManager,SIGNAL(UpdateSlice(int,int)),this,SLOT(UpdateSlice(int, int)));
228   connect(mCurrentSlicerManager,SIGNAL(UpdateTSlice(int,int)),this,SLOT(UpdateSlice(int, int)));
229
230   //  connect(mCurrentSlicerManager, SIGNAL(LeftButtonReleaseSignal(int)), SLOT(LeftButtonReleaseEvent(int)));
231 }
232 //------------------------------------------------------------------------------
233
234
235 //------------------------------------------------------------------------------
236 // void vvToolBinarize::LeftButtonReleaseEvent(int slicer) {
237 //   DD("LeftButtonReleaseEvent");
238 //   for(int i=0; i<mCurrentSlicerManager->NumberOfSlicers(); i++) {
239 //     if (i == slicer);
240 //     mCurrentSlicerManager->GetSlicer(i)->GetRenderWindow()->Render();
241 //   }
242 // }
243 //------------------------------------------------------------------------------
244
245
246 //------------------------------------------------------------------------------
247 void vvToolBinarize::UpdateSlice(int slicer,int slices)
248 {
249   if (!mInteractiveDisplayIsEnabled) return;
250   if (!mCurrentSlicerManager) close();
251   mImageContour[slicer]->Update(mThresholdSlider1->GetValue());
252   if (mRadioButtonLowerThan->isChecked()) 
253     mImageContourLower[slicer]->Update(mThresholdSlider2->GetValue());
254 }
255 //------------------------------------------------------------------------------
256
257
258 //------------------------------------------------------------------------------
259 void vvToolBinarize::GetArgsInfoFromGUI()
260 {
261
262   /* //KEEP THIS FOR READING GGO FROM FILE
263      int argc=1;
264      std::string a = "toto";
265      char * const* argv = new char*;
266      //a.c_str();
267      struct cmdline_parser_params p;
268      p.check_required = 0;
269      int good = cmdline_parser_ext(argc, argv, &args_info, &p);
270      DD(good);
271   */
272
273   mArgsInfo.imagetypes_flag = 0;
274   mArgsInfo.upper_given = 0;
275   mArgsInfo.lower_given = 0;
276   bool inverseBGandFG = false;
277
278   mArgsInfo.lower_given = 1;
279   mArgsInfo.lower_arg = mThresholdSlider1->GetValue();
280   // DD(mArgsInfo.lower_arg);
281   if (mRadioButtonLowerThan->isChecked()) {
282     mArgsInfo.upper_given = 1;
283     mArgsInfo.upper_arg = mThresholdSlider2->GetValue();
284     if (mArgsInfo.upper_arg<mArgsInfo.lower_arg) {
285       mArgsInfo.upper_given = 0;
286       DD("TODO : lower thres greater than greater thres ! Ignoring ");
287     }
288   }
289
290   mArgsInfo.fg_arg = mFGSlider->GetValue();
291   mArgsInfo.bg_arg = mBGSlider->GetValue();
292
293   if (inverseBGandFG) {
294     mArgsInfo.fg_arg = mFGSlider->GetValue();
295     mArgsInfo.bg_arg = mBGSlider->GetValue();
296   }
297   mArgsInfo.fg_given = 1;
298   mArgsInfo.bg_given = 1;
299
300   if (mCheckBoxUseBG->isChecked()) {
301     if (mCheckBoxUseFG->isChecked()) mArgsInfo.mode_arg = (char*)"both";
302     else mArgsInfo.mode_arg = (char*)"BG";
303   } else mArgsInfo.mode_arg = (char*)"FG";
304
305   mArgsInfo.verbose_flag = false;
306
307   // Required (even if not used)
308   mArgsInfo.input_given = 0;
309   mArgsInfo.output_given = 0;
310
311   mArgsInfo.input_arg = new char;
312   mArgsInfo.output_arg = new char;
313 }
314 //------------------------------------------------------------------------------
315
316
317 //------------------------------------------------------------------------------
318 void vvToolBinarize::apply()
319 {
320   if (!mCurrentSlicerManager) close();
321   QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
322   GetArgsInfoFromGUI();
323
324   // Main filter
325   clitk::BinarizeImageGenericFilter::Pointer filter =
326     clitk::BinarizeImageGenericFilter::New();
327   filter->SetInputVVImage(mCurrentImage);
328   filter->SetArgsInfo(mArgsInfo);
329   filter->EnableReadOnDisk(false);
330   filter->Update();
331
332   // Output
333   vvImage::Pointer output = filter->GetOutputVVImage();
334   std::ostringstream osstream;
335   osstream << "Binarized_" << mCurrentSlicerManager->GetSlicer(0)->GetFileName() << ".mhd";
336   AddImage(output,osstream.str());
337   QApplication::restoreOverrideCursor();
338   close();
339 }
340 //------------------------------------------------------------------------------
341
342
343 //------------------------------------------------------------------------------
344 void vvToolBinarize::valueChangedT2(double v)
345 {
346   //  DD("valueChangedT2");
347   if (mRadioButtonLowerThan->isChecked()) {
348     mThresholdSlider1->SetMaximum(v);
349     if (!mInteractiveDisplayIsEnabled) return;
350     for(int i=0;i<mCurrentSlicerManager->NumberOfSlicers(); i++) {
351       mImageContourLower[i]->Update(v);
352     }
353     mCurrentSlicerManager->Render();
354   }
355 }
356 //------------------------------------------------------------------------------
357
358
359 //------------------------------------------------------------------------------
360 void vvToolBinarize::valueChangedT1(double v)
361 {
362   //  DD("valueChangedT1");
363   if (!mCurrentSlicerManager) close();
364   mThresholdSlider2->SetMinimum(v);
365   //  int m1 = (int)lrint(v);
366   if (!mInteractiveDisplayIsEnabled) return;
367   for(int i=0;i<mCurrentSlicerManager->NumberOfSlicers(); i++) {
368     mImageContour[i]->Update(v);
369   }
370   mCurrentSlicerManager->Render();
371 }
372 //------------------------------------------------------------------------------