21 #include <itkImageRegionIteratorWithIndex.h> 27 struct GreyLevelSizeZoneMatrixHolder
30 GreyLevelSizeZoneMatrixHolder(
double min,
double max,
int number,
int maxSize);
32 int IntensityToIndex(
double intensity);
33 double IndexToMinIntensity(
int index);
34 double IndexToMeanIntensity(
int index);
35 double IndexToMaxIntensity(
int index);
37 double m_MinimumRange;
38 double m_MaximumRange;
42 Eigen::MatrixXd m_Matrix;
46 struct GreyLevelSizeZoneFeatures
48 GreyLevelSizeZoneFeatures() :
51 LowGreyLevelEmphasis(0),
52 HighGreyLevelEmphasis(0),
53 SmallZoneLowGreyLevelEmphasis(0),
54 SmallZoneHighGreyLevelEmphasis(0),
55 LargeZoneLowGreyLevelEmphasis(0),
56 LargeZoneHighGreyLevelEmphasis(0),
57 GreyLevelNonUniformity(0),
58 GreyLevelNonUniformityNormalized(0),
59 ZoneSizeNonUniformity(0),
60 ZoneSizeNoneUniformityNormalized(0),
71 double SmallZoneEmphasis;
72 double LargeZoneEmphasis;
73 double LowGreyLevelEmphasis;
74 double HighGreyLevelEmphasis;
75 double SmallZoneLowGreyLevelEmphasis;
76 double SmallZoneHighGreyLevelEmphasis;
77 double LargeZoneLowGreyLevelEmphasis;
78 double LargeZoneHighGreyLevelEmphasis;
79 double GreyLevelNonUniformity;
80 double GreyLevelNonUniformityNormalized;
81 double ZoneSizeNonUniformity;
82 double ZoneSizeNoneUniformityNormalized;
83 double ZonePercentage;
85 double GreyLevelVariance;
87 double ZoneSizeVariance;
88 double ZoneSizeEntropy;
99 mitk::GreyLevelSizeZoneMatrixHolder::GreyLevelSizeZoneMatrixHolder(
double min,
double max,
int number,
int maxSize) :
102 m_NumberOfBins(number),
103 m_MaximumSize(maxSize)
105 m_Matrix.resize(number, maxSize);
107 m_Stepsize = (max -
min) / (number);
110 int mitk::GreyLevelSizeZoneMatrixHolder::IntensityToIndex(
double intensity)
112 return std::floor((intensity - m_MinimumRange) / m_Stepsize);
115 double mitk::GreyLevelSizeZoneMatrixHolder::IndexToMinIntensity(
int index)
117 return m_MinimumRange + index * m_Stepsize;
119 double mitk::GreyLevelSizeZoneMatrixHolder::IndexToMeanIntensity(
int index)
121 return m_MinimumRange + (index+0.5) * m_Stepsize;
123 double mitk::GreyLevelSizeZoneMatrixHolder::IndexToMaxIntensity(
int index)
125 return m_MinimumRange + (index + 1) * m_Stepsize;
128 template<
typename TPixel,
unsigned int VImageDimension>
131 itk::Image<unsigned short, VImageDimension>*
mask,
132 std::vector<itk::Offset<VImageDimension> > offsets,
133 bool estimateLargestRegion,
134 mitk::GreyLevelSizeZoneMatrixHolder &holder)
136 typedef itk::Image<TPixel, VImageDimension>
ImageType;
137 typedef itk::Image<unsigned short, VImageDimension>
MaskImageType;
138 typedef typename ImageType::IndexType IndexType;
140 typedef itk::ImageRegionIteratorWithIndex<ImageType> ConstIterType;
141 typedef itk::ImageRegionIteratorWithIndex<MaskImageType> ConstMaskIterType;
143 auto region = mask->GetLargestPossibleRegion();
144 typename MaskImageType::RegionType newRegion;
145 newRegion.SetSize(region.GetSize());
146 newRegion.SetIndex(region.GetIndex());
148 ConstIterType imageIter(itkImage, itkImage->GetLargestPossibleRegion());
149 ConstMaskIterType maskIter(mask, mask->GetLargestPossibleRegion());
151 typename MaskImageType::Pointer visitedImage = MaskImageType::New();
152 visitedImage->SetRegions(newRegion);
153 visitedImage->Allocate();
154 visitedImage->FillBuffer(0);
156 int largestRegion = 0;
158 while (!maskIter.IsAtEnd())
160 if (maskIter.Value() > 0 )
162 auto startIntensityIndex = holder.IntensityToIndex(imageIter.Value());
163 std::vector<IndexType> indices;
164 indices.push_back(maskIter.GetIndex());
165 unsigned int steps = 0;
167 while (indices.size() > 0)
169 auto currentIndex = indices.back();
172 if (!region.IsInside(currentIndex))
177 auto wasVisited = visitedImage->GetPixel(currentIndex);
178 auto newIntensityIndex = holder.IntensityToIndex(itkImage->GetPixel(currentIndex));
179 auto isInMask = mask->GetPixel(currentIndex);
181 if ((isInMask > 0) &&
182 (newIntensityIndex == startIntensityIndex) &&
186 visitedImage->SetPixel(currentIndex, 1);
187 for (
auto offset : offsets)
189 auto newIndex = currentIndex +
offset;
190 indices.push_back(newIndex);
191 newIndex = currentIndex -
offset;
192 indices.push_back(newIndex);
199 largestRegion = std::max<int>(steps, largestRegion);
200 steps = std::min<unsigned int>(steps, holder.m_MaximumSize);
201 if (!estimateLargestRegion)
203 holder.m_Matrix(startIntensityIndex, steps - 1) += 1;
210 return largestRegion;
214 mitk::GreyLevelSizeZoneMatrixHolder &holder,
215 mitk::GreyLevelSizeZoneFeatures & results
218 auto SgzMatrix = holder.m_Matrix;
219 auto pgzMatrix = holder.m_Matrix;
220 auto pgMatrix = holder.m_Matrix;
221 auto pzMatrix = holder.m_Matrix;
223 double Ns = pgzMatrix.sum();
225 pgMatrix.rowwise().normalize();
226 pzMatrix.colwise().normalize();
228 for (
int i = 0; i < holder.m_NumberOfBins; ++i)
229 for (
int j = 0; j < holder.m_NumberOfBins; ++j)
231 if (pgzMatrix(i, j) != pgzMatrix(i, j))
233 if (pgMatrix(i, j) != pgMatrix(i, j))
235 if (pzMatrix(i, j) != pzMatrix(i, j))
239 Eigen::VectorXd SgVector = SgzMatrix.rowwise().sum();
240 Eigen::VectorXd SzVector = SgzMatrix.colwise().sum();
242 for (
int j = 0; j < SzVector.size(); ++j)
244 results.SmallZoneEmphasis += SzVector(j) / (j + 1) / (j + 1);
245 results.LargeZoneEmphasis += SzVector(j) * (j + 1.0) * (j + 1.0);
246 results.ZoneSizeNonUniformity += SzVector(j) * SzVector(j);
247 results.ZoneSizeNoneUniformityNormalized += SzVector(j) * SzVector(j);
249 for (
int i = 0; i < SgVector.size(); ++i)
251 results.LowGreyLevelEmphasis += SgVector(i) / (i + 1) / (i + 1);
252 results.HighGreyLevelEmphasis += SgVector(i) * (i + 1) * (i + 1);
253 results.GreyLevelNonUniformity += SgVector(i)*SgVector(i);
254 results.GreyLevelNonUniformityNormalized += SgVector(i)*SgVector(i);
257 for (
int i = 0; i < SgzMatrix.rows(); ++i)
259 for (
int j = 0; j < SgzMatrix.cols(); ++j)
261 results.SmallZoneLowGreyLevelEmphasis += SgzMatrix(i, j) / (i + 1) / (i + 1) / (j + 1) / (j + 1);
262 results.SmallZoneHighGreyLevelEmphasis += SgzMatrix(i, j) * (i + 1) * (i + 1) / (j + 1) / (j + 1);
263 results.LargeZoneLowGreyLevelEmphasis += SgzMatrix(i, j) / (i + 1) / (i + 1) * (j + 1.0) * (j + 1.0);
264 results.LargeZoneHighGreyLevelEmphasis += SgzMatrix(i, j) * (i + 1) * (i + 1) * (j + 1.0) * (j + 1.0);
265 results.ZonePercentage += SgzMatrix(i, j)*(j + 1);
267 results.GreyLevelMean += (i + 1)*pgzMatrix(i, j);
268 results.ZoneSizeMean += (j + 1)*pgzMatrix(i, j);
269 if (pgzMatrix(i, j) > 0)
270 results.ZoneSizeEntropy -= pgzMatrix(i, j) * std::log(pgzMatrix(i, j)) / std::log(2);
274 for (
int i = 0; i < SgzMatrix.rows(); ++i)
276 for (
int j = 0; j < SgzMatrix.cols(); ++j)
278 results.GreyLevelVariance += (i + 1 - results.GreyLevelMean)*(i + 1 - results.GreyLevelMean)*pgzMatrix(i, j);
279 results.ZoneSizeVariance += (j + 1 - results.ZoneSizeMean)*(j + 1 - results.ZoneSizeMean)*pgzMatrix(i, j);
283 results.SmallZoneEmphasis /= Ns;
284 results.LargeZoneEmphasis /= Ns;
285 results.LowGreyLevelEmphasis /= Ns;
286 results.HighGreyLevelEmphasis /= Ns;
288 results.SmallZoneLowGreyLevelEmphasis /= Ns;
289 results.SmallZoneHighGreyLevelEmphasis /= Ns;
290 results.LargeZoneLowGreyLevelEmphasis /= Ns;
291 results.LargeZoneHighGreyLevelEmphasis /= Ns;
292 results.GreyLevelNonUniformity /= Ns;
293 results.GreyLevelNonUniformityNormalized /= Ns*Ns;
294 results.ZoneSizeNonUniformity /= Ns;
295 results.ZoneSizeNoneUniformityNormalized /= Ns*Ns;
297 results.ZonePercentage = Ns / results.ZonePercentage;
300 template<
typename TPixel,
unsigned int VImageDimension>
304 typedef itk::Image<unsigned short, VImageDimension> MaskType;
305 typedef itk::Neighborhood<TPixel, VImageDimension > NeighborhoodType;
306 typedef itk::Offset<VImageDimension> OffsetType;
311 int numberOfBins = config.
Bins;
313 typename MaskType::Pointer maskImage = MaskType::New();
317 std::vector < itk::Offset<VImageDimension> > offsetVector;
318 NeighborhoodType hood;
320 unsigned int centerIndex = hood.GetCenterNeighborhoodIndex();
322 for (
unsigned int d = 0; d < centerIndex; d++)
324 offset = hood.GetOffset(d);
325 bool useOffset =
true;
326 for (
unsigned int i = 0; i < VImageDimension; ++i)
328 if ((config.
direction == i + 2) && offset[i] != 0)
335 offsetVector.push_back(offset);
341 offsetVector.clear();
345 offsetVector.push_back(offset);
348 std::vector<mitk::GreyLevelSizeZoneFeatures> resultVector;
349 mitk::GreyLevelSizeZoneMatrixHolder tmpHolder(rangeMin, rangeMax, numberOfBins, 3);
350 int largestRegion = CalculateGlSZMatrix<TPixel, VImageDimension>(itkImage, maskImage, offsetVector,
true, tmpHolder);
351 mitk::GreyLevelSizeZoneMatrixHolder holderOverall(rangeMin, rangeMax, numberOfBins,largestRegion);
352 mitk::GreyLevelSizeZoneFeatures overallFeature;
353 CalculateGlSZMatrix<TPixel, VImageDimension>(itkImage, maskImage, offsetVector,
false, holderOverall);
365 featureList.push_back(std::make_pair(prefix +
"Small Zone Emphasis", features.SmallZoneEmphasis));
366 featureList.push_back(std::make_pair(prefix +
"Large Zone Emphasis", features.LargeZoneEmphasis));
367 featureList.push_back(std::make_pair(prefix +
"Low Grey Level Emphasis", features.LowGreyLevelEmphasis));
368 featureList.push_back(std::make_pair(prefix +
"High Grey Level Emphasis", features.HighGreyLevelEmphasis));
369 featureList.push_back(std::make_pair(prefix +
"Small Zone Low Grey Level Emphasis", features.SmallZoneLowGreyLevelEmphasis));
370 featureList.push_back(std::make_pair(prefix +
"Small Zone High Grey Level Emphasis", features.SmallZoneHighGreyLevelEmphasis));
371 featureList.push_back(std::make_pair(prefix +
"Large Zone Low Grey Level Emphasis", features.LargeZoneLowGreyLevelEmphasis));
372 featureList.push_back(std::make_pair(prefix +
"Large Zone High Grey Level Emphasis", features.LargeZoneHighGreyLevelEmphasis));
373 featureList.push_back(std::make_pair(prefix +
"Grey Level Non-Uniformity", features.GreyLevelNonUniformity));
374 featureList.push_back(std::make_pair(prefix +
"Grey Level Non-Uniformity Normalized", features.GreyLevelNonUniformityNormalized));
375 featureList.push_back(std::make_pair(prefix +
"Zone Size Non-Uniformity", features.ZoneSizeNonUniformity));
376 featureList.push_back(std::make_pair(prefix +
"Zone Size Non-Uniformity Normalized", features.ZoneSizeNoneUniformityNormalized));
377 featureList.push_back(std::make_pair(prefix +
"Zone Percentage", features.ZonePercentage));
378 featureList.push_back(std::make_pair(prefix +
"Grey Level Mean", features.GreyLevelMean));
379 featureList.push_back(std::make_pair(prefix +
"Grey Level Variance", features.GreyLevelVariance));
380 featureList.push_back(std::make_pair(prefix +
"Zone Size Mean", features.ZoneSizeMean));
381 featureList.push_back(std::make_pair(prefix +
"Zone Size Variance", features.ZoneSizeVariance));
382 featureList.push_back(std::make_pair(prefix +
"Zone Size Entropy", features.ZoneSizeEntropy));
438 MITK_INFO <<
"Start calculating Grey leve size zone ...";
440 featureList.insert(featureList.end(), localResults.begin(), localResults.end());
441 MITK_INFO <<
"Finished calculating Grey level size zone ...";
#define AccessByItk_3(mitkImage, itkImageTypeFunction, arg1, arg2, arg3)
FeatureListType CalculateFeatures(const Image::Pointer &image, const Image::Pointer &feature) override
Calculates the Cooccurence-Matrix based features for this class.
std::vector< std::string > FeatureNameListType
itk::Image< unsigned char, 3 > ImageType
virtual void SetLongName(std::string _arg)
static void CalculateGreyLevelSizeZoneFeatures(itk::Image< TPixel, VImageDimension > *itkImage, mitk::Image::Pointer mask, mitk::GIFGreyLevelSizeZone::FeatureListType &featureList, mitk::GIFGreyLevelSizeZone::GIFGreyLevelSizeZoneConfiguration config)
DataCollection - Class to facilitate loading/accessing structured data.
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)
FeatureNameListType GetFeatureNames() override
Returns a list of the names of all features that are calculated from this class.
virtual void SetShortName(std::string _arg)
std::vector< std::pair< std::string, double > > FeatureListType
std::string QuantifierParameterString()
std::string FeatureDescriptionPrefix()
Returns a string that encodes the feature class name.
static void MatrixFeaturesTo(mitk::GreyLevelSizeZoneFeatures features, std::string prefix, mitk::GIFGreyLevelSizeZone::FeatureListType &featureList)
FeatureListType CalculateFeatures(const Image::Pointer &image, const Image::Pointer &feature) override
Calculates the Cooccurence-Matrix based features for this class.
void CalculateFeaturesUsingParameters(const Image::Pointer &feature, const Image::Pointer &mask, const Image::Pointer &maskNoNAN, FeatureListType &featureList) override
Calculates the feature of this abstact interface. Does not necessarily considers the parameter settin...
void AddArguments(mitkCommandLineParser &parser) override
virtual std::string GetLongName() const
std::string GetOptionPrefix() const
itk::Image< unsigned char, 3 > MaskImageType
virtual int GetDirection() const
mitk::Image::Pointer image
void AddQuantifierArguments(mitkCommandLineParser &parser)
virtual void SetFeatureClassName(std::string _arg)
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
mitk::Image::Pointer mask
virtual IntensityQuantifier::Pointer GetQuantifier()
void InitializeQuantifierFromParameters(const Image::Pointer &feature, const Image::Pointer &mask, unsigned int defaultBins=256)
static int CalculateGlSZMatrix(itk::Image< TPixel, VImageDimension > *itkImage, itk::Image< unsigned short, VImageDimension > *mask, std::vector< itk::Offset< VImageDimension > > offsets, bool estimateLargestRegion, mitk::GreyLevelSizeZoneMatrixHolder &holder)
void InitializeQuantifier(const Image::Pointer &feature, const Image::Pointer &mask, unsigned int defaultBins=256)
virtual ParameterTypes GetParameter() const
std::string GetCurrentFeatureEncoding() override
Adds an additional Separator to the name of the feature, which encodes the used parameters.