2 #include <itkImageRegionConstIterator.h> 3 #include <tclap/CmdLine.h> 6 int main(
int argc,
char * *argv)
8 TCLAP::CmdLine cmd(
"INRIA / IRISA - VisAGeS/Empenn Team",
' ', ANIMA_VERSION);
10 TCLAP::ValueArg <std::string> refArg(
"r",
"reffile",
"Reference segmentations",
true,
"",
"reference image", cmd);
11 TCLAP::ValueArg <std::string> testArg(
"t",
"testfile",
"Test segmentations",
true,
"",
"test image", cmd);
13 TCLAP::ValueArg <unsigned int> minNumPixelsRefArg(
"m",
"min-card",
"Minimal number of pixels in each reference label area",
false, 0,
"minimal label size", cmd);
14 TCLAP::SwitchArg totalOverlapArg(
"T",
"total-overlap",
"Compute total overlap measure as in Klein et al 2009", cmd,
false);
15 TCLAP::SwitchArg jacArg(
"J",
"jaccard",
"Compute Jaccard similarity instead of Dice", cmd,
false);
16 TCLAP::SwitchArg xlsArg(
"X",
"xlsmode",
"Output formatted to be include in a Excel readable text file", cmd,
false);
18 TCLAP::ValueArg <std::string> fileArg(
"o",
"out-txt",
"Output stored into a file",
false,
"",
"Output file", cmd);
22 cmd.parse(argc, argv);
24 catch (TCLAP::ArgException& e)
26 std::cerr <<
"Error: " << e.error() <<
"for argument " << e.argId() << std::endl;
30 typedef itk::Image <unsigned short, 3> ImageType;
31 typedef itk::ImageFileReader <ImageType> ImageReaderType;
32 typedef itk::ImageRegionConstIterator <ImageType> ImageIteratorType;
34 ImageType::Pointer refImage = anima::readImage <ImageType> (refArg.getValue());
36 ImageIteratorType refIt (refImage, refImage->GetLargestPossibleRegion());
38 std::vector <unsigned int> usefulLabels;
40 while (!refIt.IsAtEnd())
44 bool isAlreadyIn =
false;
45 for (
unsigned int i = 0; i < usefulLabels.size(); ++i)
47 if (refIt.Get() == usefulLabels[i])
55 usefulLabels.push_back(refIt.Get());
61 std::sort(usefulLabels.begin(), usefulLabels.end());
63 std::map <unsigned int, unsigned int> labelBackMap;
66 for (
unsigned int i = 0; i < usefulLabels.size(); ++i)
68 labelBackMap[usefulLabels[i]] = i+1;
73 ImageType::Pointer testImage = anima::readImage <ImageType> (testArg.getValue());
74 ImageIteratorType testIt (testImage, testImage->GetLargestPossibleRegion());
76 unsigned int numLabels = usefulLabels.size();
77 std::vector <double> cardRef(numLabels+1, 0), cardTest(numLabels+1, 0), cardInter(numLabels+1, 0);
79 while(!testIt.IsAtEnd())
81 unsigned int testVal = testIt.Get();
82 unsigned int refVal = refIt.Get();
84 cardTest[labelBackMap[testVal]]++;
85 cardRef[labelBackMap[refVal]]++;
87 if (testVal == refVal)
88 cardInter[labelBackMap[testVal]]++;
97 std::fstream oStremFileOut;
98 std::ostream *poOutStreamForResults = &std::cout;
99 bool bFileResFailed =
false;
101 if (fileArg.getValue() !=
"")
103 oStremFileOut.open(fileArg.getValue().c_str(), std::ios::out | std::ios::trunc);
104 if(!oStremFileOut.is_open())
106 std::cerr <<
"Can not open file: " << fileArg.getValue() <<
"to store score" << std::endl;
107 bFileResFailed =
true;
111 poOutStreamForResults = &oStremFileOut;
114 std::ostream &roOutStreamForResults = *poOutStreamForResults;
118 if (totalOverlapArg.isSet())
122 unsigned int numUsedLabels = 0;
123 for (
unsigned int i = 1; i <= numLabels; ++i)
125 if (cardRef[i] > minNumPixelsRefArg.getValue())
127 score += cardInter[i] / cardRef[i];
132 if (numUsedLabels > 0)
133 score /= numUsedLabels;
137 roOutStreamForResults << score << std::endl;
139 roOutStreamForResults <<
"Total overlap score: " << score << std::endl;
143 for (
unsigned int i = 1; i <= numLabels; ++i)
145 if (cardRef[i] <= minNumPixelsRefArg.getValue())
148 double score = 2*cardInter[i]/(cardRef[i] + cardTest[i]);
151 score = score/(2.0 - score);
155 roOutStreamForResults << score;
157 roOutStreamForResults <<
", " << std::flush;
159 roOutStreamForResults << std::endl;
162 roOutStreamForResults <<
"Label " << usefulLabels[i-1] <<
": " << score << std::endl;
169 if (oStremFileOut.is_open())
170 oStremFileOut.close();
int main(int argc, char **argv)