]> Creatis software - clitk.git/blob - vv/vvToolSegmentation.cxx
Add reference contour
[clitk.git] / vv / vvToolSegmentation.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
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
8
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.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
18
19 // vv
20 #include "vvToolSegmentation.h"
21 #include "vvSlicerManager.h"
22 #include "vvSlicer.h"
23 #include "vvToolInputSelectorWidget.h"
24 #include "vvImageWriter.h"
25
26 // Qt
27 #include <QFileDialog>
28 #include <QMessageBox>
29
30 // vtk
31 #include "vtkImageContinuousErode3D.h"
32 #include "vtkImageContinuousDilate3D.h"
33  
34 //------------------------------------------------------------------------------
35 // Create the tool and automagically (I like this word) insert it in
36 // the main window menu.
37 ADD_TOOL(vvToolSegmentation);
38 //------------------------------------------------------------------------------
39
40
41 //------------------------------------------------------------------------------
42 void vvToolSegmentation::Initialize()
43 {
44   SetToolName("Segmentation");
45   SetToolMenuName("Interactive Segmentation");
46   SetToolIconFilename(":/common/icons/ducky.ico");
47   SetToolTip("Image interactive segmentation (trial).");
48   SetToolExperimental(true);
49 }
50 //------------------------------------------------------------------------------
51
52
53 //------------------------------------------------------------------------------
54 vvToolSegmentation::vvToolSegmentation(vvMainWindowBase * parent, Qt::WindowFlags f)
55   :vvToolWidgetBase(parent,f),
56    vvToolBase<vvToolSegmentation>(parent),
57    Ui::vvToolSegmentation()
58 {
59   // GUI Initialization
60   Ui_vvToolSegmentation::setupUi(mToolWidget);
61   setAttribute(Qt::WA_DeleteOnClose);
62
63   // Set how many inputs are needed for this tool
64   AddInputSelector("Select one image");
65   
66   // Init
67   mKernelValue = 3; // FIXME must be odd. If even -> not symmetrical
68 }
69 //------------------------------------------------------------------------------
70
71
72 //------------------------------------------------------------------------------
73 vvToolSegmentation::~vvToolSegmentation()
74 {
75   DD("destructor");
76   mRefMaskActor->RemoveActors();
77   QWidget::close();  
78   mCurrentSlicerManager->Render();
79 }
80 //------------------------------------------------------------------------------
81
82
83 //------------------------------------------------------------------------------
84 bool vvToolSegmentation::close()
85 {
86   DD("close");
87   mRefMaskActor->RemoveActors();
88   QWidget::close();  
89   mCurrentSlicerManager->Render();
90   return true;
91 }
92 //------------------------------------------------------------------------------
93
94
95 //------------------------------------------------------------------------------
96 void vvToolSegmentation::InputIsSelected(vvSlicerManager * m)
97 {
98   DD("InputIsSelected");
99   mCurrentSlicerManager = m;
100   mCurrentImage = mCurrentSlicerManager->GetImage();
101
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"));
105     close();
106     return;
107   }
108
109   // Change gui
110   //mLabelInputInfo->setText(QString("%1").arg(m->GetFileName().c_str()));
111
112   // Open mask
113   OpenBinaryImage();
114 }
115 //------------------------------------------------------------------------------
116
117
118 //------------------------------------------------------------------------------
119 void vvToolSegmentation::apply()
120 {
121   DD("apply");
122 }
123 //------------------------------------------------------------------------------
124
125
126 //------------------------------------------------------------------------------
127 void vvToolSegmentation::OpenBinaryImage()
128 {
129   DD("OpenBinaryImage");
130
131   // Load browser and select image
132   QString Extensions = "Images files ( *.mha *.mhd *.hdr *.his)";
133   Extensions += ";;All Files (*)";
134   QString filename =
135     QFileDialog::getOpenFileName(this,tr("Open binary image"),
136                                  mMainWindowBase->GetInputPathName(),Extensions);
137   DD(filename.toStdString());
138   if (filename.size() == 0) return;
139   
140   // Open Image
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();
148   
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);
154     return;
155   }
156
157   mRefMaskImage = reader->GetOutput();
158   int dim = mRefMaskImage->GetNumberOfDimensions();
159   if (dim != 3 ) {
160     QMessageBox::information(this,tr("Sorry only 3D yet"), tr("Sorry only 3D yet"));
161     close();
162     return;
163   }
164
165   reader = vvImageReader::New();
166   reader->SetInputFilenames(filenames);
167   reader->Update(vvImageReader::IMAGE);
168   mCurrentMaskImage = reader->GetOutput();
169
170   // Add a new roi actor
171   mRefMaskActor = QSharedPointer<vvROIActor>(new vvROIActor);
172   mCurrentMaskActor = QSharedPointer<vvROIActor>(new vvROIActor);
173   std::vector<double> color;
174   color.push_back(1);
175   color.push_back(0);
176   color.push_back(0);
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();
186
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();
194
195   // Prepare widget to get keyboard event
196   grabKeyboard();  
197 }
198 //------------------------------------------------------------------------------
199
200
201 //------------------------------------------------------------------------------
202 void vvToolSegmentation::keyPressEvent(QKeyEvent * event)
203 {
204   vvToolWidgetBase::keyPressEvent(event);
205   //DD("key");
206   
207   if (event->text() == "e") {
208     Erode();
209   }
210   if (event->text() == "d") {
211     Dilate(); // FIXME -> extend image BB !!
212   }
213   if (event->text() == "s") {
214     vvImageWriter::Pointer writer = vvImageWriter::New();
215     writer->SetOutputFileName("a.mha");
216     writer->SetInput(mCurrentMaskImage);
217     writer->Update();
218   }
219 }
220 //------------------------------------------------------------------------------
221
222
223 //------------------------------------------------------------------------------
224 void vvToolSegmentation::Erode()
225 {
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);
231   erode->Update();
232   image->DeepCopy(erode->GetOutput());
233   image->Update();
234   UpdateAndRender();
235   erode->Delete();
236   QApplication::restoreOverrideCursor();
237 }
238 //------------------------------------------------------------------------------
239
240
241 //------------------------------------------------------------------------------
242 void vvToolSegmentation::Dilate()
243 {
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);
249   dilate->Update();
250   image->DeepCopy(dilate->GetOutput());
251   image->Update();
252   UpdateAndRender();
253   dilate->Delete();
254   QApplication::restoreOverrideCursor();
255 }
256 //------------------------------------------------------------------------------
257
258
259 //------------------------------------------------------------------------------
260 void vvToolSegmentation::UpdateAndRender()
261 {
262   bool visible = mCurrentMaskActor->IsVisible();
263   bool cvisible = mCurrentMaskActor->IsContourVisible();
264   mCurrentMaskActor->SetVisible(false);
265   mCurrentMaskActor->SetContourVisible(false);
266   // mCurrentSlicerManager->Render();
267
268   //mCurrentMaskActor->RemoveActors();
269   mCurrentMaskActor->UpdateImage();
270   mCurrentMaskActor->SetVisible(visible);
271   mCurrentMaskActor->SetContourVisible(cvisible);
272   mCurrentSlicerManager->Render();
273 }
274 //------------------------------------------------------------------------------