Skip to content

Commit 62582f3

Browse files
committed
Move code from CaPTk
1 parent 7cac983 commit 62582f3

File tree

79 files changed

+10823
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+10823
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
### Inspired by https://github.com/MIC-DKFZ/MITK-Diffusion/blob/master/CMake/BuildConfigurations/DiffusionAll.cmake
2+
### Basically when selected, the following will turn on automatically
3+
4+
message(STATUS "Configuring Interactive Segmentation default optional dependencies")
5+
6+
# Enable non-optional external dependencies
7+
set(MITK_USE_OpenCV ON CACHE BOOL "" FORCE)
8+
9+
# Enable/disable non-superbuild apps
10+
set(MITK_BUILD_APP_Workbench ON CACHE BOOL "Build the MITK Workbench executable" FORCE)
11+
12+
# Enable/disable non-superbuild plugins
13+
set(MITK_BUILD_org.mitk.gui.qt.segmentation ON CACHE BOOL "Build the org.mitk.gui.qt.segmentation Plugin." FORCE)
14+
set(MITK_BUILD_org.mitk.gui.qt.multilabelsegmentation ON CACHE BOOL "Build the org.mitk.gui.qt.multilabelsegmentation Plugin." FORCE)
15+
16+
# Activate in-application help generation
17+
set(MITK_DOXYGEN_GENERATE_QCH_FILES ON CACHE BOOL "Use doxygen to generate Qt compressed help files for MITK docs" FORCE)
18+
set(BLUEBERRY_USE_QT_HELP ON CACHE BOOL "Enable support for integrating bundle documentation into Qt Help" FORCE)
19+
20+
# Disable console window
21+
set(MITK_SHOW_CONSOLE_WINDOW OFF CACHE BOOL "Use this to enable or disable the console window when starting MITK GUI Applications" FORCE)
22+
23+
# Enable exporting of compile commands (useful for intellisense in vscode etc)
24+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE BOOL "Enable/Disable output of compile commands during generation." FORCE)

CMake/Findyaml-cpp.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Nothing here
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
list(APPEND ALL_LIBRARIES ${YAML_CPP_LIBRARIES})
2+
if(YAML_CPP_INCLUDE_DIR)
3+
list(APPEND ALL_INCLUDE_DIRECTORIES ${YAML_CPP_INCLUDE_DIR})
4+
endif()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mitkFunctionAddExternalProject(NAME yaml-cpp ON DOC "Use YAML Cpp Library")

CMakeExternals/yaml-cpp.cmake

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
set(proj yaml-cpp)
2+
set(proj_DEPENDENCIES "")
3+
4+
if(MITK_USE_${proj})
5+
set(${proj}_DEPENDS ${proj})
6+
7+
if(DEFINED ${proj}_DIR AND NOT EXISTS ${${proj}_DIR})
8+
message(FATAL_ERROR "${proj}_DIR variable is defined but corresponds to non-existing directory!")
9+
endif()
10+
11+
if(NOT DEFINED ${proj}_DIR)
12+
ExternalProject_Add(${proj}
13+
GIT_REPOSITORY https://github.com/jbeder/yaml-cpp.git
14+
GIT_TAG yaml-cpp-0.6.3
15+
CMAKE_ARGS ${ep_common_args}
16+
CMAKE_CACHE_ARGS ${ep_common_cache_args}
17+
-DGSL_CXX_STANDARD:STRING=${MITK_CXX_STANDARD}
18+
-DGSL_TEST:BOOL=OFF
19+
-DYAML_BUILD_SHARED_LIBS:BOOL=ON
20+
-DYAML_CPP_BUILD_TESTS:BOOL=OFF
21+
CMAKE_CACHE_DEFAULT_ARGS ${ep_common_cache_default_args}
22+
DEPENDS ${proj_DEPENDENCIES}
23+
)
24+
25+
set(${proj}_DIR ${ep_prefix})
26+
else()
27+
mitkMacroEmptyExternalProject(${proj} "${proj_DEPENDENCIES}")
28+
endif()
29+
endif()

