|
| 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