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 mImageContourLower.push_back(vvImageContour::New());
218 mImageContourLower[i]->SetSlicer(mCurrentSlicerManager->GetSlicer(i));
219 mImageContourLower[i]->SetColor(0.0, 0.0, 1.0);
221 valueChangedT1(mThresholdSlider1->GetValue());
223 connect(mThresholdSlider1, SIGNAL(valueChanged(double)), this, SLOT(valueChangedT1(double)));
224 connect(mThresholdSlider2, SIGNAL(valueChanged(double)), this, SLOT(valueChangedT2(double)));
226 connect(mCurrentSlicerManager,SIGNAL(UpdateSlice(int,int)),this,SLOT(UpdateSlice(int, int)));
227 connect(mCurrentSlicerManager,SIGNAL(UpdateTSlice(int,int)),this,SLOT(UpdateSlice(int, int)));
229 connect(mCurrentSlicerManager,SIGNAL(UpdateOrientation(int,int)),this,SLOT(UpdateOrientation(int, int)));
231 // connect(mCurrentSlicerManager, SIGNAL(LeftButtonReleaseSignal(int)), SLOT(LeftButtonReleaseEvent(int)));
233 InteractiveDisplayToggled(mInteractiveDisplayIsEnabled);
235 //------------------------------------------------------------------------------
238 //------------------------------------------------------------------------------
239 // void vvToolBinarize::LeftButtonReleaseEvent(int slicer) {
240 // DD("LeftButtonReleaseEvent");
241 // for(int i=0; i<mCurrentSlicerManager->GetNumberOfSlicers(); i++) {
243 // mCurrentSlicerManager->GetSlicer(i)->GetRenderWindow()->Render();
246 //------------------------------------------------------------------------------
249 //------------------------------------------------------------------------------
250 void vvToolBinarize::UpdateOrientation(int slicer,int orientation)
254 //------------------------------------------------------------------------------
256 //------------------------------------------------------------------------------
257 void vvToolBinarize::UpdateSlice(int slicer,int slices)
261 //------------------------------------------------------------------------------
263 //------------------------------------------------------------------------------
264 void vvToolBinarize::Update(int slicer)
266 if (!mInteractiveDisplayIsEnabled) return;
267 if (!mCurrentSlicerManager) close();
268 mImageContour[slicer]->Update(mThresholdSlider1->GetValue());
269 if (mRadioButtonLowerThan->isChecked())
270 mImageContourLower[slicer]->Update(mThresholdSlider2->GetValue());
272 //------------------------------------------------------------------------------
274 //------------------------------------------------------------------------------
275 void vvToolBinarize::GetArgsInfoFromGUI()
278 /* //KEEP THIS FOR READING GGO FROM FILE
280 std::string a = "toto";
281 char * const* argv = new char*;
283 struct cmdline_parser_params p;
284 p.check_required = 0;
285 int good = cmdline_parser_ext(argc, argv, &args_info, &p);
288 cmdline_parser_clitkBinarizeImage_init(&mArgsInfo); // Initialisation to default
289 bool inverseBGandFG = false;
291 mArgsInfo.lower_given = 1;
292 mArgsInfo.lower_arg = mThresholdSlider1->GetValue();
293 if (mRadioButtonLowerThan->isChecked()) {
294 mArgsInfo.upper_given = 1;
295 mArgsInfo.upper_arg = mThresholdSlider2->GetValue();
296 if (mArgsInfo.upper_arg<mArgsInfo.lower_arg) {
297 mArgsInfo.upper_given = 0;
298 DD("TODO : lower thres greater than greater thres ! Ignoring ");
302 mArgsInfo.fg_arg = mFGSlider->GetValue();
303 mArgsInfo.bg_arg = mBGSlider->GetValue();
305 if (inverseBGandFG) {
306 mArgsInfo.fg_arg = mFGSlider->GetValue();
307 mArgsInfo.bg_arg = mBGSlider->GetValue();
309 mArgsInfo.fg_given = 1;
310 mArgsInfo.bg_given = 1;
312 if (mCheckBoxUseBG->isChecked()) {
313 if (mCheckBoxUseFG->isChecked()) mArgsInfo.mode_arg = (char*)"both";
314 else mArgsInfo.mode_arg = (char*)"BG";
315 } else mArgsInfo.mode_arg = (char*)"FG";
317 mArgsInfo.verbose_flag = false;
319 // // Required (even if not used)
320 // mArgsInfo.input_given = 0;
321 // mArgsInfo.output_given = 0;
323 // mArgsInfo.input_arg = new char;
324 // mArgsInfo.output_arg = new char;
326 //------------------------------------------------------------------------------
329 //------------------------------------------------------------------------------
330 void vvToolBinarize::apply()
332 if (!mCurrentSlicerManager) close();
333 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
334 GetArgsInfoFromGUI();
337 clitk::BinarizeImageGenericFilter::Pointer filter =
338 clitk::BinarizeImageGenericFilter::New();
339 filter->SetInputVVImage(mCurrentImage);
340 filter->SetArgsInfo(mArgsInfo);
344 vvImage::Pointer output = filter->GetOutputVVImage();
345 std::ostringstream osstream;
346 osstream << "Binarized_" << mCurrentSlicerManager->GetSlicer(0)->GetFileName() << ".mhd";
347 AddImage(output,osstream.str());
348 QApplication::restoreOverrideCursor();
351 //------------------------------------------------------------------------------
354 //------------------------------------------------------------------------------
355 void vvToolBinarize::valueChangedT2(double v)
357 // DD("valueChangedT2");
358 if (mRadioButtonLowerThan->isChecked()) {
359 mThresholdSlider1->SetMaximum(v);
360 if (!mInteractiveDisplayIsEnabled) return;
361 for(int i=0;i<mCurrentSlicerManager->GetNumberOfSlicers(); i++) {
362 mImageContourLower[i]->Update(v);
364 mCurrentSlicerManager->Render();
367 //------------------------------------------------------------------------------
370 //------------------------------------------------------------------------------
371 void vvToolBinarize::valueChangedT1(double v)
373 // DD("valueChangedT1");
374 if (!mCurrentSlicerManager) close();
375 mThresholdSlider2->SetMinimum(v);
376 // int m1 = (int)lrint(v);
377 if (!mInteractiveDisplayIsEnabled) return;
378 for(int i=0;i<mCurrentSlicerManager->GetNumberOfSlicers(); i++) {
379 mImageContour[i]->Update(v);
381 mCurrentSlicerManager->Render();
383 //------------------------------------------------------------------------------