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 "vvToolSegmentation.h"
21 #include "vvSlicerManager.h"
23 #include "vvToolInputSelectorWidget.h"
24 #include "vvImageWriter.h"
27 #include <QFileDialog>
28 #include <QMessageBox>
31 #include "vtkImageContinuousErode3D.h"
32 #include "vtkImageContinuousDilate3D.h"
34 //------------------------------------------------------------------------------
35 // Create the tool and automagically (I like this word) insert it in
36 // the main window menu.
37 ADD_TOOL(vvToolSegmentation);
38 //------------------------------------------------------------------------------
41 //------------------------------------------------------------------------------
42 void vvToolSegmentation::Initialize()
44 SetToolName("Segmentation");
45 SetToolMenuName("Interactive Segmentation");
46 SetToolIconFilename(":/common/icons/ducky.ico");
47 SetToolTip("Image interactive segmentation (trial).");
48 SetToolExperimental(true);
50 //------------------------------------------------------------------------------
53 //------------------------------------------------------------------------------
54 vvToolSegmentation::vvToolSegmentation(vvMainWindowBase * parent, Qt::WindowFlags f)
55 :vvToolWidgetBase(parent,f),
56 vvToolBase<vvToolSegmentation>(parent),
57 Ui::vvToolSegmentation()
60 Ui_vvToolSegmentation::setupUi(mToolWidget);
61 setAttribute(Qt::WA_DeleteOnClose);
63 // Set how many inputs are needed for this tool
64 AddInputSelector("Select one image");
67 mKernelValue = 3; // FIXME must be odd. If even -> not symmetrical
69 //------------------------------------------------------------------------------
72 //------------------------------------------------------------------------------
73 vvToolSegmentation::~vvToolSegmentation()
76 mRefMaskActor->RemoveActors();
78 mCurrentSlicerManager->Render();
80 //------------------------------------------------------------------------------
83 //------------------------------------------------------------------------------
84 bool vvToolSegmentation::close()
87 mRefMaskActor->RemoveActors();
89 mCurrentSlicerManager->Render();
92 //------------------------------------------------------------------------------
95 //------------------------------------------------------------------------------
96 void vvToolSegmentation::InputIsSelected(vvSlicerManager * m)
98 DD("InputIsSelected");
99 mCurrentSlicerManager = m;
100 mCurrentImage = mCurrentSlicerManager->GetImage();
102 // Refuse if non 3D image
103 if (mCurrentImage->GetNumberOfDimensions() != 3) {
104 QMessageBox::information(this,tr("Sorry only 3D yet"), tr("Sorry only 3D yet"));
110 //mLabelInputInfo->setText(QString("%1").arg(m->GetFileName().c_str()));
115 //------------------------------------------------------------------------------
118 //------------------------------------------------------------------------------
119 void vvToolSegmentation::apply()
123 //------------------------------------------------------------------------------
126 //------------------------------------------------------------------------------
127 void vvToolSegmentation::OpenBinaryImage()
129 DD("OpenBinaryImage");
131 // Load browser and select image
132 QString Extensions = "Images files ( *.mha *.mhd *.hdr *.his)";
133 Extensions += ";;All Files (*)";
135 QFileDialog::getOpenFileName(this,tr("Open binary image"),
136 mMainWindowBase->GetInputPathName(),Extensions);
137 DD(filename.toStdString());
138 if (filename.size() == 0) return;
141 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
142 vvImageReader::Pointer reader = vvImageReader::New();
143 std::vector<std::string> filenames;
144 filenames.push_back(filename.toStdString());
145 reader->SetInputFilenames(filenames);
146 reader->Update(vvImageReader::IMAGE);
147 QApplication::restoreOverrideCursor();
149 if (reader->GetLastError().size() != 0) {
150 std::cerr << "Error while reading " << filename.toStdString() << std::endl;
151 QString error = "Cannot open file \n";
152 error += reader->GetLastError().c_str();
153 QMessageBox::information(this,tr("Reading problem"),error);
157 mRefMaskImage = reader->GetOutput();
158 int dim = mRefMaskImage->GetNumberOfDimensions();
160 QMessageBox::information(this,tr("Sorry only 3D yet"), tr("Sorry only 3D yet"));
165 reader = vvImageReader::New();
166 reader->SetInputFilenames(filenames);
167 reader->Update(vvImageReader::IMAGE);
168 mCurrentMaskImage = reader->GetOutput();
170 // Add a new roi actor
171 mRefMaskActor = QSharedPointer<vvROIActor>(new vvROIActor);
172 mCurrentMaskActor = QSharedPointer<vvROIActor>(new vvROIActor);
173 std::vector<double> color;
177 clitk::DicomRT_ROI::Pointer roi = clitk::DicomRT_ROI::New();
178 roi->SetFromBinaryImage(mRefMaskImage, 1, std::string("toto"), color, filename.toStdString());
179 mRefMaskActor->SetBGMode(true);
180 mRefMaskActor->SetROI(roi);
181 mRefMaskActor->SetSlicerManager(mCurrentSlicerManager);
182 mRefMaskActor->Initialize(10, true);
183 mRefMaskActor->SetContourVisible(true);
184 mRefMaskActor->SetVisible(false);
185 mRefMaskActor->Update();
187 clitk::DicomRT_ROI::Pointer roi2 = clitk::DicomRT_ROI::New();
188 roi2->SetFromBinaryImage(mCurrentMaskImage, 1, std::string("toto"), color, filename.toStdString());
189 mCurrentMaskActor->SetBGMode(true);
190 mCurrentMaskActor->SetROI(roi2);
191 mCurrentMaskActor->SetSlicerManager(mCurrentSlicerManager);
192 mCurrentMaskActor->Initialize(10, true);
193 mCurrentMaskActor->Update();
195 // Prepare widget to get keyboard event
198 //------------------------------------------------------------------------------
201 //------------------------------------------------------------------------------
202 void vvToolSegmentation::keyPressEvent(QKeyEvent * event)
204 vvToolWidgetBase::keyPressEvent(event);
207 if (event->text() == "e") {
210 if (event->text() == "d") {
211 Dilate(); // FIXME -> extend image BB !!
213 if (event->text() == "s") {
214 vvImageWriter::Pointer writer = vvImageWriter::New();
215 writer->SetOutputFileName("a.mha");
216 writer->SetInput(mCurrentMaskImage);
220 //------------------------------------------------------------------------------
223 //------------------------------------------------------------------------------
224 void vvToolSegmentation::Erode()
226 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
227 vtkImageContinuousErode3D* erode = vtkImageContinuousErode3D::New();
228 erode->SetKernelSize(mKernelValue,mKernelValue,mKernelValue);
229 vtkImageData* image = mCurrentMaskImage->GetVTKImages()[0];
230 erode->SetInput(image);
232 image->DeepCopy(erode->GetOutput());
236 QApplication::restoreOverrideCursor();
238 //------------------------------------------------------------------------------
241 //------------------------------------------------------------------------------
242 void vvToolSegmentation::Dilate()
244 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
245 vtkImageContinuousDilate3D* dilate = vtkImageContinuousDilate3D::New();
246 dilate->SetKernelSize(mKernelValue,mKernelValue,mKernelValue);
247 vtkImageData* image = mCurrentMaskImage->GetVTKImages()[0];
248 dilate->SetInput(image);
250 image->DeepCopy(dilate->GetOutput());
254 QApplication::restoreOverrideCursor();
256 //------------------------------------------------------------------------------
259 //------------------------------------------------------------------------------
260 void vvToolSegmentation::UpdateAndRender()
262 bool visible = mCurrentMaskActor->IsVisible();
263 bool cvisible = mCurrentMaskActor->IsContourVisible();
264 mCurrentMaskActor->SetVisible(false);
265 mCurrentMaskActor->SetContourVisible(false);
266 // mCurrentSlicerManager->Render();
268 //mCurrentMaskActor->RemoveActors();
269 mCurrentMaskActor->UpdateImage();
270 mCurrentMaskActor->SetVisible(visible);
271 mCurrentMaskActor->SetContourVisible(cvisible);
272 mCurrentSlicerManager->Render();
274 //------------------------------------------------------------------------------