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://oncora1.lyon.fnclcc.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),
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 = new clitk::BinarizeImageGenericFilter;
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) {
137 //------------------------------------------------------------------------------
140 //------------------------------------------------------------------------------
141 void vvToolBinarize::reject()
143 // DD("vvToolBinarize::reject");
145 return vvToolWidgetBase::reject();
147 //------------------------------------------------------------------------------
150 //------------------------------------------------------------------------------
151 void vvToolBinarize::enableLowerThan(bool b)
154 mThresholdSlider1->resetMaximum();
155 for(unsigned int i=0; i<mImageContour.size(); i++) {
156 mImageContourLower[i]->HideActors();
158 mCurrentSlicerManager->Render();
160 valueChangedT1(mThresholdSlider1->GetValue());
161 valueChangedT2(mThresholdSlider2->GetValue());
162 for(unsigned int i=0; i<mImageContour.size(); i++) {
163 mImageContourLower[i]->ShowActors();
165 mCurrentSlicerManager->Render();
168 //------------------------------------------------------------------------------
171 //------------------------------------------------------------------------------
172 void vvToolBinarize::useFGBGtoggled(bool)
174 if (!mCheckBoxUseBG->isChecked() && !mCheckBoxUseFG->isChecked())
175 mCheckBoxUseBG->toggle();
177 //------------------------------------------------------------------------------
180 //------------------------------------------------------------------------------
181 // void vvToolBinarize::InputIsSelected(std::vector<vvSlicerManager *> & m) {
182 // DD("vvToolBinarize::InputIsSelected vector in binarize");
185 void vvToolBinarize::InputIsSelected(vvSlicerManager * m)
187 mCurrentSlicerManager = m;
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());
202 // Output is uchar ...
203 mFGSlider->SetMaximum(255);
204 mFGSlider->SetMinimum(0);
205 mBGSlider->SetMaximum(255);
206 mBGSlider->SetMinimum(0);
208 mFGSlider->SetValue(1);
209 mBGSlider->SetValue(0);
210 mFGSlider->SetSingleStep(1);
211 mBGSlider->SetSingleStep(1);
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);
222 valueChangedT1(mThresholdSlider1->GetValue());
224 connect(mThresholdSlider1, SIGNAL(valueChanged(double)), this, SLOT(valueChangedT1(double)));
225 connect(mThresholdSlider2, SIGNAL(valueChanged(double)), this, SLOT(valueChangedT2(double)));
227 connect(mCurrentSlicerManager,SIGNAL(UpdateSlice(int,int)),this,SLOT(UpdateSlice(int, int)));
228 connect(mCurrentSlicerManager,SIGNAL(UpdateTSlice(int,int)),this,SLOT(UpdateSlice(int, int)));
230 // connect(mCurrentSlicerManager, SIGNAL(LeftButtonReleaseSignal(int)), SLOT(LeftButtonReleaseEvent(int)));
232 //------------------------------------------------------------------------------
235 //------------------------------------------------------------------------------
236 // void vvToolBinarize::LeftButtonReleaseEvent(int slicer) {
237 // DD("LeftButtonReleaseEvent");
238 // for(int i=0; i<mCurrentSlicerManager->GetNumberOfSlicers(); i++) {
240 // mCurrentSlicerManager->GetSlicer(i)->GetRenderWindow()->Render();
243 //------------------------------------------------------------------------------
246 //------------------------------------------------------------------------------
247 void vvToolBinarize::UpdateSlice(int slicer,int slices)
249 if (!mInteractiveDisplayIsEnabled) return;
250 if (!mCurrentSlicerManager) close();
251 mImageContour[slicer]->Update(mThresholdSlider1->GetValue());
252 if (mRadioButtonLowerThan->isChecked())
253 mImageContourLower[slicer]->Update(mThresholdSlider2->GetValue());
255 //------------------------------------------------------------------------------
258 //------------------------------------------------------------------------------
259 void vvToolBinarize::GetArgsInfoFromGUI()
262 /* //KEEP THIS FOR READING GGO FROM FILE
264 std::string a = "toto";
265 char * const* argv = new char*;
267 struct cmdline_parser_params p;
268 p.check_required = 0;
269 int good = cmdline_parser_ext(argc, argv, &args_info, &p);
272 cmdline_parser_clitkBinarizeImage_init(&mArgsInfo); // Initialisation to default
273 bool inverseBGandFG = false;
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 ");
286 mArgsInfo.fg_arg = mFGSlider->GetValue();
287 mArgsInfo.bg_arg = mBGSlider->GetValue();
289 if (inverseBGandFG) {
290 mArgsInfo.fg_arg = mFGSlider->GetValue();
291 mArgsInfo.bg_arg = mBGSlider->GetValue();
293 mArgsInfo.fg_given = 1;
294 mArgsInfo.bg_given = 1;
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";
301 mArgsInfo.verbose_flag = false;
303 // // Required (even if not used)
304 // mArgsInfo.input_given = 0;
305 // mArgsInfo.output_given = 0;
307 // mArgsInfo.input_arg = new char;
308 // mArgsInfo.output_arg = new char;
310 //------------------------------------------------------------------------------
313 //------------------------------------------------------------------------------
314 void vvToolBinarize::apply()
316 if (!mCurrentSlicerManager) close();
317 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
318 GetArgsInfoFromGUI();
321 clitk::BinarizeImageGenericFilter::Pointer filter =
322 clitk::BinarizeImageGenericFilter::New();
323 filter->SetInputVVImage(mCurrentImage);
324 filter->SetArgsInfo(mArgsInfo);
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();
335 //------------------------------------------------------------------------------
338 //------------------------------------------------------------------------------
339 void vvToolBinarize::valueChangedT2(double v)
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);
348 mCurrentSlicerManager->Render();
351 //------------------------------------------------------------------------------
354 //------------------------------------------------------------------------------
355 void vvToolBinarize::valueChangedT1(double v)
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);
365 mCurrentSlicerManager->Render();
367 //------------------------------------------------------------------------------