-
Notifications
You must be signed in to change notification settings - Fork 1
/
ApplicationData.cpp
412 lines (336 loc) · 14.5 KB
/
ApplicationData.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//-------------------------------------------------------------------------------------------------------------------
/*! \brief Implementation of class ApplicationData
* \file ApplicationData.cpp
*///-----------------------------------------------------------------------------------------------------------------
#include "ApplicationData.h"
/*---- ITK Includes ----*/
/*---- QT Includes ----*/
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QStringList>
/*---- VTK Includes ----*/
#include <vtkImageFlip.h>
#include <vtkPointData.h>
#include <vtkShortArray.h>
#include <vtkUnsignedCharArray.h>
#include <itkImageFileReader.h>
#include <itkGDCMImageIO.h>
#include <itkImageRegionConstIterator.h>
#include <itkBinaryThresholdImageFilter.h>
#include <vtkMarchingCubes.h>
#include <vtkDecimatePro.h>
#include <vtkSTLWriter.h>
#include <vtkBooleanOperationPolyDataFilter.h>
#include <itkConnectedComponentImageFilter.h>
#include <itkRelabelComponentImageFilter.h>
//Initialize static variable
ApplicationData * ApplicationData::m_instance = 0;
//-----------------------------------------------------------------------------------------------------------------
ApplicationData::ApplicationData() : QObject(),
m_scapula(vtkPolyData::New()), m_humerus(vtkPolyData::New())
//-----------------------------------------------------------------------------------------------------------------
{
}
//-----------------------------------------------------------------------------------------------------------------
ApplicationData::~ApplicationData()
//-----------------------------------------------------------------------------------------------------------------
{
m_scapula->Delete();
m_humerus->Delete();
}
//-----------------------------------------------------------------------------------------------------------------
bool ApplicationData::loadDirectory(const QString & p_dirPath)
//-----------------------------------------------------------------------------------------------------------------
{
emit progressStarted();
//reset previous saved state
m_rawImages.clear();
m_rawImagesLabelerCounter.clear();
//Save path
m_openingPath = p_dirPath;
//List files in a directory
QDir dir(p_dirPath);
//Create the list of string to store files path
QStringList filesList;
//Set the appropriate filter (we only want Files) and sorting procedure (by name).
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks | QDir::Readable);
dir.setSorting(QDir::Name);
//List all files
const QFileInfoList list = dir.entryInfoList();
emit progressNewStep("Listing files", list.size());
for (int i = 0 ; i < list.size() ; ++i)
{
//Check if it is a readable file
if ( (list[i].isFile()) && (list[i].isReadable()) )
filesList.push_back(list[i].absoluteFilePath());
emit progressUpdate(i + 1);
}
qDebug() << filesList.size();
//filesList contains all the files found in the directory
//Read Dicom Image
emit progressNewStep("Reading DICOM image files", filesList.size());
double positionZ = 0.;
for(int i = 0 ; i < filesList.size() ; ++i)
{
//try to read the ITK file and save the image
// qDebug() << "Reading image " << i + 1 << "with path:" << filesList[i];
itk::ImageFileReader<UShortImageType>::Pointer reader = itk::ImageFileReader<UShortImageType>::New();
itk::GDCMImageIO::Pointer gdcmImageIO = itk::GDCMImageIO::New();
try
{
reader->SetFileName(filesList[i].toStdString().c_str());
reader->SetImageIO(gdcmImageIO);
reader->Update();
m_rawImages.append(reader->GetOutput());
//For the first two images, we get the image position (0020, 0032).
//From the difference, we compute the spacingZ
if(i <= 1)
{
std::string currentPosition;
gdcmImageIO->GetValueFromTag(std::string("0020|0032"), currentPosition);
const double currentPositionZ = QString(currentPosition.c_str()).trimmed().split("\\").last().toDouble();
qDebug() << QString(currentPosition.c_str()) << positionZ;
if(i == 1)
{
m_spacingZ = currentPositionZ - positionZ;
}
else positionZ = currentPositionZ;
}
}
//catch error in case we try to read a non dicom file
catch(...)
{
qDebug() << "Skipping not valid DICOM image file: " << filesList[i].toStdString().c_str();
}
emit progressUpdate(i + 1);
}
//if no DICOM image found, returning false
if(m_rawImages.size() == 0)
{
emit progressStopped();
return false;
}
emit progressNewStep("Computing metadatas", 1);
//Get dimensionX/Y
m_dimensionX = m_rawImages.first()->GetLargestPossibleRegion().GetSize()[0];
m_dimensionY = m_rawImages.first()->GetLargestPossibleRegion().GetSize()[1];
//Get spacing X/Y
m_spacingX = m_rawImages.first()->GetSpacing()[0];
m_spacingY = m_rawImages.first()->GetSpacing()[1];
//Get dimensionZ
m_dimensionZ = m_rawImages.size();
qDebug() << "dim x" << m_dimensionX;
qDebug() << "dim y" << m_dimensionY;
qDebug() << "dim z" << m_dimensionZ;
qDebug() << "spa x" << m_spacingX;
qDebug() << "spa y" << m_spacingY;
qDebug() << "spa z" << m_spacingZ;
emit progressUpdate(1);
//Build the vtkImageData containing the raw volume
emit progressNewStep("Building raw volume", m_rawImages.size());
m_rawVTKData = vtkSmartPointer<vtkImageData>::New();
m_rawVTKData->SetDimensions(m_dimensionX, m_dimensionY, m_dimensionZ);
m_rawVTKData->SetSpacing(m_spacingX, m_spacingY, m_spacingZ);
m_rawVTKData->SetOrigin(0, 0, 0);
m_rawVTKData->SetExtent(0, m_dimensionX - 1, 0, m_dimensionY - 1, 0, m_dimensionZ - 1);
//Create scalars to fill the vtkImageData
vtkSmartPointer<vtkShortArray> scalars = vtkSmartPointer<vtkShortArray>::New();
scalars->SetNumberOfValues(m_dimensionX * m_dimensionY * m_dimensionZ);
int offset = 0;
//Iterate over all images and fill the scalars
for(int z = 0 ; z < m_rawImages.size() ; ++z)
{
//Create const iterator on each image
auto image = m_rawImages[z];
itk::ImageRegionConstIterator<UShortImageType> globalIterator(image, image->GetLargestPossibleRegion());
globalIterator.GoToBegin();
//In the while, set the value of each pixel to the scalars using offset
while(! globalIterator.IsAtEnd())
{
scalars->SetValue(offset, globalIterator.Get());
offset++;
++globalIterator;
}
emit progressUpdate(z + 1);
}
m_rawVTKData->GetPointData()->SetScalars(scalars);
//Flip image along Y to respect difference of referentiel between ITK/VTK display
emit progressNewStep("Flipping along Y axes", 1);
vtkSmartPointer<vtkImageFlip> flipper = vtkSmartPointer<vtkImageFlip>::New();
flipper->SetFilteredAxis(1);//car l'origine n'est pas la même dans itk et vtk
flipper->SetInputData(m_rawVTKData);
flipper->Update();
m_rawVTKData = flipper->GetOutput();
emit progressUpdate(1);
emit progressStopped();
return true;
}
//-----------------------------------------------------------------------------------------------------------------
void ApplicationData::segmentData()
//-----------------------------------------------------------------------------------------------------------------
{
emit progressStarted();
//Segment from 1 to 2 for Scapula
m_scapula->Delete();
m_scapula = segmentData(2, 2);
int scapulaSize = m_scapula->GetPointData()->GetScalars()->GetSize();
emit progressStopped();
//Segment from 0 to 1 for Humerus
emit progressStarted();
m_humerus->Delete();
m_humerus = segmentData(1, 1);
int humerusSize = m_humerus->GetPointData()->GetScalars()->GetSize();
//In DICOM images, sometimes scapula and humerus have their color inverted.
//As the scapula is usually bigger than humerus, we invert them using their size (in point number)
if(scapulaSize < humerusSize)
{
vtkPolyData* temp = m_scapula;
m_scapula = m_humerus;
m_humerus = temp;
}
emit progressStopped();
}
//-----------------------------------------------------------------------------------------------------------------
void ApplicationData::countObjectInSlices()
//-----------------------------------------------------------------------------------------------------------------
{
emit progressStarted();
emit progressNewStep("Labeling slices", m_rawImages.size());
for(int z = 0 ; z < m_rawImages.size() ; ++z)
{
//Create const iterator on each image
auto image = m_rawImages[z];
itk::ImageRegionConstIterator<UShortImageType> globalIterator(image, image->GetLargestPossibleRegion());
itk::BinaryThresholdImageFilter<UShortImageType, UCharImageType>::Pointer thresholder =
itk::BinaryThresholdImageFilter<UShortImageType, UCharImageType>::New();
thresholder->SetInput(image);
thresholder->SetLowerThreshold(1);
thresholder->SetUpperThreshold(2);
thresholder->SetInsideValue(255);
thresholder->SetOutsideValue(0);
thresholder->Update();
//Labelize the image
itk::ConnectedComponentImageFilter<UCharImageType, IntImageType>::Pointer labeler =
itk::ConnectedComponentImageFilter<UCharImageType, IntImageType>::New();
labeler->SetInput(thresholder->GetOutput());
labeler->Update();
//Relabelize the image to get info about the group of found pixels
itk::RelabelComponentImageFilter<IntImageType, UCharImageType>::Pointer relabeler =
itk::RelabelComponentImageFilter<IntImageType, UCharImageType>::New();
relabeler->SetInput(labeler->GetOutput());
relabeler->SetMinimumObjectSize(100);
relabeler->Update();
m_rawImagesLabelerCounter.append(relabeler->GetNumberOfObjects());
emit progressUpdate(z + 1);
}
emit progressStopped();
}
void ApplicationData::save3dModels(const QString directorySavePath)
{
m_savingPath = directorySavePath;
emit progressStarted();
emit progressNewStep("Saving 3d model of humerus scapula both", 4);
vtkSmartPointer<vtkSTLWriter> writer = vtkSmartPointer<vtkSTLWriter>::New();
writer->SetInputData(m_humerus);
writer->SetFileName((directorySavePath + "/meshHumerus.stl").toStdString().c_str());
writer->Write();
emit progressUpdate(1);
writer->SetInputData(m_scapula);
writer->SetFileName((directorySavePath + "/meshScapula.stl").toStdString().c_str());
writer->Write();
emit progressUpdate(2);
//Merging both scapula and humerus in one model
vtkSmartPointer<vtkBooleanOperationPolyDataFilter> booleanFilter =
vtkSmartPointer<vtkBooleanOperationPolyDataFilter>::New();
booleanFilter->SetInputData(0, m_scapula);
booleanFilter->SetInputData(1, m_humerus);
booleanFilter->SetOperationToUnion();
booleanFilter->Update();
emit progressUpdate(3);
writer->SetInputData(booleanFilter->GetOutput());
writer->SetFileName((directorySavePath + "/meshHumerusScapula.stl").toStdString().c_str());
writer->Write();
emit progressUpdate(4);
emit progressStopped();
}
//-----------------------------------------------------------------------------------------------------------------
vtkPolyData * ApplicationData::segmentData(const int p_lowerThreshold, const int p_upperThreshold) const
//-----------------------------------------------------------------------------------------------------------------
{
QList<UCharImageType::Pointer> binaryImages;
binaryImages.clear();
//Apply threshold on each raw images
emit progressNewStep("Applying 2d threshold", m_rawImages.size());
//Same as ITKReader with itk::BinaryThresholdImageFilter
//See Exo6 ITK
itk::BinaryThresholdImageFilter<ShortImageType, UCharImageType>::Pointer thresholder =
itk::BinaryThresholdImageFilter<ShortImageType, UCharImageType>::New();
for(int i = 0 ; i < m_rawImages.size() ; ++i)
{
thresholder->SetInput(m_rawImages[i]);
thresholder->SetLowerThreshold(p_lowerThreshold);
thresholder->SetUpperThreshold(p_upperThreshold);
thresholder->SetInsideValue(255);
thresholder->SetOutsideValue(0);
thresholder->Update();
binaryImages.append(thresholder->GetOutput());
thresholder->GetOutput()->DisconnectPipeline();//cleaning thresholder input (to avoid allocating a new one ech loop iteration
emit progressUpdate(i + 1);
}
//Build Image Data for input of marching cubes
emit progressNewStep("Building 3d model: creating scalars", binaryImages.size());
vtkSmartPointer<vtkImageData> marchingCubesRawVTKData =
vtkSmartPointer<vtkImageData>::New();
marchingCubesRawVTKData->SetDimensions(m_dimensionX, m_dimensionY, m_dimensionZ);
marchingCubesRawVTKData->SetSpacing(m_spacingX, m_spacingY, m_spacingZ);
marchingCubesRawVTKData->SetOrigin(0, 0, 0);
marchingCubesRawVTKData->SetExtent(0, m_dimensionX - 1, 0, m_dimensionY - 1, 0, m_dimensionZ - 1);
//Create scalars to fill the vtkImageData
vtkSmartPointer<vtkShortArray> scalars = vtkSmartPointer<vtkShortArray>::New();
scalars->SetNumberOfValues(m_dimensionX * m_dimensionY * m_dimensionZ);
int offset = 0;
//Iterate over all images and fill the scalars
//for on binary images
for(int z = 0 ; z < binaryImages.size() ; ++z)
{
//Same as before with m_rawImages and m_rawData
auto image = binaryImages[z];
itk::ImageRegionConstIterator<UCharImageType> globalIterator(image, image->GetLargestPossibleRegion());
globalIterator.GoToBegin();
while(! globalIterator.IsAtEnd())
{
scalars->SetValue(offset, globalIterator.Get());
offset++;
++globalIterator;
}
emit progressUpdate(z + 1);
}
//computing 3d model from 2d slices
marchingCubesRawVTKData->GetPointData()->SetScalars(scalars);
//Apply the marching cubes
emit progressNewStep("Building 3d model: applying marching cubes", 1);
//vtkMarchingCubes
//See Exo12 VTK
vtkSmartPointer<vtkMarchingCubes> marchingcubes =
vtkSmartPointer<vtkMarchingCubes>::New();
marchingcubes->SetInputData(marchingCubesRawVTKData);
marchingcubes->SetValue(0, 1);
marchingcubes->Update();//création d'un mesh en 3d à partir des img 3d
emit progressUpdate(1);
qDebug() << "Before decimater:" << marchingcubes->GetOutput()->GetNumberOfPoints();
//Reducing point number
emit progressNewStep("Building 3d model: applying decimater", 1);
vtkSmartPointer<vtkDecimatePro> decimater =
vtkSmartPointer<vtkDecimatePro>::New();
decimater->SetInputData(marchingcubes->GetOutput());
decimater->SetTargetReduction(0.7);
decimater->Update();//supression du maximum de triagle coplanaire (ds le meme plan)
qDebug() << "After decimater: " << decimater->GetOutput()->GetNumberOfPoints();
emit progressUpdate(1);
//Return resulting mesh after copy
vtkPolyData * mesh = vtkPolyData::New();
mesh->DeepCopy(decimater->GetOutput());
emit progressStopped();
return mesh;
}