Skip to content

Commit 50eed1b

Browse files
committed
[bugfixes] Automatic segmentation fixed where labels would appear as doubles (e.g 2.99999 instead of 3).
1 parent dd78b2a commit 50eed1b

3 files changed

Lines changed: 48 additions & 2 deletions

File tree

Version2.0/Modules/CemrgAppModule/include/CemrgCommonUtils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@ class MITKCEMRGAPPMODULE_EXPORT CemrgCommonUtils {
5656

5757
//Nifti Conversion Utils
5858
static bool ConvertToNifti(mitk::BaseData::Pointer oneNode, QString path2file, bool resample=false, bool reorient=false);
59+
static void RoundPixelValues(QString pathToImage, QString outputPath="");
5960

6061
//Mesh Utils
6162
static mitk::Surface::Pointer LoadVTKMesh(std::string path);
6263
static mitk::Surface::Pointer ExtractSurfaceFromSegmentation(mitk::Image::Pointer image, double thresh=0.5, double blur=0.8, double smoothIterations=3, double decimation=0.5);
63-
static void FlipXYPlane(mitk::Surface::Pointer surf, QString dir, QString vtkname="segmentation.vtk");
64+
static void FlipXYPlane(mitk::Surface::Pointer surf, QString dir, QString vtkname="segmentation.vtk");
6465
static QString M3dlibParamFileGenerator(QString dir, QString filename="param-template.par", QString thicknessCalc="0");
6566
static void ConvertToCarto(std::string vtkPath, std::vector<double>, double, double, int, bool);
6667
static void CalculatePolyDataNormals(vtkSmartPointer<vtkPolyData>& pd, bool celldata=true);

Version2.0/Modules/CemrgAppModule/src/CemrgCommonUtils.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ PURPOSE. See the above copyright notices for more information.
2929
//ITK
3030
#include <itkNearestNeighborInterpolateImageFunction.h>
3131
#include <itkBSplineInterpolateImageFunction.h>
32+
#include <itkImageRegionIteratorWithIndex.h>
3233
#include <itkResampleImageFilter.h>
3334
#include <itkOrientImageFilter.h>
3435

@@ -76,6 +77,7 @@ PURPOSE. See the above copyright notices for more information.
7677
#include <QMessageBox>
7778
#include <QString>
7879
#include <QFile>
80+
#include <QFileInfo>
7981
#include <QTextStream>
8082
#include "CemrgCommonUtils.h"
8183

@@ -265,6 +267,38 @@ bool CemrgCommonUtils::ConvertToNifti(mitk::BaseData::Pointer oneNode, QString p
265267
return successful;
266268
}
267269

270+
void CemrgCommonUtils::RoundPixelValues(QString pathToImage, QString outputPath){
271+
QFileInfo fi(pathToImage);
272+
if(fi.exists()){
273+
using ImageType = itk::Image<double, 3>;
274+
using IteratorType = itk::ImageRegionIteratorWithIndex<ImageType>;
275+
276+
ImageType::Pointer im = ImageType::New();
277+
mitk::CastToItkImage(mitk::IOUtil::Load<mitk::Image>(pathToImage.toStdString()), im);
278+
279+
IteratorType imIter(im, im->GetLargestPossibleRegion());
280+
281+
imIter.GoToBegin();
282+
while(!imIter.IsAtEnd()){
283+
double pixelValue = imIter.Get();
284+
imIter.Set(std::round(pixelValue));
285+
286+
++imIter;
287+
}
288+
289+
QString writingPath = (outputPath.isEmpty()) ? pathToImage : outputPath;
290+
291+
mitk::Image::Pointer outputImg = mitk::Image::New();
292+
mitk::CastToMitkImage(im, outputImg);
293+
294+
MITK_INFO(outputPath.isEmpty()) << ("Overwriting: " + pathToImage).toStdString();
295+
mitk::IOUtil::Save(outputImg, writingPath.toStdString());
296+
297+
} else{
298+
MITK_WARN << ("Path: " + pathToImage + " does not exist.").toStdString();
299+
}
300+
}
301+
268302

269303
mitk::Surface::Pointer CemrgCommonUtils::LoadVTKMesh(std::string path) {
270304

Version2.0/Plugins/kcl.cemrgapp.scar/src/internal/AtrialScarView.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,13 @@ void AtrialScarView::AutomaticAnalysis() {
466466

467467
timerLog->StartTimer();
468468
if (cnnPath.isEmpty()) {
469+
MITK_INFO << "[AUTOMATIC_ANALYSIS] Computing automatic segmentation step.";
469470
cnnPath = cmd->DockerCemrgNetPrediction(mraPath);
470471
}
471472

473+
MITK_INFO << "Round pixel values from automatic segmentation.";
474+
CemrgCommonUtils::RoundPixelValues(cnnPath);
475+
472476
if (!cnnPath.isEmpty()) {
473477

474478
MITK_INFO << ("Successful prediction with file "+cnnPath).toStdString();
@@ -867,20 +871,27 @@ void AtrialScarView::SegmentIMGS() {
867871
this->BusyCursorOn();
868872
mitk::ProgressBar::GetInstance()->AddStepsToDo(2);
869873

870-
//CNN prediction
874+
MITK_INFO << "CNN prediction";
871875
mraPath = directory + mitk::IOUtil::GetDirectorySeparator() + "test.nii";
872876
mitk::IOUtil::Save(image, mraPath.toStdString());
873877
std::unique_ptr<CemrgCommandLine> cmd(new CemrgCommandLine());
878+
874879
cmd->SetUseDockerContainers(true);
875880
QString cnnPath = cmd->DockerCemrgNetPrediction(mraPath);
881+
882+
MITK_INFO << "Round pixel values from automatic segmentation.";
883+
CemrgCommonUtils::RoundPixelValues(cnnPath);
876884
mitk::ProgressBar::GetInstance()->Progress();
877885

878886
//Clean prediction
879887
using ImageTypeCHAR = itk::Image<short, 3>;
880888
using ConnectedComponentImageFilterType = itk::ConnectedComponentImageFilter<ImageTypeCHAR, ImageTypeCHAR>;
881889
using LabelShapeKeepNObjImgFilterType = itk::LabelShapeKeepNObjectsImageFilter<ImageTypeCHAR>;
890+
882891
ImageTypeCHAR::Pointer orgSegImage = ImageTypeCHAR::New();
883892
mitk::CastToItkImage(mitk::IOUtil::Load<mitk::Image>(cnnPath.toStdString()), orgSegImage);
893+
894+
884895
ConnectedComponentImageFilterType::Pointer connected = ConnectedComponentImageFilterType::New();
885896
connected->SetInput(orgSegImage);
886897
connected->Update();

0 commit comments

Comments
 (0)