12 #ifndef mitkCLPolyToNrrd_cpp 13 #define mitkCLPolyToNrrd_cpp 22 #include "itkImageRegionIterator.h" 28 #include <itkLabelStatisticsImageFilter.h> 29 #include <itkMinimumMaximumImageCalculator.h> 34 template<
typename TPixel,
unsigned int VImageDimension>
38 typedef itk::Image<TPixel, VImageDimension>
ImageType;
39 typedef itk::Image<unsigned char, VImageDimension> MaskType;
41 typename ImageType::Pointer itkIm2 = ImageType::New();
42 typename MaskType::Pointer itkMask1 = MaskType::New();
46 itk::ImageRegionIterator<ImageType> iterI1(itkImage, itkImage->GetLargestPossibleRegion());
47 itk::ImageRegionIterator<ImageType> iterI2(itkIm2, itkImage->GetLargestPossibleRegion());
48 itk::ImageRegionIterator<MaskType> iter(itkMask1, itkImage->GetLargestPossibleRegion());
49 while (! iter.IsAtEnd())
51 unsigned char maskV = 0;
52 if (iterI1.Value() > 0.0001 && iterI2.Value() > 0.00001)
64 int main(
int argc,
char* argv[])
67 typedef itk::Image<unsigned short, 3> MaskType;
78 parser.
setTitle(
"MR Normalization Tool");
79 parser.
setDescription(
"Normalizes a MR image. Sets the Median of the tissue covered by mask 0 to 0 and the median of the area covered by mask 1 to 1.");
82 std::map<std::string, us::Any> parsedArgs = parser.
parseArguments(argc, argv);
84 if (parsedArgs.size()==0)
88 if ( parsedArgs.count(
"help") || parsedArgs.count(
"h"))
92 us::Any listAny = parsedArgs[
"image"];
95 std::vector<ImageType::Pointer> imageList;
96 for (std::size_t i = 0; i < inputImageList.size(); ++i)
99 ImageType::Pointer itkImage = ImageType::New();
101 imageList.push_back(itkImage);
104 MaskType::Pointer
mask = MaskType::New();
107 itk::ImageRegionIterator<MaskType> maskIter(mask, mask->GetLargestPossibleRegion());
108 while (!maskIter.IsAtEnd())
114 std::vector<ImageType::IndexType> listOfIndexes;
115 listOfIndexes.reserve(1000);
118 ImageType::IndexType tmpIndex;
119 ImageType::IndexType startIndex;
121 for (
unsigned char i = 0; i < 8; ++i)
124 if ((i & 1) > 0) tmpIndex[0] = mask->GetLargestPossibleRegion().GetSize(0)-1;
125 if ((i & 2) > 0) tmpIndex[1] = mask->GetLargestPossibleRegion().GetSize(1)-1;
126 if ((i & 4) > 0) tmpIndex[2] = mask->GetLargestPossibleRegion().GetSize(2)-1;
129 if (imageList[0]->GetPixel(tmpIndex) < imageList[0]->GetPixel(startIndex))
131 startIndex = tmpIndex;
134 listOfIndexes.push_back(tmpIndex);
136 while (listOfIndexes.size() > 0)
138 ImageType::IndexType currentIndex = listOfIndexes.back();
139 listOfIndexes.pop_back();
140 if (!(mask->GetLargestPossibleRegion().IsInside(currentIndex)))
144 if (mask->GetPixel(currentIndex) == 0)
146 mask->SetPixel(currentIndex, 1);
148 for (std::size_t i = 0; i < imageList.size(); ++i)
150 minimum = std::min<double>(minimum, imageList[i]->GetPixel(currentIndex));
154 mask->SetPixel(currentIndex, 2);
155 tmpIndex = currentIndex;
157 listOfIndexes.push_back(tmpIndex);
159 listOfIndexes.push_back(tmpIndex);
162 listOfIndexes.push_back(tmpIndex);
164 listOfIndexes.push_back(tmpIndex);
167 listOfIndexes.push_back(tmpIndex);
169 listOfIndexes.push_back(tmpIndex);
174 maskIter.GoToBegin();
175 while (!maskIter.IsAtEnd())
177 if (maskIter.Get() == 2)
186 std::string maskPath = parsedArgs[
"mask"].ToString();
static void DetectSkull(itk::Image< TPixel, VImageDimension > *itkImage, mitk::Image::Pointer im2, mitk::Image::Pointer mask1, std::string output)
itk::Image< unsigned char, 3 > ImageType
void setContributor(std::string contributor)
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)
Image::Pointer ImportItkImage(const itk::SmartPointer< ItkOutputImageType > &itkimage, const BaseGeometry *geometry=nullptr, bool update=true)
Imports an itk::Image (with a specific type) as an mitk::Image.Instantiates instance of ITKImageImpor...
itk::Image< double, 3 > FloatImageType
void setCategory(std::string category)
mitk::Image::Pointer image
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
static void Save(const mitk::BaseData *data, const std::string &path, bool setPathProperty=false)
Save a mitk::BaseData instance.
std::vector< std::string > StringContainerType
mitk::Image::Pointer mask
void setTitle(std::string title)
itk::Image< unsigned char, 3 > MaskImageType
int main(int argc, char *argv[])
void setDescription(std::string description)