Skip to content

Commit

Permalink
Merge pull request #1 from dboun/master
Browse files Browse the repository at this point in the history
Move code from CaPTk
  • Loading branch information
ashishsingh18 authored Mar 27, 2020
2 parents 7cac983 + 62582f3 commit 4716ee7
Show file tree
Hide file tree
Showing 79 changed files with 10,823 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
### Inspired by https://github.com/MIC-DKFZ/MITK-Diffusion/blob/master/CMake/BuildConfigurations/DiffusionAll.cmake
### Basically when selected, the following will turn on automatically

message(STATUS "Configuring Interactive Segmentation default optional dependencies")

# Enable non-optional external dependencies
set(MITK_USE_OpenCV ON CACHE BOOL "" FORCE)

# Enable/disable non-superbuild apps
set(MITK_BUILD_APP_Workbench ON CACHE BOOL "Build the MITK Workbench executable" FORCE)

# Enable/disable non-superbuild plugins
set(MITK_BUILD_org.mitk.gui.qt.segmentation ON CACHE BOOL "Build the org.mitk.gui.qt.segmentation Plugin." FORCE)
set(MITK_BUILD_org.mitk.gui.qt.multilabelsegmentation ON CACHE BOOL "Build the org.mitk.gui.qt.multilabelsegmentation Plugin." FORCE)

# Activate in-application help generation
set(MITK_DOXYGEN_GENERATE_QCH_FILES ON CACHE BOOL "Use doxygen to generate Qt compressed help files for MITK docs" FORCE)
set(BLUEBERRY_USE_QT_HELP ON CACHE BOOL "Enable support for integrating bundle documentation into Qt Help" FORCE)

# Disable console window
set(MITK_SHOW_CONSOLE_WINDOW OFF CACHE BOOL "Use this to enable or disable the console window when starting MITK GUI Applications" FORCE)

# Enable exporting of compile commands (useful for intellisense in vscode etc)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE BOOL "Enable/Disable output of compile commands during generation." FORCE)
1 change: 1 addition & 0 deletions CMake/Findyaml-cpp.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Nothing here
4 changes: 4 additions & 0 deletions CMake/PackageDepends/MITK_yaml-cpp_Config.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
list(APPEND ALL_LIBRARIES ${YAML_CPP_LIBRARIES})
if(YAML_CPP_INCLUDE_DIR)
list(APPEND ALL_INCLUDE_DIRECTORIES ${YAML_CPP_INCLUDE_DIR})
endif()
1 change: 1 addition & 0 deletions CMakeExternals/ExternalProjectList.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mitkFunctionAddExternalProject(NAME yaml-cpp ON DOC "Use YAML Cpp Library")
29 changes: 29 additions & 0 deletions CMakeExternals/yaml-cpp.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
set(proj yaml-cpp)
set(proj_DEPENDENCIES "")

if(MITK_USE_${proj})
set(${proj}_DEPENDS ${proj})

if(DEFINED ${proj}_DIR AND NOT EXISTS ${${proj}_DIR})
message(FATAL_ERROR "${proj}_DIR variable is defined but corresponds to non-existing directory!")
endif()

if(NOT DEFINED ${proj}_DIR)
ExternalProject_Add(${proj}
GIT_REPOSITORY https://github.com/jbeder/yaml-cpp.git
GIT_TAG yaml-cpp-0.6.3
CMAKE_ARGS ${ep_common_args}
CMAKE_CACHE_ARGS ${ep_common_cache_args}
-DGSL_CXX_STANDARD:STRING=${MITK_CXX_STANDARD}
-DGSL_TEST:BOOL=OFF
-DYAML_BUILD_SHARED_LIBS:BOOL=ON
-DYAML_CPP_BUILD_TESTS:BOOL=OFF
CMAKE_CACHE_DEFAULT_ARGS ${ep_common_cache_default_args}
DEPENDS ${proj_DEPENDENCIES}
)

set(${proj}_DIR ${ep_prefix})
else()
mitkMacroEmptyExternalProject(${proj} "${proj_DEPENDENCIES}")
endif()
endif()
12 changes: 12 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
By installing the Cancer and Phenomics Toolkit (CaPTk), the user agrees to the following licenses, which pertain to the code and its different constituents:

