]> Creatis software - clitk.git/blob - vv/vvToolBinarize.cxx
Rounding errors in display extent of overlay, fusion and vf.
[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->GetNumberOfSlicers(); i++) {
215     mImageContour.push_back(vvImageContour::New());
216     mImageContour[i]->SetSlicer(mCurrentSlicerManager->GetSlicer(i));
217     mImageContour[i]->SetColor(1.0, 0.0, 0.0);
218     mImageContourLower.push_back(vvImageContour::New());
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->GetNumberOfSlicers(); 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   cmdline_parser_clitkBinarizeImage_init(&mArgsInfo); // Initialisation to default
273   bool inverseBGandFG = false;
274
275   mArgsInfo.lower_given = 1;
276   mArgsInfo.lower_arg = mThresholdSlider1->GetValue();
277   if (mRadioButtonLowerThan->isChecked()) {
278     mArgsInfo.upper_given = 1;
279     mArgsInfo.upper_arg = mThresholdSlider2->GetValue();
280     if (mArgsInfo.upper_arg<mArgsInfo.lower_arg) {
281       mArgsInfo.upper_given = 0;
282       DD("TODO : lower thres greater than greater thres ! Ignoring ");
283     }
284   }
285
286   mArgsInfo.fg_arg = mFGSlider->GetValue();
287   mArgsInfo.bg_arg = mBGSlider->GetValue();
288
289   if (inverseBGandFG) {
290     mArgsInfo.fg_arg = mFGSlider->GetValue();
291     mArgsInfo.bg_arg = mBGSlider->GetValue();
292   }
293   mArgsInfo.fg_given = 1;
294   mArgsInfo.bg_given = 1;
295
296   if (mCheckBoxUseBG->isChecked()) {
297     if (mCheckBoxUseFG->isChecked()) mArgsInfo.mode_arg = (char*)"both";
298     else mArgsInfo.mode_arg = (char*)"BG";
299   } else mArgsInfo.mode_arg = (char*)"FG";
300
301   mArgsInfo.verbose_flag = false;
302
303   // // Required (even if not used)
304   // mArgsInfo.input_given = 0;
305   // mArgsInfo.output_given = 0;
306
307   // mArgsInfo.input_arg = new char;
308   // mArgsInfo.output_arg = new char;
309 }
310 //------------------------------------------------------------------------------
311
312
313 //------------------------------------------------------------------------------
314 void vvToolBinarize::apply()
315 {
316   if (!mCurrentSlicerManager) close();
317   QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
318   GetArgsInfoFromGUI();
319
320   // Main filter
321   clitk::BinarizeImageGenericFilter::Pointer filter =
322     clitk::BinarizeImageGenericFilter::New();
323   filter->SetInputVVImage(mCurrentImage);
324   filter->SetArgsInfo(mArgsInfo);
325   filter->Update();
326
327   // Output
328   vvImage::Pointer output = filter->GetOutputVVImage();
329   std::ostringstream osstream;
330   osstream << "Binarized_" << mCurrentSlicerManager->GetSlicer(0)->GetFileName() << ".mhd";
331   AddImage(output,osstream.str());
332   QApplication::restoreOverrideCursor();
333   close();
334 }
335 //------------------------------------------------------------------------------
336
337
338 //------------------------------------------------------------------------------
339 void vvToolBinarize::valueChangedT2(double v)
340 {
341   //  DD("valueChangedT2");
342   if (mRadioButtonLowerThan->isChecked()) {
343     mThresholdSlider1->SetMaximum(v);
344     if (!mInteractiveDisplayIsEnabled) return;
345     for(int i=0;i<mCurrentSlicerManager->GetNumberOfSlicers(); i++) {
346       mImageContourLower[i]->Update(v);
347     }
348     mCurrentSlicerManager->Render();
349   }
350 }
351 //------------------------------------------------------------------------------
352
353
354 //------------------------------------------------------------------------------
355 void vvToolBinarize::valueChangedT1(double v)
356 {
357   //  DD("valueChangedT1");
358   if (!mCurrentSlicerManager) close();
359   mThresholdSlider2->SetMinimum(v);
360   //  int m1 = (int)lrint(v);
361   if (!mInteractiveDisplayIsEnabled) return;
362   for(int i=0;i<mCurrentSlicerManager->GetNumberOfSlicers(); i++) {
363     mImageContour[i]->Update(v);
364   }
365   mCurrentSlicerManager->Render();
366 }
367 //------------------------------------------------------------------------------