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<args_info_clitkBinarizeImage>;
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->NumberOfSlicers(); i++) {
215 mImageContour.push_back(new vvImageContour);
216 mImageContour[i]->SetSlicer(mCurrentSlicerManager->GetSlicer(i));
217 mImageContour[i]->SetColor(1.0, 0.0, 0.0);
218 mImageContourLower.push_back(new vvImageContour);
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->NumberOfSlicers(); 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);
273 mArgsInfo.imagetypes_flag = 0;
274 mArgsInfo.upper_given = 0;
275 mArgsInfo.lower_given = 0;
276 bool inverseBGandFG = false;
278 mArgsInfo.lower_given = 1;
279 mArgsInfo.lower_arg = mThresholdSlider1->GetValue();
280 // DD(mArgsInfo.lower_arg);
281 if (mRadioButtonLowerThan->isChecked()) {
282 mArgsInfo.upper_given = 1;
283 mArgsInfo.upper_arg = mThresholdSlider2->GetValue();
284 if (mArgsInfo.upper_arg<mArgsInfo.lower_arg) {
285 mArgsInfo.upper_given = 0;
286 DD("TODO : lower thres greater than greater thres ! Ignoring ");
290 mArgsInfo.fg_arg = mFGSlider->GetValue();
291 mArgsInfo.bg_arg = mBGSlider->GetValue();
293 if (inverseBGandFG) {
294 mArgsInfo.fg_arg = mFGSlider->GetValue();
295 mArgsInfo.bg_arg = mBGSlider->GetValue();
297 mArgsInfo.fg_given = 1;
298 mArgsInfo.bg_given = 1;
300 if (mCheckBoxUseBG->isChecked()) {
301 if (mCheckBoxUseFG->isChecked()) mArgsInfo.mode_arg = (char*)"both";
302 else mArgsInfo.mode_arg = (char*)"BG";
303 } else mArgsInfo.mode_arg = (char*)"FG";
305 mArgsInfo.verbose_flag = false;
307 // Required (even if not used)
308 mArgsInfo.input_given = 0;
309 mArgsInfo.output_given = 0;
311 mArgsInfo.input_arg = new char;
312 mArgsInfo.output_arg = new char;
314 //------------------------------------------------------------------------------
317 //------------------------------------------------------------------------------
318 void vvToolBinarize::apply()
320 if (!mCurrentSlicerManager) close();
321 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
322 GetArgsInfoFromGUI();
325 clitk::BinarizeImageGenericFilter<args_info_clitkBinarizeImage>::Pointer filter =
326 clitk::BinarizeImageGenericFilter<args_info_clitkBinarizeImage>::New();
327 filter->SetInputVVImage(mCurrentImage);
328 filter->SetArgsInfo(mArgsInfo);
329 filter->EnableReadOnDisk(false);
333 vvImage::Pointer output = filter->GetOutputVVImage();
334 std::ostringstream osstream;
335 osstream << "Binarized_" << mCurrentSlicerManager->GetSlicer(0)->GetFileName() << ".mhd";
336 AddImage(output,osstream.str());
337 QApplication::restoreOverrideCursor();
340 //------------------------------------------------------------------------------
343 //------------------------------------------------------------------------------
344 void vvToolBinarize::valueChangedT2(double v)
346 // DD("valueChangedT2");
347 if (mRadioButtonLowerThan->isChecked()) {
348 mThresholdSlider1->SetMaximum(v);
349 if (!mInteractiveDisplayIsEnabled) return;
350 for(int i=0;i<mCurrentSlicerManager->NumberOfSlicers(); i++) {
351 mImageContourLower[i]->Update(v);
353 mCurrentSlicerManager->Render();
356 //------------------------------------------------------------------------------
359 //------------------------------------------------------------------------------
360 void vvToolBinarize::valueChangedT1(double v)
362 // DD("valueChangedT1");
363 if (!mCurrentSlicerManager) close();
364 mThresholdSlider2->SetMinimum(v);
365 // int m1 = (int)lrint(v);
366 if (!mInteractiveDisplayIsEnabled) return;
367 for(int i=0;i<mCurrentSlicerManager->NumberOfSlicers(); i++) {
368 mImageContour[i]->Update(v);
370 mCurrentSlicerManager->Render();
372 //------------------------------------------------------------------------------