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://www.centreleonberard.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 "vvToolBinarize.h"
21 #include "vvSlicerManager.h"
23 #include "vvToolInputSelectorWidget.h"
26 #include "clitkBinarizeImageGenericFilter.h"
29 #include <vtkImageActor.h>
30 #include <vtkCamera.h>
31 #include <vtkImageClip.h>
32 #include <vtkRenderWindow.h>
35 //------------------------------------------------------------------------------
36 // Create the tool and automagically (I like this word) insert it in
37 // the main window menu.
38 ADD_TOOL(vvToolBinarize);
39 //------------------------------------------------------------------------------
42 //------------------------------------------------------------------------------
43 void vvToolBinarize::Initialize()
45 SetToolName("Binarize");
46 SetToolMenuName("Binarize");
47 SetToolIconFilename(":/common/icons/binarize.png");
48 SetToolTip("Image interactive binarization with thresholds.");
50 //------------------------------------------------------------------------------
53 //------------------------------------------------------------------------------
54 vvToolBinarize::vvToolBinarize(vvMainWindowBase * parent, Qt::WindowFlags f)
55 :vvToolWidgetBase(parent,f),
56 vvToolBase<vvToolBinarize>(parent),
60 Ui_vvToolBinarize::setupUi(mToolWidget);
61 mInteractiveDisplayIsEnabled = mCheckBoxInteractiveDisplay->isChecked();
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)));
69 // Initialize some widget
70 mThresholdSlider1->SetText("");
71 mThresholdSlider2->SetText("");
72 mFGSlider->SetText("Foreground value");
73 mBGSlider->SetText("Background value");
76 mFilter = clitk::BinarizeImageGenericFilter::New();
78 // Set how many inputs are needed for this tool
79 AddInputSelector("Select one image", mFilter);
81 //------------------------------------------------------------------------------
84 //------------------------------------------------------------------------------
85 vvToolBinarize::~vvToolBinarize()
88 //------------------------------------------------------------------------------
91 //------------------------------------------------------------------------------
92 void vvToolBinarize::InteractiveDisplayToggled(bool b)
94 mInteractiveDisplayIsEnabled = b;
95 if (!mInteractiveDisplayIsEnabled) {
98 for(unsigned int i=0; i<mImageContour.size(); i++) {
99 mImageContour[i]->ShowActors();
100 if (mRadioButtonLowerThan->isChecked())
101 mImageContourLower[i]->ShowActors();
103 if (mCurrentSlicerManager)
104 mCurrentSlicerManager->Render();
107 //------------------------------------------------------------------------------
110 //------------------------------------------------------------------------------
111 void vvToolBinarize::RemoveVTKObjects()
113 for(unsigned int i=0; i<mImageContour.size(); i++) {
114 mImageContour[i]->HideActors();
115 mImageContourLower[i]->HideActors();
117 if (mCurrentSlicerManager)
118 mCurrentSlicerManager->Render();
120 //------------------------------------------------------------------------------
123 //------------------------------------------------------------------------------
124 bool vvToolBinarize::close()
126 // RemoveVTKObjects();
127 return vvToolWidgetBase::close();
129 //------------------------------------------------------------------------------
132 //------------------------------------------------------------------------------
133 void vvToolBinarize::closeEvent(QCloseEvent *event)
138 //------------------------------------------------------------------------------
141 //------------------------------------------------------------------------------
142 void vvToolBinarize::reject()
144 // DD("vvToolBinarize::reject");
146 return vvToolWidgetBase::reject();
148 //------------------------------------------------------------------------------
151 //------------------------------------------------------------------------------
152 void vvToolBinarize::enableLowerThan(bool b)
155 mThresholdSlider1->resetMaximum();
156 for(unsigned int i=0; i<mImageContour.size(); i++) {
157 mImageContourLower[i]->HideActors();
159 mCurrentSlicerManager->Render();
161 valueChangedT1(mThresholdSlider1->GetValue());
162 valueChangedT2(mThresholdSlider2->GetValue());
163 for(unsigned int i=0; i<mImageContour.size(); i++) {
164 mImageContourLower[i]->ShowActors();
166 mCurrentSlicerManager->Render();
169 //------------------------------------------------------------------------------
172 //------------------------------------------------------------------------------
173 void vvToolBinarize::useFGBGtoggled(bool)
175 if (!mCheckBoxUseBG->isChecked() && !mCheckBoxUseFG->isChecked())
176 mCheckBoxUseBG->toggle();
178 //------------------------------------------------------------------------------
181 //------------------------------------------------------------------------------
182 // void vvToolBinarize::InputIsSelected(std::vector<vvSlicerManager *> & m) {
183 // DD("vvToolBinarize::InputIsSelected vector in binarize");
186 void vvToolBinarize::InputIsSelected(vvSlicerManager * m)
188 mCurrentSlicerManager = m;
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());
203 // Output is uchar ...
204 mFGSlider->SetMaximum(255);
205 mFGSlider->SetMinimum(0);
206 mBGSlider->SetMaximum(255);
207 mBGSlider->SetMinimum(0);
209 mFGSlider->SetValue(1);
210 mBGSlider->SetValue(0);
211 mFGSlider->SetSingleStep(1);
212 mBGSlider->SetSingleStep(1);
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)
225 valueChangedT1(mThresholdSlider1->GetValue());
227 connect(mThresholdSlider1, SIGNAL(valueChanged(double)), this, SLOT(valueChangedT1(double)));
228 connect(mThresholdSlider2, SIGNAL(valueChanged(double)), this, SLOT(valueChangedT2(double)));
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)));
233 connect(mCurrentSlicerManager,SIGNAL(UpdateOrientation(int,int)),this,SLOT(UpdateOrientation(int, int)));
235 // connect(mCurrentSlicerManager, SIGNAL(LeftButtonReleaseSignal(int)), SLOT(LeftButtonReleaseEvent(int)));
236 InteractiveDisplayToggled(mInteractiveDisplayIsEnabled);
238 //------------------------------------------------------------------------------
241 //------------------------------------------------------------------------------
242 // void vvToolBinarize::LeftButtonReleaseEvent(int slicer) {
243 // DD("LeftButtonReleaseEvent");
244 // for(int i=0; i<mCurrentSlicerManager->GetNumberOfSlicers(); i++) {
246 // mCurrentSlicerManager->GetSlicer(i)->GetRenderWindow()->Render();
249 //------------------------------------------------------------------------------
252 //------------------------------------------------------------------------------
253 void vvToolBinarize::UpdateOrientation(int slicer,int orientation)
257 //------------------------------------------------------------------------------
260 //------------------------------------------------------------------------------
261 void vvToolBinarize::UpdateSlice(int slicer,int slices, int code)
265 //------------------------------------------------------------------------------
267 //------------------------------------------------------------------------------
268 void vvToolBinarize::Update(int slicer)
270 if (!mInteractiveDisplayIsEnabled) return;
271 if (!mCurrentSlicerManager) close();
272 mImageContour[slicer]->Update(mThresholdSlider1->GetValue());
273 if (mRadioButtonLowerThan->isChecked())
274 mImageContourLower[slicer]->Update(mThresholdSlider2->GetValue());
276 //------------------------------------------------------------------------------
278 //------------------------------------------------------------------------------
279 void vvToolBinarize::GetArgsInfoFromGUI()
282 /* //KEEP THIS FOR READING GGO FROM FILE
284 std::string a = "toto";
285 char * const* argv = new char*;
287 struct cmdline_parser_params p;
288 p.check_required = 0;
289 int good = cmdline_parser_ext(argc, argv, &args_info, &p);
292 cmdline_parser_clitkBinarizeImage_init(&mArgsInfo); // Initialisation to default
293 bool inverseBGandFG = false;
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 ");
306 mArgsInfo.fg_arg = mFGSlider->GetValue();
307 mArgsInfo.bg_arg = mBGSlider->GetValue();
309 if (inverseBGandFG) {
310 mArgsInfo.fg_arg = mFGSlider->GetValue();
311 mArgsInfo.bg_arg = mBGSlider->GetValue();
313 mArgsInfo.fg_given = 1;
314 mArgsInfo.bg_given = 1;
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";
321 mArgsInfo.verbose_flag = false;
323 // // Required (even if not used)
324 // mArgsInfo.input_given = 0;
325 // mArgsInfo.output_given = 0;
327 // mArgsInfo.input_arg = new char;
328 // mArgsInfo.output_arg = new char;
330 //------------------------------------------------------------------------------
333 //------------------------------------------------------------------------------
334 void vvToolBinarize::apply()
336 if (!mCurrentSlicerManager) close();
337 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
338 GetArgsInfoFromGUI();
341 clitk::BinarizeImageGenericFilter::Pointer filter =
342 clitk::BinarizeImageGenericFilter::New();
343 filter->SetInputVVImage(mCurrentImage);
344 filter->SetArgsInfo(mArgsInfo);
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();
356 //------------------------------------------------------------------------------
359 //------------------------------------------------------------------------------
360 void vvToolBinarize::valueChangedT2(double v)
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);
369 mCurrentSlicerManager->Render();
372 //------------------------------------------------------------------------------
375 //------------------------------------------------------------------------------
376 void vvToolBinarize::valueChangedT1(double v)
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);
386 mCurrentSlicerManager->Render();
388 //------------------------------------------------------------------------------