1 #include <itkThresholdImageFilter.h> 2 #include <itkThresholdLabelerImageFilter.h> 3 #include <itkImageRegionConstIterator.h> 10 #include <tclap/CmdLine.h> 14 int main(
int argc,
char **argv)
16 TCLAP::CmdLine cmd(
"INRIA / IRISA - VisAGeS/Empenn Team",
' ',ANIMA_VERSION);
18 TCLAP::ValueArg<std::string> inputArg(
"i",
"inputimage",
"Input image",
true,
"",
"Input image",cmd);
19 TCLAP::ValueArg<std::string> outputArg(
"o",
"outputimage",
"Output image",
true,
"",
"Output image",cmd);
20 TCLAP::ValueArg<std::string> maskArg(
"m",
"maskfile",
"mask file",
false,
"",
"mask file",cmd);
22 TCLAP::ValueArg<double> thrArg(
"t",
"thr",
"Threshold value",
false,1.0,
"Threshold value",cmd);
23 TCLAP::ValueArg<double> upperThrArg(
"u",
"uthr",
"Upper threshold value",
false,USHRT_MAX,
"Upper threshold value",cmd);
24 TCLAP::ValueArg<double> adaptThrArg(
"a",
"adaptivethr",
"Adaptative threshold value (between 0 and 1)",
false,0.0,
"adaptative threshold value",cmd);
26 TCLAP::SwitchArg invArg(
"I",
"inv",
"Computes 1-res",cmd,
false);
32 catch (TCLAP::ArgException& e)
34 std::cerr <<
"Error: " << e.error() <<
"for argument " << e.argId() << std::endl;
38 typedef itk::Image<double,3> DoubleImageType;
39 typedef itk::Image<unsigned char, 3> UCImageType;
41 typedef itk::ImageRegionConstIterator <DoubleImageType> DoubleImageIterator;
42 typedef itk::ImageRegionIterator <UCImageType> UCImageIterator;
44 typedef itk::ThresholdImageFilter <DoubleImageType> ThresholdFilterType;
45 typedef itk::ThresholdLabelerImageFilter <DoubleImageType,UCImageType> LabelerFilterType;
47 DoubleImageType::Pointer inputImage = anima::readImage <DoubleImageType> (inputArg.getValue());
49 DoubleImageType::RegionType tmpRegionInputImage = inputImage->GetLargestPossibleRegion();
50 DoubleImageIterator doubleIt(inputImage,tmpRegionInputImage);
52 unsigned int totalSize = tmpRegionInputImage.GetSize()[0]*tmpRegionInputImage.GetSize()[1]*tmpRegionInputImage.GetSize()[2];
53 std::vector <double> tmpVec (totalSize);
57 if (maskArg.getValue() !=
"")
59 UCImageType::Pointer maskImage = anima::readImage <UCImageType> (maskArg.getValue());
61 UCImageType::RegionType tmpRegionMaskImage = maskImage->GetLargestPossibleRegion();
62 UCImageIterator ucIt(maskImage,tmpRegionMaskImage);
64 if ((tmpRegionInputImage.GetSize()[0]!=tmpRegionMaskImage.GetSize()[0]) ||
65 (tmpRegionInputImage.GetSize()[1]!=tmpRegionMaskImage.GetSize()[1]) ||
66 (tmpRegionInputImage.GetSize()[2]!=tmpRegionMaskImage.GetSize()[2]))
68 std::cerr <<
"InputImage size != MaskImage size" << std::endl;
72 for (
unsigned int i = 0;i < totalSize;++i)
76 tmpVec[idx] = doubleIt.Get();
89 for (
unsigned int i = 0;i < totalSize;++i)
91 tmpVec[i] = doubleIt.Get();
96 if ((adaptThrArg.getValue() < 0) || (adaptThrArg.getValue() > 1))
98 std::cerr <<
"Adaptative threshold value has to be included in the [0,1] interval" << std::endl;
102 unsigned int partialElt = (
unsigned int)floor(adaptThrArg.getValue()*idx);
103 if (partialElt == idx)
104 partialElt = idx - 1;
107 std::partial_sort(tmpVec.begin(),tmpVec.begin() + partialElt + 1,tmpVec.end());
109 double thrV = thrArg.getValue();
111 thrV = tmpVec[partialElt];
113 ThresholdFilterType::Pointer thrFilter = ThresholdFilterType::New();
114 thrFilter->SetInput(inputImage);
116 double upperThr = upperThrArg.getValue();
120 if (upperThr != USHRT_MAX)
121 thrFilter->ThresholdOutside(thrV,upperThr);
123 thrFilter->ThresholdBelow(thrV);
129 catch (itk::ExceptionObject &e)
131 std::cerr << e << std::endl;
135 LabelerFilterType::Pointer mainFilter = LabelerFilterType::New();
136 mainFilter->SetInput(thrFilter->GetOutput());
138 LabelerFilterType::RealThresholdVector thrVals;
139 thrVals.push_back(0);
141 mainFilter->SetRealThresholds(thrVals);
145 mainFilter->Update();
147 catch (itk::ExceptionObject &e)
149 std::cerr << e << std::endl;
155 UCImageIterator resIt(mainFilter->GetOutput(),tmpRegionInputImage);
157 for (
unsigned int i = 0;i < totalSize;++i)
159 resIt.Set(1-resIt.Get());
164 anima::writeImage <UCImageType> (outputArg.getValue(),mainFilter->GetOutput());
int main(int argc, char **argv)