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(LeftButtonReleaseSignal(int)), SLOT(LeftButtonReleaseEvent(int)));
231 InteractiveDisplayToggled(mInteractiveDisplayIsEnabled);
233 //------------------------------------------------------------------------------
236 //------------------------------------------------------------------------------
237 // void vvToolBinarize::LeftButtonReleaseEvent(int slicer) {
238 // DD("LeftButtonReleaseEvent");
239 // for(int i=0; i<mCurrentSlicerManager->GetNumberOfSlicers(); i++) {
241 // mCurrentSlicerManager->GetSlicer(i)->GetRenderWindow()->Render();
244 //------------------------------------------------------------------------------
247 //------------------------------------------------------------------------------
248 void vvToolBinarize::UpdateSlice(int slicer,int slices)
250 if (!mInteractiveDisplayIsEnabled) return;
251 if (!mCurrentSlicerManager) close();
252 mImageContour[slicer]->Update(mThresholdSlider1->GetValue());
253 if (mRadioButtonLowerThan->isChecked())
254 mImageContourLower[slicer]->Update(mThresholdSlider2->GetValue());
256 //------------------------------------------------------------------------------
259 //------------------------------------------------------------------------------
260 void vvToolBinarize::GetArgsInfoFromGUI()
263 /* //KEEP THIS FOR READING GGO FROM FILE
265 std::string a = "toto";
266 char * const* argv = new char*;
268 struct cmdline_parser_params p;
269 p.check_required = 0;
270 int good = cmdline_parser_ext(argc, argv, &args_info, &p);
273 cmdline_parser_clitkBinarizeImage_init(&mArgsInfo); // Initialisation to default
274 bool inverseBGandFG = false;
276 mArgsInfo.lower_given = 1;
277 mArgsInfo.lower_arg = mThresholdSlider1->GetValue();
278 if (mRadioButtonLowerThan->isChecked()) {
279 mArgsInfo.upper_given = 1;
280 mArgsInfo.upper_arg = mThresholdSlider2->GetValue();
281 if (mArgsInfo.upper_arg<mArgsInfo.lower_arg) {
282 mArgsInfo.upper_given = 0;
283 DD("TODO : lower thres greater than greater thres ! Ignoring ");
287 mArgsInfo.fg_arg = mFGSlider->GetValue();
288 mArgsInfo.bg_arg = mBGSlider->GetValue();
290 if (inverseBGandFG) {
291 mArgsInfo.fg_arg = mFGSlider->GetValue();
292 mArgsInfo.bg_arg = mBGSlider->GetValue();
294 mArgsInfo.fg_given = 1;
295 mArgsInfo.bg_given = 1;
297 if (mCheckBoxUseBG->isChecked()) {
298 if (mCheckBoxUseFG->isChecked()) mArgsInfo.mode_arg = (char*)"both";
299 else mArgsInfo.mode_arg = (char*)"BG";
300 } else mArgsInfo.mode_arg = (char*)"FG";
302 mArgsInfo.verbose_flag = false;
304 // // Required (even if not used)
305 // mArgsInfo.input_given = 0;
306 // mArgsInfo.output_given = 0;
308 // mArgsInfo.input_arg = new char;
309 // mArgsInfo.output_arg = new char;
311 //------------------------------------------------------------------------------
314 //------------------------------------------------------------------------------
315 void vvToolBinarize::apply()
317 if (!mCurrentSlicerManager) close();
318 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
319 GetArgsInfoFromGUI();
322 clitk::BinarizeImageGenericFilter::Pointer filter =
323 clitk::BinarizeImageGenericFilter::New();
324 filter->SetInputVVImage(mCurrentImage);
325 filter->SetArgsInfo(mArgsInfo);
329 vvImage::Pointer output = filter->GetOutputVVImage();
330 std::ostringstream osstream;
331 osstream << "Binarized_" << mCurrentSlicerManager->GetSlicer(0)->GetFileName() << ".mhd";
332 AddImage(output,osstream.str());
333 QApplication::restoreOverrideCursor();
336 //------------------------------------------------------------------------------
339 //------------------------------------------------------------------------------
340 void vvToolBinarize::valueChangedT2(double v)
342 // DD("valueChangedT2");
343 if (mRadioButtonLowerThan->isChecked()) {
344 mThresholdSlider1->SetMaximum(v);
345 if (!mInteractiveDisplayIsEnabled) return;
346 for(int i=0;i<mCurrentSlicerManager->GetNumberOfSlicers(); i++) {
347 mImageContourLower[i]->Update(v);
349 mCurrentSlicerManager->Render();
352 //------------------------------------------------------------------------------
355 //------------------------------------------------------------------------------
356 void vvToolBinarize::valueChangedT1(double v)
358 // DD("valueChangedT1");
359 if (!mCurrentSlicerManager) close();
360 mThresholdSlider2->SetMinimum(v);
361 // int m1 = (int)lrint(v);
362 if (!mInteractiveDisplayIsEnabled) return;
363 for(int i=0;i<mCurrentSlicerManager->GetNumberOfSlicers(); i++) {
364 mImageContour[i]->Update(v);
366 mCurrentSlicerManager->Render();
368 //------------------------------------------------------------------------------