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>
34 //------------------------------------------------------------------------------
35 // Create the tool and automagically (I like this word) insert it in
36 // the main window menu.
37 ADD_TOOL(vvToolBinarize);
38 //------------------------------------------------------------------------------
41 //------------------------------------------------------------------------------
42 void vvToolBinarize::Initialize()
44 SetToolName("Binarize");
45 SetToolMenuName("Binarize");
46 SetToolIconFilename(":/common/icons/binarize.png");
47 SetToolTip("Image interactive binarization with thresholds.");
49 //------------------------------------------------------------------------------
52 //------------------------------------------------------------------------------
53 vvToolBinarize::vvToolBinarize(vvMainWindowBase * parent, Qt::WindowFlags f)
54 :vvToolWidgetBase(parent,f),
55 vvToolBase<vvToolBinarize>(parent),
59 Ui_vvToolBinarize::setupUi(mToolWidget);
60 mInteractiveDisplayIsEnabled = mCheckBoxInteractiveDisplay->isChecked();
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)));
68 // Initialize some widget
69 mThresholdSlider1->SetText("");
70 mThresholdSlider2->SetText("");
71 mFGSlider->SetText("Foreground value");
72 mBGSlider->SetText("Background value");
75 mFilter = clitk::BinarizeImageGenericFilter::New();
77 // Set how many inputs are needed for this tool
78 AddInputSelector("Select one image", mFilter);
80 //------------------------------------------------------------------------------
83 //------------------------------------------------------------------------------
84 vvToolBinarize::~vvToolBinarize()
87 //------------------------------------------------------------------------------
90 //------------------------------------------------------------------------------
91 void vvToolBinarize::InteractiveDisplayToggled(bool b)
93 mInteractiveDisplayIsEnabled = b;
94 if (!mInteractiveDisplayIsEnabled) {
97 for(unsigned int i=0; i<mImageContour.size(); i++) {
98 mImageContour[i]->ShowActors();
99 if (mRadioButtonLowerThan->isChecked())
100 mImageContourLower[i]->ShowActors();
102 if (mCurrentSlicerManager)
103 mCurrentSlicerManager->Render();
106 //------------------------------------------------------------------------------
109 //------------------------------------------------------------------------------
110 void vvToolBinarize::RemoveVTKObjects()
112 for(unsigned int i=0; i<mImageContour.size(); i++) {
113 mImageContour[i]->HideActors();
114 mImageContourLower[i]->HideActors();
116 if (mCurrentSlicerManager)
117 mCurrentSlicerManager->Render();
119 //------------------------------------------------------------------------------
122 //------------------------------------------------------------------------------
123 bool vvToolBinarize::close()
125 // RemoveVTKObjects();
126 return vvToolWidgetBase::close();
128 //------------------------------------------------------------------------------
131 //------------------------------------------------------------------------------
132 void vvToolBinarize::closeEvent(QCloseEvent *event) {
136 //------------------------------------------------------------------------------
139 //------------------------------------------------------------------------------
140 void vvToolBinarize::reject()
142 // DD("vvToolBinarize::reject");
144 return vvToolWidgetBase::reject();
146 //------------------------------------------------------------------------------
149 //------------------------------------------------------------------------------
150 void vvToolBinarize::enableLowerThan(bool b)
153 mThresholdSlider1->resetMaximum();
154 for(unsigned int i=0; i<mImageContour.size(); i++) {
155 mImageContourLower[i]->HideActors();
157 mCurrentSlicerManager->Render();
159 valueChangedT1(mThresholdSlider1->GetValue());
160 valueChangedT2(mThresholdSlider2->GetValue());
161 for(unsigned int i=0; i<mImageContour.size(); i++) {
162 mImageContourLower[i]->ShowActors();
164 mCurrentSlicerManager->Render();
167 //------------------------------------------------------------------------------
170 //------------------------------------------------------------------------------
171 void vvToolBinarize::useFGBGtoggled(bool)
173 if (!mCheckBoxUseBG->isChecked() && !mCheckBoxUseFG->isChecked())
174 mCheckBoxUseBG->toggle();
176 //------------------------------------------------------------------------------
179 //------------------------------------------------------------------------------
180 // void vvToolBinarize::InputIsSelected(std::vector<vvSlicerManager *> & m) {
181 // DD("vvToolBinarize::InputIsSelected vector in binarize");
184 void vvToolBinarize::InputIsSelected(vvSlicerManager * m)
186 mCurrentSlicerManager = m;
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());
201 // Output is uchar ...
202 mFGSlider->SetMaximum(255);
203 mFGSlider->SetMinimum(0);
204 mBGSlider->SetMaximum(255);
205 mBGSlider->SetMinimum(0);
207 mFGSlider->SetValue(1);
208 mBGSlider->SetValue(0);
209 mFGSlider->SetSingleStep(1);
210 mBGSlider->SetSingleStep(1);
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 mImageContour[i]->SetDepth(100); // to be in front of (whe used with ROI tool)
218 mImageContourLower.push_back(vvImageContour::New());
219 mImageContourLower[i]->SetSlicer(mCurrentSlicerManager->GetSlicer(i));
220 mImageContourLower[i]->SetColor(0.0, 0.0, 1.0);
221 mImageContourLower[i]->SetDepth(100); // to be in front of (whe used with ROI tool)
223 valueChangedT1(mThresholdSlider1->GetValue());
225 connect(mThresholdSlider1, SIGNAL(valueChanged(double)), this, SLOT(valueChangedT1(double)));
226 connect(mThresholdSlider2, SIGNAL(valueChanged(double)), this, SLOT(valueChangedT2(double)));
228 connect(mCurrentSlicerManager,SIGNAL(UpdateSlice(int,int)),this,SLOT(UpdateSlice(int, int)));
229 connect(mCurrentSlicerManager,SIGNAL(UpdateTSlice(int,int)),this,SLOT(UpdateSlice(int, int)));
231 connect(mCurrentSlicerManager,SIGNAL(UpdateOrientation(int,int)),this,SLOT(UpdateOrientation(int, int)));
233 // connect(mCurrentSlicerManager, SIGNAL(LeftButtonReleaseSignal(int)), SLOT(LeftButtonReleaseEvent(int)));
235 InteractiveDisplayToggled(mInteractiveDisplayIsEnabled);
237 //------------------------------------------------------------------------------
240 //------------------------------------------------------------------------------
241 // void vvToolBinarize::LeftButtonReleaseEvent(int slicer) {
242 // DD("LeftButtonReleaseEvent");
243 // for(int i=0; i<mCurrentSlicerManager->GetNumberOfSlicers(); i++) {
245 // mCurrentSlicerManager->GetSlicer(i)->GetRenderWindow()->Render();
248 //------------------------------------------------------------------------------
251 //------------------------------------------------------------------------------
252 void vvToolBinarize::UpdateOrientation(int slicer,int orientation)
256 //------------------------------------------------------------------------------
258 //------------------------------------------------------------------------------
259 void vvToolBinarize::UpdateSlice(int slicer,int slices)
263 //------------------------------------------------------------------------------
265 //------------------------------------------------------------------------------
266 void vvToolBinarize::Update(int slicer)
268 if (!mInteractiveDisplayIsEnabled) return;
269 if (!mCurrentSlicerManager) close();
270 mImageContour[slicer]->Update(mThresholdSlider1->GetValue());
271 if (mRadioButtonLowerThan->isChecked())
272 mImageContourLower[slicer]->Update(mThresholdSlider2->GetValue());
274 //------------------------------------------------------------------------------
276 //------------------------------------------------------------------------------
277 void vvToolBinarize::GetArgsInfoFromGUI()
280 /* //KEEP THIS FOR READING GGO FROM FILE
282 std::string a = "toto";
283 char * const* argv = new char*;
285 struct cmdline_parser_params p;
286 p.check_required = 0;
287 int good = cmdline_parser_ext(argc, argv, &args_info, &p);
290 cmdline_parser_clitkBinarizeImage_init(&mArgsInfo); // Initialisation to default
291 bool inverseBGandFG = false;
293 mArgsInfo.lower_given = 1;
294 mArgsInfo.lower_arg = mThresholdSlider1->GetValue();
295 if (mRadioButtonLowerThan->isChecked()) {
296 mArgsInfo.upper_given = 1;
297 mArgsInfo.upper_arg = mThresholdSlider2->GetValue();
298 if (mArgsInfo.upper_arg<mArgsInfo.lower_arg) {
299 mArgsInfo.upper_given = 0;
300 DD("TODO : lower thres greater than greater thres ! Ignoring ");
304 mArgsInfo.fg_arg = mFGSlider->GetValue();
305 mArgsInfo.bg_arg = mBGSlider->GetValue();
307 if (inverseBGandFG) {
308 mArgsInfo.fg_arg = mFGSlider->GetValue();
309 mArgsInfo.bg_arg = mBGSlider->GetValue();
311 mArgsInfo.fg_given = 1;
312 mArgsInfo.bg_given = 1;
314 if (mCheckBoxUseBG->isChecked()) {
315 if (mCheckBoxUseFG->isChecked()) mArgsInfo.mode_arg = (char*)"both";
316 else mArgsInfo.mode_arg = (char*)"BG";
317 } else mArgsInfo.mode_arg = (char*)"FG";
319 mArgsInfo.verbose_flag = false;
321 // // Required (even if not used)
322 // mArgsInfo.input_given = 0;
323 // mArgsInfo.output_given = 0;
325 // mArgsInfo.input_arg = new char;
326 // mArgsInfo.output_arg = new char;
328 //------------------------------------------------------------------------------
331 //------------------------------------------------------------------------------
332 void vvToolBinarize::apply()
334 if (!mCurrentSlicerManager) close();
335 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
336 GetArgsInfoFromGUI();
339 clitk::BinarizeImageGenericFilter::Pointer filter =
340 clitk::BinarizeImageGenericFilter::New();
341 filter->SetInputVVImage(mCurrentImage);
342 filter->SetArgsInfo(mArgsInfo);
346 vvImage::Pointer output = filter->GetOutputVVImage();
347 std::ostringstream osstream;
348 osstream << "Binarized_" << mCurrentSlicerManager->GetSlicer(0)->GetFileName() << ".mhd";
349 AddImage(output,osstream.str());
350 QApplication::restoreOverrideCursor();
353 //------------------------------------------------------------------------------
356 //------------------------------------------------------------------------------
357 void vvToolBinarize::valueChangedT2(double v)
359 // DD("valueChangedT2");
360 if (mRadioButtonLowerThan->isChecked()) {
361 mThresholdSlider1->SetMaximum(v);
362 if (!mInteractiveDisplayIsEnabled) return;
363 for(int i=0;i<mCurrentSlicerManager->GetNumberOfSlicers(); i++) {
364 mImageContourLower[i]->Update(v);
366 mCurrentSlicerManager->Render();
369 //------------------------------------------------------------------------------
372 //------------------------------------------------------------------------------
373 void vvToolBinarize::valueChangedT1(double v)
375 // DD("valueChangedT1");
376 if (!mCurrentSlicerManager) close();
377 mThresholdSlider2->SetMinimum(v);
378 // int m1 = (int)lrint(v);
379 if (!mInteractiveDisplayIsEnabled) return;
380 for(int i=0;i<mCurrentSlicerManager->GetNumberOfSlicers(); i++) {
381 mImageContour[i]->Update(v);
383 mCurrentSlicerManager->Render();
385 //------------------------------------------------------------------------------