1. SBIA Software License - https://www.med.upenn.edu/sbia/software-agreement.html
2. SBIA Non-Commercial Software License (applies to PHI Estimator) - https://www.med.upenn.edu/sbia/software-agreement-non-commercial.html
3. Insight Toolkit License - https://cmake.org/Wiki/ITK/License_Information
4. Visualization Toolkit License - http://www.vtk.org/licensing/
5. Qt Open Source License - https://doc.qt.io/qt-5.10/qtcore-index.html#licenses-and-attributions; our copy can be found at https://github.com/CBICA/qt
6. CC Attribution-ShareAlike for SRI24 data - https://creativecommons.org/licenses/by-sa/3.0/us/legalcode
7. MRIcroGL License - BSD
8. YAML-CPP - MIT License and can be found at https://opensource.org/licenses/MIT
9. Eigen - MPL2 License and can be found at https://www.mozilla.org/en-US/MPL/2.0/
10. MITK - BSD-style license http://www.mitk.org/wiki/License
7 changes: 7 additions & 0 deletions Modules/CaPTkInteractiveSegmentation/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mitk_create_module(
INCLUDE_DIRS PUBLIC include third_party/jsoncpp/include
PACKAGE_DEPENDS ITK Qt5|Core+Widgets PRIVATE yaml-cpp
DEPENDS PUBLIC MitkCore MitkMultilabel #MitkCaPTkCommon
)

add_subdirectory(cmdapps)
7 changes: 7 additions & 0 deletions Modules/CaPTkInteractiveSegmentation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## CaPTkInteractiveSegmentation

Contains the algorithm part of the interactive segmentation.

It uses brief, user-drawn seeds on co-registered input images to produce a segmentation.

For UI look at the respective plugin.
10 changes: 10 additions & 0 deletions Modules/CaPTkInteractiveSegmentation/cmdapps/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
option(BUILD_CaPTkInteractiveSegmentationCmdApp "Build command-line app for CaPTkInteractiveSegmentation" OFF)

if(BUILD_CaPTkInteractiveSegmentationCmdApp)
mitkFunctionCreateCommandLineApp(
NAME CaPTkInteractiveSegmentation
CPP_FILES "${SRC_FILES}" CaPTkInteractiveSegmentation.cpp
PACKAGE_DEPENDS ITK OpenCV Qt5|Core+WebEngineWidgets
DEPENDS MitkCaPTkInteractiveSegmentation
)
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
#include <mitkCommandLineParser.h>
#include <mitkIOUtil.h>
#include <mitkDataStorage.h>
#include <mitkStandaloneDataStorage.h>
#include <mitkImageCast.h>

#include <CaPTkInteractiveSegmentation.h>
#include <GeodesicTrainingSegmentation.h>

// #include <json/json.h>

#include <algorithm>
#include <string>
#include <fstream>
#include <iostream>
#include <memory>

/**Splits a string into a list using a delimiter*/
std::vector<std::string>
split(const std::string &s, char delim) {
std::stringstream ss(s);
std::string item;
std::vector<std::string> elems;
while (std::getline(ss, item, delim)) {
elems.push_back(std::move(item));
}
return elems;
}