LICENSE

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
By installing the Cancer and Phenomics Toolkit (CaPTk), the user agrees to the following licenses, which pertain to the code and its different constituents:
2+
3+
1. SBIA Software License - https://www.med.upenn.edu/sbia/software-agreement.html
4+
2. SBIA Non-Commercial Software License (applies to PHI Estimator) - https://www.med.upenn.edu/sbia/software-agreement-non-commercial.html
5+
3. Insight Toolkit License - https://cmake.org/Wiki/ITK/License_Information
6+
4. Visualization Toolkit License - http://www.vtk.org/licensing/
7+
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
8+
6. CC Attribution-ShareAlike for SRI24 data - https://creativecommons.org/licenses/by-sa/3.0/us/legalcode
9+
7. MRIcroGL License - BSD
10+
8. YAML-CPP - MIT License and can be found at https://opensource.org/licenses/MIT
11+
9. Eigen - MPL2 License and can be found at https://www.mozilla.org/en-US/MPL/2.0/
12+
10. MITK - BSD-style license http://www.mitk.org/wiki/License
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
mitk_create_module(
2+
INCLUDE_DIRS PUBLIC include third_party/jsoncpp/include
3+
PACKAGE_DEPENDS ITK Qt5|Core+Widgets PRIVATE yaml-cpp
4+
DEPENDS PUBLIC MitkCore MitkMultilabel #MitkCaPTkCommon
5+
)
6+
7+
add_subdirectory(cmdapps)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## CaPTkInteractiveSegmentation
2+
3+
Contains the algorithm part of the interactive segmentation.
4+
5+
It uses brief, user-drawn seeds on co-registered input images to produce a segmentation.
6+
7+
For UI look at the respective plugin.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
option(BUILD_CaPTkInteractiveSegmentationCmdApp "Build command-line app for CaPTkInteractiveSegmentation" OFF)
2+
3+
if(BUILD_CaPTkInteractiveSegmentationCmdApp)
4+
mitkFunctionCreateCommandLineApp(
5+
NAME CaPTkInteractiveSegmentation
6+
CPP_FILES "${SRC_FILES}" CaPTkInteractiveSegmentation.cpp
7+
PACKAGE_DEPENDS ITK OpenCV Qt5|Core+WebEngineWidgets
8+
DEPENDS MitkCaPTkInteractiveSegmentation
9+
)
10+
endif()
Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
#include <mitkCommandLineParser.h>
2+
#include <mitkIOUtil.h>
3+
#include <mitkDataStorage.h>
4+
#include <mitkStandaloneDataStorage.h>
5+
#include <mitkImageCast.h>
6+
7+
#include <CaPTkInteractiveSegmentation.h>
8+
#include <GeodesicTrainingSegmentation.h>
9+
10+
// #include <json/json.h>
11+
12+
#include <algorithm>
13+
#include <string>
14+
#include <fstream>
15+
#include <iostream>
16+
#include <memory>
17+
18+
/**Splits a string into a list using a delimiter*/
19+
std::vector<std::string>
20+
split(const std::string &s, char delim) {
21+
std::stringstream ss(s);
22+
std::string item;
23+
std::vector<std::string> elems;
24+
while (std::getline(ss, item, delim)) {
25+
elems.push_back(std::move(item));
26+
}
27+
return elems;
28+
}
29+
30+
/** \brief command-line app for batch processing of images
31+
*
32+
* This command-line app takes a task and and a cohort and runs the algorithm on everything.
33+
*/
34+
int main(int argc, char* argv[])
35+
{
36+
mitkCommandLineParser parser;
37+
38+
/**** Set general information about the command-line app ****/
39+
40+
parser.setCategory("CaPTk Cmd App Category");
41+
parser.setTitle("CaPTk Interactive Segmentation Cmd App");
42+
parser.setContributor("CBICA");
43+
parser.setDescription(
44+
"This command-line app takes a task and cohort and runs the interactive segmentation algorithm on everything.");
45+
46+
// How should arguments be prefixed
47+
parser.setArgumentPrefix("--", "-");
48+
49+
/**** Add arguments. Unless specified otherwise, each argument is optional.
50+
See mitkCommandLineParser::addArgument() for more information. ****/
51+
52+
parser.addArgument(
53+
"images",
54+
"i",
55+
mitkCommandLineParser::String,
56+
"Images",
57+
"Paths to the input images, separated by comma",
58+
us::Any(),
59+
false);
60+
61+
parser.addArgument(
62+
"labels",
63+
"l",
64+
mitkCommandLineParser::String,
65+
"Labels Image",
66+
"Path to the input seeds image.",
67+
us::Any(),
68+
false);
69+
70+
parser.addArgument(
71+
"priorimages",
72+
"p",
73+
mitkCommandLineParser::String,
74+
"Prior Images",
75+
"These are also input images, but are generated by a deep learning algorithm",
76+
us::Any(),
77+
true);
78+
79+
parser.addArgument(
80+
"outputdir",
81+
"o",
82+
mitkCommandLineParser::String,
83+
"Output Directory",
84+
"Where to generate the output",
85+
us::Any(),
86+
false);
87+
88+
// // Add arguments. Unless specified otherwise, each argument is optional.
89+
// // See mitkCommandLineParser::addArgument() for more information.
90+
// parser.addArgument(
91+
// "task",
92+
// "t",
93+
// mitkCommandLineParser::String,
94+
// "Task",
95+
// "JSON file that contains information on how to run this execution.",
96+
// us::Any(),
97+
// false);
98+
// parser.addArgument(
99+
// "cohort",
100+
// "c",
101+
// mitkCommandLineParser::String,
102+
// "Cohort",
103+
// "JSON file that contains information on how to run this execution.",
104+
// us::Any(),
105+
// false);
106+
107+
/**** Parse arguments. This method returns a mapping of long argument names to
108+
their values. ****/
109+
110+
auto parsedArgs = parser.parseArguments(argc, argv);
111+
112+
if (parsedArgs.empty())
113+
return EXIT_FAILURE; // Just exit, usage information was already printed.
114+
115+
if (parsedArgs["task"].Empty() || parsedArgs["cohort"].Empty())
116+
{
117+
MITK_INFO << parser.helpText();
118+
return EXIT_FAILURE;
119+
}
120+
121+
// // Parse, cast and set required arguments
122+
// auto task = us::any_cast<std::string>(parsedArgs["task"]);
123+
// auto cohort = us::any_cast<std::string>(parsedArgs["cohort"]);
124+
125+
auto imagesPaths = us::any_cast<std::string>(parsedArgs["images"]);
126+
auto labelsPath = us::any_cast<std::string>(parsedArgs["labels"]);
127+
auto outputDir = us::any_cast<std::string>(parsedArgs["outputdir"]);
128+
129+
/**** Default values for optional arguments ****/
130+
131+
std::string priorImagesPaths = "";
132+
// // Parse, cast and set optional arguments
133+
if (parsedArgs.end() != parsedArgs.find("priorimages"))
134+
{
135+
priorImagesPaths = us::any_cast<std::string>(parsedArgs["priorimages"]);
136+
}
137+
138+
std::vector<std::string> imagesPathsVector = split(imagesPaths, ',');
139+
std::vector<std::string> priorImagesPathsVector = split(priorImagesPaths, ',');
140+
141+
/**** Run ****/
142+
143+
try
144+
{
145+
std::vector<mitk::Image::Pointer> images;
146+
std::vector<mitk::Image::Pointer> priorImages;
147+
mitk::LabelSetImage::Pointer seeds;
148+
149+
/**** Read input ****/
150+
151+
for (std::string& imagePath : imagesPathsVector)
152+
{
153+
auto image = mitk::IOUtil::Load<mitk::Image>(imagePath);
154+
images.push_back(image);
155+
}
156+
157+
for (auto& priorImagePath : priorImagesPathsVector)
158+
{
159+
auto priorImage = mitk::IOUtil::Load<mitk::Image>(priorImagePath);
160+
priorImages.push_back(priorImage);
161+
}
162+
163+
seeds = mitk::IOUtil::Load<mitk::LabelSetImage>(labelsPath);
164+
165+
// auto algorithm = new CaPTkInteractiveSegmentation(nullptr);
166+
// algorithm->Run(task, cohort);
167+
168+
if (images[0]->GetDimension() == 3)
169+
{
170+
// [ 3D ]
171+
172+
/**** Convert to itk ****/
173+
174+
std::vector<itk::Image<float,3>::Pointer> imagesItk;
175+
std::vector<itk::Image<float,3>::Pointer> priorImagesItk;
176+
typename itk::Image<int,3>::Pointer seedsItk;
177+
178+
for (auto& image : images)
179+
{
180+
typename itk::Image<float, 3>::Pointer imageItk;
181+
mitk::CastToItkImage(image, imageItk);
182+
imagesItk.push_back(imageItk);
183+
}
184+
185+
for (auto& image : priorImages)
186+
{
187+
typename itk::Image<float, 3>::Pointer imageItk;
188+
mitk::CastToItkImage(image, imageItk);
189+
priorImagesItk.push_back(imageItk);
190+
}
191+
192+
mitk::CastToItkImage(seeds, seedsItk);
193+
194+
std::unique_ptr<GeodesicTrainingSegmentation::Coordinator<float,3>> geodesicTraining(
195+
new GeodesicTrainingSegmentation::Coordinator<float,3>()
196+
);
197+
geodesicTraining->SetInputImages(imagesItk);
198+
geodesicTraining->SetExtraInputImagesNotAGDable(priorImagesItk);
199+
geodesicTraining->SetLabels(seedsItk);
200+
geodesicTraining->SetOutputPath(outputDir);
201+
// geodesicTraining->SetNumberOfThreads(16);
202+
// geodesicTraining->SaveOnlyNormalSegmentation(true, "segmentation");
203+
geodesicTraining->SetVerbose(true);
204+
205+
/**** Run algorithm ****/
206+
207+
auto result = geodesicTraining->Execute();
208+
209+
if (result->ok)
210+
{
211+
mitk::Image::Pointer segmNormal;
212+
mitk::CastToMitkImage(result->labelsImage, segmNormal);
213+
214+
mitk::LabelSetImage::Pointer segm = mitk::LabelSetImage::New();
215+
216+
segm->InitializeByLabeledImage(segmNormal);
217+
218+
mitk::IOUtil::Save(segm, outputDir + std::string("/segmentation.nii.gz"));
219+
}
220+
else {
221+
std::cout << "Algorithm finished with internal error\n";
222+
return EXIT_FAILURE;
223+
}
224+
}
225+
else
226+
{
227+
// [ 2D ]
228+
229+
std::cout << "2D not supported yet\n";
230+
return EXIT_FAILURE;
231+
}
232+
233+
return EXIT_SUCCESS;
234+
}
235+
catch (const std::exception &e)
236+
{
237+
MITK_ERROR << e.what();
238+
return EXIT_FAILURE;
239+
}
240+
catch (...)
241+
{
242+
MITK_ERROR << "Unexpected error!";
243+
return EXIT_FAILURE;
244+
}
245+
}

0 commit comments

Comments
 (0)