28 #include <itkConnectedThresholdImageFilter.h> 29 #include <itkImageRegionConstIterator.h> 31 template<
typename TPixel,
unsigned int VImageDimension>
32 void GetMinimum(itk::Image<TPixel, VImageDimension>* itkImage,
double &minimum)
37 itk::ImageRegionConstIterator<InputImageType> iter(itkImage, itkImage->GetLargestPossibleRegion());
39 while (!iter.IsAtEnd())
41 minimum = std::min<double>(minimum, iter.Get());
46 template<
typename TPixel,
unsigned int VImageDimension>
50 typedef typename InputImageType::IndexType IndexType;
51 typedef itk::ConnectedThresholdImageFilter<InputImageType, InputImageType> RegionGrowingFilterType;
52 typename RegionGrowingFilterType::Pointer regionGrower = RegionGrowingFilterType::New();
57 IndexType bestSeedIndex;
58 startIndex[0] = itkImage->GetLargestPossibleRegion().GetSize()[0]/2;
59 startIndex[1] = itkImage->GetLargestPossibleRegion().GetSize()[1]/2;
60 startIndex[2] = itkImage->GetLargestPossibleRegion().GetSize()[2]/2;
61 auto region = itkImage->GetLargestPossibleRegion();
62 auto spacing = itkImage->GetSpacing();
63 spacing[0] = itkImage->GetSpacing()[0];
64 spacing[1] = itkImage->GetSpacing()[1];
65 spacing[2] = itkImage->GetSpacing()[2];
67 int minimumDistance = 50 * 50 * (spacing[0] + spacing[1] + spacing[2]);
69 for (
int x = -50; x < 50; ++x)
71 for (
int y = -50; y < 50; ++y)
73 for (
int z = -20; z < 20; ++z)
75 seedIndex[0] = startIndex[0] + x;
76 seedIndex[1] = startIndex[1] + y;
77 seedIndex[2] = startIndex[2] + z;
78 if (region.IsInside(seedIndex))
80 if (itkImage->GetPixel(seedIndex) > 0)
82 int newDistance = x*x*spacing[0] + y*y*spacing[1] + z*z*spacing[2];
83 if (newDistance < minimumDistance)
85 bestSeedIndex = seedIndex;
86 minimumDistance = newDistance;
93 seedIndex = bestSeedIndex;
97 regionGrower->SetInput(itkImage);
98 regionGrower->AddSeed(seedIndex);
100 regionGrower->SetLower(1);
101 regionGrower->SetUpper(255);
105 regionGrower->Update();
107 catch (
const itk::ExceptionObject&)
120 int main(
int argc,
char* argv[])
136 std::map<std::string, us::Any> parsedArgs = parser.
parseArguments(argc, argv);
138 if (parsedArgs.size()==0)
142 if ( parsedArgs.count(
"help") || parsedArgs.count(
"h"))
148 std::string inputFile =
us::any_cast<std::string>(parsedArgs[
"input"]);
155 MITK_INFO <<
"Loaded Image";
164 MITK_INFO <<
"With Minimum at " << minimum<<
" Offset is set to: " <<
offset;
167 otsuFilter->SetNumberOfThresholds(1+offset);
168 otsuFilter->SetValleyEmphasis(
false);
169 otsuFilter->SetNumberOfBins(128);
170 otsuFilter->SetInput(image);
173 otsuFilter->Update();
177 mitkThrow() <<
"itkOtsuFilter error (image dimension must be in {2, 3} and image must not be RGB)";
180 MITK_INFO <<
"Calculated Otsu";
183 resultImage->InitializeByLabeledImage(otsuFilter->GetOutput());
#define AccessFixedTypeByItk_n(mitkImage, itkImageTypeFunction, pixelTypeSeq, dimSeq, va_tuple)
Access a mitk-image with known type (pixel type and dimension) by an itk-image with one or more param...
void GetMinimum(itk::Image< TPixel, VImageDimension > *itkImage, double &minimum)
void setContributor(std::string contributor)
::mitk::Image InputImageType
ValueType * any_cast(Any *operand)
void addArgument(const std::string &longarg, const std::string &shortarg, Type type, const std::string &argLabel, const std::string &argHelp=std::string(), const us::Any &defaultValue=us::Any(), bool optional=true, bool ignoreRest=false, bool deprecated=false, mitkCommandLineParser::Channel channel=mitkCommandLineParser::Channel::None)
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
#define AccessByItk_1(mitkImage, itkImageTypeFunction, arg1)
static void Closing(mitk::Image::Pointer &image, int factor, StructuralElementType structuralElement)
Perform morphological operation on 2D, 3D or 3D+t segmentation.
void StartRegionGrowing(itk::Image< TPixel, VImageDimension > *itkImage, mitk::Image::Pointer &result)
std::string helpText() const
void setCategory(std::string category)
mitk::Image::Pointer image
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
void CastToMitkImage(const itk::SmartPointer< ItkOutputImageType > &itkimage, itk::SmartPointer< mitk::Image > &mitkoutputimage)
Cast an itk::Image (with a specific type) to an mitk::Image.
static void Save(const mitk::BaseData *data, const std::string &path, bool setPathProperty=false)
Save a mitk::BaseData instance.
mitk::Label::PixelType PixelType
void setTitle(std::string title)
void setDescription(std::string description)
int main(int argc, char *argv[])
static void FillHoles(mitk::Image::Pointer &image)
Perform morphological operation on 2D, 3D or 3D+t segmentation.