/** \brief command-line app for batch processing of images
*
* This command-line app takes a task and and a cohort and runs the algorithm on everything.
*/
int main(int argc, char* argv[])
{
mitkCommandLineParser parser;

/**** Set general information about the command-line app ****/

parser.setCategory("CaPTk Cmd App Category");
parser.setTitle("CaPTk Interactive Segmentation Cmd App");
parser.setContributor("CBICA");
parser.setDescription(
"This command-line app takes a task and cohort and runs the interactive segmentation algorithm on everything.");

// How should arguments be prefixed
parser.setArgumentPrefix("--", "-");

/**** Add arguments. Unless specified otherwise, each argument is optional.
See mitkCommandLineParser::addArgument() for more information. ****/

parser.addArgument(
"images",
"i",
mitkCommandLineParser::String,
"Images",
"Paths to the input images, separated by comma",
us::Any(),
false);

parser.addArgument(
"labels",
"l",
mitkCommandLineParser::String,
"Labels Image",
"Path to the input seeds image.",
us::Any(),
false);

parser.addArgument(
"priorimages",
"p",
mitkCommandLineParser::String,
"Prior Images",
"These are also input images, but are generated by a deep learning algorithm",
us::Any(),
true);

parser.addArgument(
"outputdir",
"o",
mitkCommandLineParser::String,
"Output Directory",
"Where to generate the output",
us::Any(),
false);

// // Add arguments. Unless specified otherwise, each argument is optional.
// // See mitkCommandLineParser::addArgument() for more information.
// parser.addArgument(
// "task",
// "t",
// mitkCommandLineParser::String,
// "Task",
// "JSON file that contains information on how to run this execution.",
// us::Any(),
// false);
// parser.addArgument(
// "cohort",
// "c",
// mitkCommandLineParser::String,
// "Cohort",
// "JSON file that contains information on how to run this execution.",
// us::Any(),
// false);

/**** Parse arguments. This method returns a mapping of long argument names to
their values. ****/

auto parsedArgs = parser.parseArguments(argc, argv);

if (parsedArgs.empty())
return EXIT_FAILURE; // Just exit, usage information was already printed.

if (parsedArgs["task"].Empty() || parsedArgs["cohort"].Empty())
{
MITK_INFO << parser.helpText();
return EXIT_FAILURE;
}

// // Parse, cast and set required arguments
// auto task = us::any_cast<std::string>(parsedArgs["task"]);
// auto cohort = us::any_cast<std::string>(parsedArgs["cohort"]);

auto imagesPaths = us::any_cast<std::string>(parsedArgs["images"]);
auto labelsPath = us::any_cast<std::string>(parsedArgs["labels"]);
auto outputDir = us::any_cast<std::string>(parsedArgs["outputdir"]);

/**** Default values for optional arguments ****/

std::string priorImagesPaths = "";
// // Parse, cast and set optional arguments
if (parsedArgs.end() != parsedArgs.find("priorimages"))
{
priorImagesPaths = us::any_cast<std::string>(parsedArgs["priorimages"]);
}

std::vector<std::string> imagesPathsVector = split(imagesPaths, ',');
std::vector<std::string> priorImagesPathsVector = split(priorImagesPaths, ',');

/**** Run ****/

try
{
std::vector<mitk::Image::Pointer> images;
std::vector<mitk::Image::Pointer> priorImages;
mitk::LabelSetImage::Pointer seeds;

/**** Read input ****/

for (std::string& imagePath : imagesPathsVector)
{
auto image = mitk::IOUtil::Load<mitk::Image>(imagePath);
images.push_back(image);
}

for (auto& priorImagePath : priorImagesPathsVector)
{
auto priorImage = mitk::IOUtil::Load<mitk::Image>(priorImagePath);
priorImages.push_back(priorImage);
}

seeds = mitk::IOUtil::Load<mitk::LabelSetImage>(labelsPath);

// auto algorithm = new CaPTkInteractiveSegmentation(nullptr);
// algorithm->Run(task, cohort);

if (images[0]->GetDimension() == 3)
{
// [ 3D ]

/**** Convert to itk ****/

std::vector<itk::Image<float,3>::Pointer> imagesItk;
std::vector<itk::Image<float,3>::Pointer> priorImagesItk;
typename itk::Image<int,3>::Pointer seedsItk;

for (auto& image : images)
{
typename itk::Image<float, 3>::Pointer imageItk;
mitk::CastToItkImage(image, imageItk);
imagesItk.push_back(imageItk);
}

for (auto& image : priorImages)
{
typename itk::Image<float, 3>::Pointer imageItk;
mitk::CastToItkImage(image, imageItk);
priorImagesItk.push_back(imageItk);
}

mitk::CastToItkImage(seeds, seedsItk);

std::unique_ptr<GeodesicTrainingSegmentation::Coordinator<float,3>> geodesicTraining(
new GeodesicTrainingSegmentation::Coordinator<float,3>()
);
geodesicTraining->SetInputImages(imagesItk);
geodesicTraining->SetExtraInputImagesNotAGDable(priorImagesItk);
geodesicTraining->SetLabels(seedsItk);
geodesicTraining->SetOutputPath(outputDir);
// geodesicTraining->SetNumberOfThreads(16);
// geodesicTraining->SaveOnlyNormalSegmentation(true, "segmentation");
geodesicTraining->SetVerbose(true);

/**** Run algorithm ****/

auto result = geodesicTraining->Execute();

if (result->ok)
{
mitk::Image::Pointer segmNormal;
mitk::CastToMitkImage(result->labelsImage, segmNormal);

mitk::LabelSetImage::Pointer segm = mitk::LabelSetImage::New();

segm->InitializeByLabeledImage(segmNormal);

mitk::IOUtil::Save(segm, outputDir + std::string("/segmentation.nii.gz"));
}
else {
std::cout << "Algorithm finished with internal error\n";
return EXIT_FAILURE;
}
}
else
{
// [ 2D ]

std::cout << "2D not supported yet\n";
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}
catch (const std::exception &e)
{
MITK_ERROR << e.what();
return EXIT_FAILURE;
}
catch (...)
{
MITK_ERROR << "Unexpected error!";
return EXIT_FAILURE;
}
}
Loading

0 comments on commit 4716ee7

Please sign in to comment.