Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions CemrgApp/Modules/CemrgAppModule/include/CemrgCommandLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class MITKCEMRGAPPMODULE_EXPORT CemrgCommandLine: public QObject {
QDialog* GetDialog();

//Execute Plugin Specific Functions
QString ExecuteSurf(QString dir, QString segPath, QString morphOperation = "close", int iter = 1, float th = 0.5, int blur = 0, int smth = 10);
QString ExecuteSurf(QString dir, QString segPath, float th = 0.5, int blur = 0, int smth = 10);
QString ExecuteCreateCGALMesh(QString dir, QString outputName, QString paramsFullPath, QString segmentationName = "converted.inr");
void ExecuteTracking(QString dir, QString imgTimes, QString param, QString output = "tsffd.dof");
void ExecuteApplying(QString dir, QString inputMesh, double iniTime, QString dofin, int noFrames, int smooth);
Expand All @@ -58,9 +58,6 @@ class MITKCEMRGAPPMODULE_EXPORT CemrgCommandLine: public QObject {
void ExecuteSimpleTranslation(QString dir, QString sourceMeshP, QString targetMeshP, QString transformFileName = "init.dof", bool transformThePoints = true);

//Execute MIRTK Specific Functions
QString ExecuteMorphologicalOperation(QString operation, QString dir, QString segPath, QString outputPath = "segmentation.s.nii", int iter = 1);
QString ExecuteExtractSurface(QString dir, QString segPath, QString outputPath = "segmentation.vtk", float th = 0.5, int blur = 0);
QString ExecuteSmoothSurface(QString dir, QString segPath, QString outputPath, int smth = 10);
void ExecuteTransformationOnPoints(QString dir, QString meshFullPath, QString outputMeshFullPath, QString transformFileFullPath, double applyingIniTime = -100);
void ExecuteResamplingOnNifti(QString niiFullPath, QString outputNiiFullPath, int isovalue);

Expand Down
2 changes: 1 addition & 1 deletion CemrgApp/Modules/CemrgAppModule/include/CemrgCommonUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class MITKCEMRGAPPMODULE_EXPORT CemrgCommonUtils {

//Mesh Utils
static mitk::Surface::Pointer LoadVTKMesh(std::string path);
static mitk::Surface::Pointer ExtractSurfaceFromSegmentation(mitk::Image::Pointer image, double thresh=0.5, double blur=0.8, double smoothIterations=3, double decimation=0.5);
static mitk::Surface::Pointer ExtractSurfaceFromSegmentation(mitk::Image::Pointer image, double thresh=0.5, double blur=0.8, double smoothIterations=3, double decimation=0.0);
static mitk::Surface::Pointer ClipWithSphere(mitk::Surface::Pointer surface, double x_c, double y_c, double z_c, double radius, QString saveToPath="");
static void FlipXYPlane(mitk::Surface::Pointer surf, QString dir, QString vtkname="segmentation.vtk");
static QString M3dlibParamFileGenerator(QString dir, QString filename="param-template.par", QString thicknessCalc="0");
Expand Down
180 changes: 13 additions & 167 deletions CemrgApp/Modules/CemrgAppModule/src/CemrgCommandLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ PURPOSE. See the above copyright notices for more information.
#include <chrono>
#include <sys/stat.h>
#include "CemrgCommandLine.h"
#include "CemrgCommonUtils.h"

CemrgCommandLine::CemrgCommandLine() {

Expand Down Expand Up @@ -92,32 +93,22 @@ QDialog* CemrgCommandLine::GetDialog() {
****************** Execute Plugin Specific Functions **********************
***************************************************************************/

QString CemrgCommandLine::ExecuteSurf(QString dir, QString segPath, QString morphOperation, int iter, float th, int blur, int smth) {

MITK_INFO << "[ATTENTION] SURFACE CREATION: Close -> Surface -> Smooth";

QString closeOutputPath, surfOutputPath;
QString outAbsolutePath = "ERROR_IN_PROCESSING";
closeOutputPath = ExecuteMorphologicalOperation(morphOperation, dir, segPath, "segmentation.s.nii", iter);
QString CemrgCommandLine::ExecuteSurf(QString dir, QString segPath, float thresh, int blur, int smooth) {
MITK_INFO << "[ATTENTION] SURFACE CREATION: Surface -> Smooth";

// Load input image into memory
QString inputPath = segPath.contains(dir) ? segPath : dir + "/" + segPath;
mitk::ProgressBar::GetInstance()->Progress();
if (QString::compare(closeOutputPath, "ERROR_IN_PROCESSING")!=0) {

surfOutputPath = ExecuteExtractSurface(dir, closeOutputPath, "segmentation.vtk", th, blur);
mitk::ProgressBar::GetInstance()->Progress();
mitk::Image::Pointer inputImage = mitk::IOUtil::Load<mitk::Image>(inputPath.toStdString());

if (QString::compare(surfOutputPath, "ERROR_IN_PROCESSING")!=0) {
// TODO: check semantics of thresh/blur/smooth are identical
mitk::Surface::Pointer smoothedSurface = CemrgCommonUtils::ExtractSurfaceFromSegmentation(inputImage, (double)thresh, (double)blur, (double)smooth);
CemrgCommonUtils::FlipXYPlane(smoothedSurface, "", "");

outAbsolutePath = ExecuteSmoothSurface(dir, surfOutputPath, surfOutputPath, smth);
remove((dir + "/segmentation.s.nii").toStdString().c_str());
mitk::ProgressBar::GetInstance()->Progress();

} else {
mitk::ProgressBar::GetInstance()->Progress();
}//_if
} else {
mitk::ProgressBar::GetInstance()->Progress(2);
}//_if
// Calling code expects a QString of the file path as return value, not the Surface pointer itself.
QString outAbsolutePath = dir + "/segmentation.vtk";
mitk::IOUtil::Save(smoothedSurface, outAbsolutePath.toStdString());
mitk::ProgressBar::GetInstance()->Progress(2);

return outAbsolutePath;
}
Expand Down Expand Up @@ -364,151 +355,6 @@ void CemrgCommandLine::ExecuteSimpleTranslation(QString dir, QString sourceMeshP
****************** Execute MIRTK Specific Functions **********************
***************************************************************************/

QString CemrgCommandLine::ExecuteMorphologicalOperation(QString operation, QString dir, QString segPath, QString outputPath, int iter) {

MITK_INFO << "[ATTENTION] Attempting Pointset transformation.";

QString commandName;

if (QString::compare(operation, "dilate", Qt::CaseInsensitive)==0) {
commandName = "dilate-image";
} else if (QString::compare(operation, "erode", Qt::CaseInsensitive)==0) {
commandName = "erode-image";
} else if (QString::compare(operation, "open", Qt::CaseInsensitive)==0) {
commandName = "open-image";
} else if (QString::compare(operation, "close", Qt::CaseInsensitive)==0) {
commandName = "close-image";
} else {
MITK_ERROR << ("Morphological operation: " + operation + " misspelled or not supported.").toStdString();
return "ERROR_IN_PROCESSING";
}

QString inputImgFullPath, outAbsolutePath;
QString prodPath = dir + "/";

inputImgFullPath = segPath.contains(dir, Qt::CaseSensitive) ? segPath : prodPath + segPath;
outAbsolutePath = outputPath.contains(dir, Qt::CaseSensitive) ? outputPath : prodPath + outputPath;

MITK_INFO << ("[...] OPERATION: " + operation).toStdString();
MITK_INFO << ("[...] INPUT IMAGE: " + inputImgFullPath).toStdString();
MITK_INFO << ("[...] OUTPUT IMAGE: " + outAbsolutePath).toStdString();

MITK_INFO << "Using static MIRTK libraries.";
QString executablePath = QCoreApplication::applicationDirPath() + "/MLib";
QString executableName = executablePath + "/" + commandName;
QDir apathd(executablePath);
QStringList arguments;

if (apathd.exists()) {

process->setWorkingDirectory(executablePath);
arguments << inputImgFullPath;
arguments << outAbsolutePath;
arguments << "-iterations" << QString::number(iter);

} else {
QMessageBox::warning(NULL, "Please check the LOG", "MIRTK libraries not found");
MITK_WARN << "MIRTK libraries not found. Please make sure the MLib folder is inside the directory;\n\t"+
mitk::IOUtil::GetProgramPath();
}//_if

bool successful = ExecuteCommand(executableName, arguments, outAbsolutePath);
if (!successful) {
MITK_WARN << "Local MIRTK libraries did not produce a good outcome.";
return "ERROR_IN_PROCESSING";
} else {
return outAbsolutePath;
}
}

QString CemrgCommandLine::ExecuteExtractSurface(QString dir, QString segPath, QString outputPath,float th, int blur) {

MITK_INFO << "[ATTENTION] Attempting Surface extraction.";

QString commandName = "extract-surface";
QString inputImgFullPath, outAbsolutePath;
QString prodPath = dir + "/";

inputImgFullPath = segPath.contains(dir, Qt::CaseSensitive) ? segPath : prodPath + segPath;
outAbsolutePath = outputPath.contains(dir, Qt::CaseSensitive) ? outputPath : prodPath + outputPath;

MITK_INFO << ("[...] INPUT IMAGE: " + inputImgFullPath).toStdString();
MITK_INFO << ("[...] OUTPUT MESH: " + outAbsolutePath).toStdString();

MITK_INFO << "Using static MIRTK libraries.";
QString executablePath = QCoreApplication::applicationDirPath() + "/MLib";
QString executableName = executablePath + "/" + commandName;
QDir apathd(executablePath);
QStringList arguments;

if (apathd.exists()) {

process->setWorkingDirectory(executablePath);
arguments << inputImgFullPath;
arguments << outAbsolutePath;
arguments << "-isovalue" << QString::number(th);
arguments << "-blur" << QString::number(blur);
arguments << "-ascii";
arguments << "-verbose" << "3";

} else {
QMessageBox::warning(NULL, "Please check the LOG", "MIRTK libraries not found");
MITK_WARN << "MIRTK libraries not found. Please make sure the MLib folder is inside the directory;\n\t"+
mitk::IOUtil::GetProgramPath();
}//_if

bool successful = ExecuteCommand(executableName, arguments, outAbsolutePath);
if (!successful) {
MITK_WARN << "Local MIRTK libraries did not produce a good outcome.";
return "ERROR_IN_PROCESSING";
} else {
return outAbsolutePath;
}
}

QString CemrgCommandLine::ExecuteSmoothSurface(QString dir, QString segPath, QString outputPath, int smth) {

MITK_INFO << "[ATTENTION] Attempting Surface extraction.";

QString commandName = "smooth-surface";
QString inputMeshFullPath, outAbsolutePath;
QString prodPath = dir + "/";

inputMeshFullPath = segPath.contains(dir, Qt::CaseSensitive) ? segPath : prodPath + segPath;
outAbsolutePath = outputPath.contains(dir, Qt::CaseSensitive) ? outputPath : prodPath + outputPath;

MITK_INFO << ("[...] INPUT IMAGE: " + inputMeshFullPath).toStdString();
MITK_INFO << ("[...] OUTPUT MESH: " + outAbsolutePath).toStdString();

MITK_INFO << "Using static MIRTK libraries.";
QString executablePath = QCoreApplication::applicationDirPath() + "/MLib";
QString executableName = executablePath + "/" + commandName;
QDir apathd(executablePath);
QStringList arguments;

if (apathd.exists()) {

process->setWorkingDirectory(executablePath);
arguments << inputMeshFullPath;
arguments << outAbsolutePath;
arguments << "-iterations" << QString::number(smth);
arguments << "-verbose" << "3";

} else {
QMessageBox::warning(NULL, "Please check the LOG", "MIRTK libraries not found");
MITK_WARN << "MIRTK libraries not found. Please make sure the MLib folder is inside the directory;\n\t"+
mitk::IOUtil::GetProgramPath();
}//_if

bool successful = ExecuteCommand(executableName, arguments, outAbsolutePath);
if (!successful) {
MITK_WARN << "Local MIRTK libraries did not produce a good outcome.";
return "ERROR_IN_PROCESSING";
} else {
return outAbsolutePath;
}
}

void CemrgCommandLine::ExecuteTransformationOnPoints(QString dir, QString meshFullPath, QString outputMeshFullPath, QString transformFileFullPath, double applyingIniTime) {

MITK_INFO << "[ATTENTION] Attempting Pointset transformation.";
Expand Down
13 changes: 4 additions & 9 deletions CemrgApp/Modules/CemrgAppModule/test/CemrgCommandLineTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,14 @@ void TestCemrgCommandLine::cleanupTestCase() {

void TestCemrgCommandLine::ExecuteSurf_data() {
QTest::addColumn<QString>("segPath");
QTest::addColumn<QString>("morphOperation");
QTest::addColumn<int>("iterations");
QTest::addColumn<float>("threshold");
QTest::addColumn<int>("blur");
QTest::addColumn<int>("smoothness");
QTest::addColumn<QString>("result");

const array<tuple<QString, QString, int, float, int, int, QString>, 3> surfData { {
{"sphere_initial.nii", "close", 1, 0.5, 0, 10, "/surf_expected_1.vtk"},
{"sphere_shifted.nii", "dilate", 1, 0.5, 0, 10, "/surf_expected_2.vtk"},
{"sphere_initial.nii", "erode", 1, 0.5, 0, 10, "/surf_expected_3.vtk"}
const array<tuple<QString, QString, int, float, int, int, QString>, 2> surfData { {
{"sphere_initial.nii", 0.5, 0, 10, "/surf_expected_1.vtk"},
{"sphere_shifted.nii", 0.5, 0, 10, "/surf_expected_2.vtk"},
} };

for (size_t i = 0; i < surfData.size(); i++)
Expand All @@ -142,14 +139,12 @@ void TestCemrgCommandLine::ExecuteSurf_data() {

void TestCemrgCommandLine::ExecuteSurf() {
QFETCH(QString, segPath);
QFETCH(QString, morphOperation);
QFETCH(int, iterations);
QFETCH(float, threshold);
QFETCH(int, blur);
QFETCH(int, smoothness);
QFETCH(QString, result);

QString surfOutput = cemrgCommandLine->ExecuteSurf(dataPath, segPath, morphOperation, iterations, threshold, blur, smoothness);
QString surfOutput = cemrgCommandLine->ExecuteSurf(dataPath, segPath, threshold, blur, smoothness);
QVERIFY2(EqualFiles(surfOutput, dataPath + result), "The function output is different from the expected output!");
}

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ void AtrialFibresView::AutomaticAnalysis(){
std::unique_ptr<CemrgCommandLine> cmd(new CemrgCommandLine());
cmd->SetUseDockerContainers(true);

cmd->ExecuteSurf(directory, Path("prodClean.nii"), "close", uiMesh_iter, uiMesh_th, uiMesh_bl, uiMesh_smth);
cmd->ExecuteSurf(directory, Path("prodClean.nii"), uiMesh_th, uiMesh_bl, uiMesh_smth);
atrium->ProjectTagsOnExistingSurface(tagAtriumAuto, directory, tagName+".vtk");

MITK_INFO << "[AUTOMATIC_PIPELINE] Add the mesh to storage";
Expand Down Expand Up @@ -673,7 +673,7 @@ void AtrialFibresView::IdentifyPV(){
std::unique_ptr<CemrgCommandLine> cmd(new CemrgCommandLine());
cmd->SetUseDockerContainers(true);

cmd->ExecuteSurf(directory, Path("prodClean.nii"), "close", uiMesh_iter, uiMesh_th, uiMesh_bl, uiMesh_smth);
cmd->ExecuteSurf(directory, Path("prodClean.nii"), uiMesh_th, uiMesh_bl, uiMesh_smth);

//Add the mesh to storage
std::string meshName = segNode->GetName() + "-Mesh";
Expand Down Expand Up @@ -728,7 +728,7 @@ void AtrialFibresView::CreateLabelledMesh(){
std::unique_ptr<CemrgCommandLine> cmd(new CemrgCommandLine());
cmd->SetUseDockerContainers(true);

cmd->ExecuteSurf(directory, Path("prodClean.nii"), "close", uiMesh_iter, uiMesh_th, uiMesh_bl, uiMesh_smth);
cmd->ExecuteSurf(directory, Path("prodClean.nii"), uiMesh_th, uiMesh_bl, uiMesh_smth);
atrium->ProjectTagsOnExistingSurface(pveins, directory, tagName+".vtk");

MITK_INFO << "Add the mesh to storage";
Expand Down Expand Up @@ -1996,7 +1996,6 @@ bool AtrialFibresView::GetUserMeshingInputs(){

if(userHasSetMeshingParams){
QString msg = "The parameters have been set already, change them?\n";
msg += "close iter= " + QString::number(uiMesh_iter) + '\n';
msg += "threshold= " + QString::number(uiMesh_th) + '\n';
msg += "blur= " + QString::number(uiMesh_bl) + '\n';
msg += "smooth iter= " + QString::number(uiMesh_smth);
Expand All @@ -2017,19 +2016,17 @@ bool AtrialFibresView::GetUserMeshingInputs(){
//Act on dialog return code
if (dialogCode == QDialog::Accepted) {

bool ok1, ok2, ok3, ok4;
bool ok1, ok2, ok3;
uiMesh_th= m_UIMeshing.lineEdit_1->text().toDouble(&ok1);
uiMesh_bl= m_UIMeshing.lineEdit_2->text().toDouble(&ok2);
uiMesh_smth= m_UIMeshing.lineEdit_3->text().toDouble(&ok3);
uiMesh_iter= m_UIMeshing.lineEdit_4->text().toDouble(&ok4);

//Set default values
if (!ok1 || !ok2 || !ok3 || !ok4)
if (!ok1 || !ok2 || !ok3)
QMessageBox::warning(NULL, "Attention", "Reverting to default parameters!");
if (!ok1) uiMesh_th=0.5;
if (!ok2) uiMesh_bl=0.0;
if (!ok3) uiMesh_smth=10;
if (!ok4) uiMesh_iter=0;

inputs->deleteLater();
userInputAccepted=true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ protected slots:
bool automaticPipeline, analysisOnLge, resurfaceMesh, userHasSetMeshingParams;

// user-defined parameters
double uiMesh_th, uiMesh_bl, uiMesh_smth, uiMesh_iter;
double uiMesh_th, uiMesh_bl, uiMesh_smth;
double uiRemesh_max, uiRemesh_avrg, uiRemesh_min, uiRemesh_surfcorr;
bool uiRemesh_isscalar, uiRemesh_extractParts, uiRemesh_cleanmesh;
int uiScar_minStep, uiScar_maxStep, uiScar_projectionMethod, uiScar_thresholdMethod, uiFormat_scale;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>222</width>
<height>196</height>
<height>160</height>
</rect>
</property>
<property name="sizePolicy">
Expand All @@ -19,13 +19,13 @@
<property name="minimumSize">
<size>
<width>0</width>
<height>196</height>
<height>160</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>196</height>
<height>160</height>
</size>
</property>
<property name="windowTitle">
Expand All @@ -45,13 +45,6 @@
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_4">
<property name="placeholderText">
<string>'Close' iterations (default=0)</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_1">
<property name="placeholderText">
Expand Down
Loading