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 "vvToolExtractLung.h"
21 #include "vvToolStructureSetManager.h"
23 #include "vvImageReader.h"
24 #include "vvImageWriter.h"
25 #include "vvLabelImageLoaderWidget.h"
26 #include "vvThreadedFilter.h"
29 #include <QMessageBox>
31 //------------------------------------------------------------------------------
32 // Create the tool and automagically (I like this word) insert it in
33 // the main window menu.
34 ADD_TOOL(vvToolExtractLung);
35 //------------------------------------------------------------------------------
38 //------------------------------------------------------------------------------
39 vvToolExtractLung::vvToolExtractLung(vvMainWindowBase * parent, Qt::WindowFlags f):
40 vvToolWidgetBase(parent,f),
41 vvToolBase<vvToolExtractLung>(parent),
42 Ui::vvToolExtractLung()
45 Ui_vvToolExtractLung::setupUi(mToolWidget);
46 mMaskLoaderBox->setEnabled(false);
47 //mOptionsBox->setEnabled(false);
48 mPatientMaskInputWidget->SetText("Patient mask");
49 connect(mPatientMaskInputWidget, SIGNAL(accepted()), this, SLOT(PatientMaskInputIsSelected()));
52 mArgsInfo = new ArgsInfoType;
53 cmdline_parser_clitkExtractLung_init(mArgsInfo);
54 m_IsThreadInterrupted = false;
56 // Create a new ExtractLung filter
57 mFilter = new FilterType; // used in AddInputSelector
60 AddInputSelector("Select CT thorax image", mFilter);
62 //------------------------------------------------------------------------------
65 //------------------------------------------------------------------------------
66 vvToolExtractLung::~vvToolExtractLung()
69 //------------------------------------------------------------------------------
72 //------------------------------------------------------------------------------
73 void vvToolExtractLung::Initialize() {
74 SetToolName("ExtractLung");
75 SetToolMenuName("Extract lungs");
76 SetToolIconFilename(":/common/icons/lung-overlay.png");
77 SetToolTip("Extract lung mask from thorax CT.");
78 // SetToolExperimental(true);
79 // SetToolInMenu("Segmentation");
81 //------------------------------------------------------------------------------
84 //------------------------------------------------------------------------------
85 void vvToolExtractLung::InputIsSelected(vvSlicerManager *m)
88 HideInputSelector(); // splitter
89 mToolInputSelectionWidget->hide();
90 mCurrentSlicerManager = m;
91 mCurrentImage = m->GetImage();
92 mMaskLoaderBox->setEnabled(true);
93 mLabelInput->setText(m->GetFileName().c_str());
95 //------------------------------------------------------------------------------
98 //------------------------------------------------------------------------------
99 void vvToolExtractLung::PatientMaskInputIsSelected()
101 // Get Patient mask and BG value
102 mPatient = mPatientMaskInputWidget->GetImage();
103 mPatientBackgroundValue = mPatientMaskInputWidget->GetBackgroundValue();
105 // Check patient dimension
106 if (mPatient->GetNumberOfDimensions() != 3) {
107 QMessageBox::information(this,tr("*Error*"), "Mask image must be 3D");
111 mMaskLoaderBox->setEnabled(false);
112 //mOptionsBox->setEnabled(true);
114 //------------------------------------------------------------------------------
117 //------------------------------------------------------------------------------
118 void vvToolExtractLung::GetArgsInfoFromGUI()
120 mArgsInfo->patientBG_arg = 0; //mPatientBackgroundValueSpinBox->value();
121 mArgsInfo->verboseOption_flag = false;
122 mArgsInfo->verboseStep_flag = false;
123 mArgsInfo->writeStep_flag = false;
124 mArgsInfo->input_given = 0;
125 mArgsInfo->patient_given = 0;
126 mArgsInfo->output_given = 0;
127 mArgsInfo->outputTrachea_given = 0;
128 mArgsInfo->remove1_given = 0;
130 //------------------------------------------------------------------------------
133 //------------------------------------------------------------------------------
134 void vvToolExtractLung::apply()
136 // Change cursor to wait
137 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
139 // Read options from GUI and put it in the ArgsInfo struct
140 GetArgsInfoFromGUI();
143 if (mFilter) delete mFilter;
144 mFilter = new FilterType; // needed when thread cancel the filter
145 mFilter->StopOnErrorOff();
146 // mFilter->SetIOVerbose(true);
147 mFilter->AddInputVVImage(mCurrentImage); // CT
148 mFilter->AddInputVVImage(mPatient); // patient mask
149 mFilter->SetArgsInfo(*mArgsInfo);
151 // Created threaded execution
152 vvThreadedFilter thread;
153 connect(&thread, SIGNAL(ThreadInterrupted()), this, SLOT(ThreadInterrupted()));
154 thread.SetFilter(mFilter);
157 // Check if the thread has been canceled. In this case, return
158 if (m_IsThreadInterrupted) {
159 m_IsThreadInterrupted = false;
160 QApplication::restoreOverrideCursor();
163 disconnect(&thread, SIGNAL(ThreadInterrupted()), this, SLOT(ThreadInterrupted()));
165 // Check error during filter
166 if (mFilter->HasError()) {
167 QApplication::restoreOverrideCursor();
168 QMessageBox::information(this,tr("Error"), mFilter->GetLastError().c_str());
173 std::vector<vvImage::Pointer> output = mFilter->GetOutputVVImages();
174 if (output.size() == 0) {
175 std::cerr << "Error : no output ?" << std::endl;
176 QApplication::restoreOverrideCursor();
182 vvImage::Pointer lung = output[0];
183 std::ostringstream osstream;
184 osstream << "Lung_" << mCurrentSlicerManager->GetSlicer(0)->GetFileName() << ".mhd";
185 vvSlicerManager * v = AddImage(lung,osstream.str());
187 vvToolStructureSetManager::AddImage(mCurrentSlicerManager, "Right lung", lung, 1, false); // Right is greater than Left
188 vvToolStructureSetManager::AddImage(mCurrentSlicerManager, "Left lung", lung, 2, false);
190 // Set trachea into VV
191 if (output.size() == 2) {
192 vvImage::Pointer trachea = output[1];
193 std::ostringstream osstream;
194 osstream << "Trachea_" << mCurrentSlicerManager->GetSlicer(0)->GetFileName() << ".mhd";
195 vvSlicerManager * v = AddImage(trachea,osstream.str());
197 vvToolStructureSetManager::AddImage(mCurrentSlicerManager, "Trachea", trachea, 0);
201 QApplication::restoreOverrideCursor();
204 //------------------------------------------------------------------------------
207 //------------------------------------------------------------------------------
208 bool vvToolExtractLung::close()
210 return vvToolWidgetBase::close();
212 //------------------------------------------------------------------------------
215 //------------------------------------------------------------------------------
216 void vvToolExtractLung::ThreadInterrupted()
218 m_IsThreadInterrupted = true;
220 //------------------------------------------------------------------------------