~ubuntu-branches/debian/sid/simpleitk/sid

« back to all changes in this revision

Viewing changes to Testing/Unit/sitkLabelStatisticsTest.cxx

  • Committer: Package Import Robot
  • Author(s): Ghislain Antony Vaillant
  • Date: 2017-11-02 08:49:18 UTC
  • Revision ID: package-import@ubuntu.com-20171102084918-7hs09ih668xq87ej
Tags: upstream-1.0.1
ImportĀ upstreamĀ versionĀ 1.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*=========================================================================
 
2
*
 
3
*  Copyright Insight Software Consortium
 
4
*
 
5
*  Licensed under the Apache License, Version 2.0 (the "License");
 
6
*  you may not use this file except in compliance with the License.
 
7
*  You may obtain a copy of the License at
 
8
*
 
9
*         http://www.apache.org/licenses/LICENSE-2.0.txt
 
10
*
 
11
*  Unless required by applicable law or agreed to in writing, software
 
12
*  distributed under the License is distributed on an "AS IS" BASIS,
 
13
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
*  See the License for the specific language governing permissions and
 
15
*  limitations under the License.
 
16
*
 
17
*=========================================================================*/
 
18
#include <SimpleITKTestHarness.h>
 
19
#include <SimpleITK.h>
 
20
#include "itkImage.h"
 
21
#include "itkVectorImage.h"
 
22
 
 
23
#include <memory>
 
24
 
 
25
TEST(LabelStatistics,Simple) {
 
26
  itk::simple::ImageFileReader reader;
 
27
 
 
28
  //By using the same image, the label min/max values should equal the label itself.
 
29
  itk::simple::Image intensityImage = reader.SetFileName ( dataFinder.GetFile ( "Input/2th_cthead1.png" ) ).Execute();
 
30
  itk::simple::Image labelImage     = reader.SetFileName ( dataFinder.GetFile ( "Input/2th_cthead1.png" ) ).Execute();
 
31
 
 
32
  itk::simple::LabelStatisticsImageFilter lsFilter;
 
33
 
 
34
  EXPECT_TRUE(lsFilter.GetUseHistograms());
 
35
  lsFilter.UseHistogramsOff();
 
36
  EXPECT_FALSE(lsFilter.GetUseHistograms());
 
37
  lsFilter.UseHistogramsOn();
 
38
  EXPECT_TRUE(lsFilter.GetUseHistograms());
 
39
  lsFilter.SetUseHistograms(false);
 
40
  EXPECT_FALSE(lsFilter.GetUseHistograms());
 
41
  lsFilter.SetUseHistograms(true);
 
42
  EXPECT_TRUE(lsFilter.GetUseHistograms());
 
43
 
 
44
  try {
 
45
    lsFilter.Execute ( intensityImage, labelImage );
 
46
  } catch ( itk::ExceptionObject e ) {
 
47
    std::cout << "LabelStatistics failed: " << e.what() << std::endl;
 
48
  }
 
49
 
 
50
  std::vector<int64_t> labels = lsFilter.GetLabels();
 
51
  for(std::vector<int64_t>::const_iterator i = labels.begin(); i != labels.end(); ++i)
 
52
    {
 
53
    //By using the same image, the label min/max/mean values should equal the label itself.
 
54
    ASSERT_EQ(lsFilter.GetMinimum (*i) , *i);
 
55
    ASSERT_EQ(lsFilter.GetMaximum (*i) , *i);
 
56
    ASSERT_EQ(lsFilter.GetMean    (*i) , *i);
 
57
    ASSERT_EQ(lsFilter.GetMedian  (*i) , *i);
 
58
    //By using the same image, the label variance values should equal to Zero.
 
59
    ASSERT_EQ(lsFilter.GetSigma   (*i) , 0.0      );
 
60
    ASSERT_EQ(lsFilter.GetVariance(*i) , 0.0      );
 
61
    }
 
62
 
 
63
  ASSERT_EQ(lsFilter.GetSum  (0) , 0     );
 
64
  ASSERT_EQ(lsFilter.GetCount(0) , 33390u );
 
65
}
 
66
 
 
67
 
 
68
TEST(LabelStatistics,Commands) {
 
69
  namespace sitk = itk::simple;
 
70
 
 
71
  sitk::Image image = sitk::ReadImage ( dataFinder.GetFile ( "Input/cthead1.png" ) );
 
72
  sitk::Image labels = sitk::ReadImage ( dataFinder.GetFile ( "Input/2th_cthead1.mha" ) );
 
73
 
 
74
  sitk::LabelStatisticsImageFilter stats;
 
75
 
 
76
 
 
77
  ProgressUpdate progressCmd(stats);
 
78
  stats.AddCommand(sitk::sitkProgressEvent, progressCmd);
 
79
 
 
80
  CountCommand abortCmd(stats);
 
81
  stats.AddCommand(sitk::sitkAbortEvent, abortCmd);
 
82
 
 
83
  CountCommand deleteCmd(stats);
 
84
  stats.AddCommand(sitk::sitkDeleteEvent, deleteCmd);
 
85
 
 
86
  CountCommand endCmd(stats);
 
87
  stats.AddCommand(sitk::sitkEndEvent, endCmd);
 
88
 
 
89
  CountCommand iterCmd(stats);
 
90
  stats.AddCommand(sitk::sitkIterationEvent, iterCmd);
 
91
 
 
92
  CountCommand startCmd(stats);
 
93
  stats.AddCommand(sitk::sitkStartEvent, startCmd);
 
94
 
 
95
  CountCommand userCmd(stats);
 
96
  stats.AddCommand(sitk::sitkUserEvent, userCmd);
 
97
 
 
98
  stats.DebugOn();
 
99
 
 
100
  stats.Execute ( image, labels );
 
101
 
 
102
  EXPECT_EQ( stats.GetName(), "LabelStatisticsImageFilter" );
 
103
  EXPECT_NO_THROW( stats.ToString() );
 
104
  EXPECT_TRUE ( stats.HasLabel ( 0 ) );
 
105
  EXPECT_TRUE ( stats.HasLabel ( 1 ) );
 
106
  EXPECT_TRUE ( stats.HasLabel ( 2 ) );
 
107
  EXPECT_FALSE ( stats.HasLabel ( 99 ) );
 
108
  EXPECT_FALSE ( stats.HasLabel ( 1024 ) );
 
109
 
 
110
  EXPECT_NEAR ( stats.GetMinimum ( 0 ), 0, 0.01 );
 
111
  EXPECT_NEAR ( stats.GetMaximum ( 0 ), 99, 0.01 );
 
112
  EXPECT_NEAR ( stats.GetMean ( 0 ), 13.0911, 0.001 );
 
113
  EXPECT_NEAR ( stats.GetSigma ( 0 ),  16.4065, 0.01 );
 
114
  EXPECT_NEAR ( stats.GetVariance ( 0 ),  269.173, 0.01 );
 
115
  EXPECT_NEAR ( stats.GetCount ( 0 ),  36172, 0.01 );
 
116
  EXPECT_NEAR ( stats.GetSum ( 0 ),  473533, 0.01 );
 
117
  EXPECT_NEAR ( stats.GetMedian ( 0 ), 12.0, 0.001 );
 
118
 
 
119
  ASSERT_EQ( 4u, stats.GetBoundingBox(0).size() );
 
120
  EXPECT_EQ( 0, stats.GetBoundingBox(0)[0] );
 
121
  EXPECT_EQ( 255, stats.GetBoundingBox(0)[1] );
 
122
  EXPECT_EQ( 0, stats.GetBoundingBox(0)[2] );
 
123
  EXPECT_EQ( 255, stats.GetBoundingBox(0)[3] );
 
124
 
 
125
  EXPECT_EQ ( 1.0f, stats.GetProgress() );
 
126
  EXPECT_EQ ( 1.0f, progressCmd.m_Progress );
 
127
  EXPECT_EQ ( 0, abortCmd.m_Count );
 
128
  EXPECT_EQ ( 1, endCmd.m_Count );
 
129
  EXPECT_EQ ( 0, iterCmd.m_Count );
 
130
  EXPECT_EQ ( 1, startCmd.m_Count );
 
131
  EXPECT_EQ ( 0, userCmd.m_Count );
 
132
 
 
133
 // internal filter does not get deleted since there are active measurements
 
134
  EXPECT_EQ ( 0, deleteCmd.m_Count );
 
135
 
 
136
  const std::vector<int64_t> myLabels = stats.GetLabels();
 
137
  EXPECT_EQ ( myLabels.size() , 3u);
 
138
 
 
139
  // const sitk::LabelStatisticsImageFilter::LabelStatisticsMap myMap = stats.GetLabelStatisticsMap();
 
140
  // EXPECT_EQ( myLabels.size() , myMap.size() );
 
141
 
 
142
  // const sitk::MeasurementMap myMeasurementMap = stats.GetMeasurementMap(0);
 
143
  // EXPECT_EQ( myMeasurementMap.size(), 8u ); //4 measurements produced
 
144
 
 
145
  // const sitk::BasicMeasurementMap myBasicMeasurementMap =
 
146
  //   myMeasurementMap.GetBasicMeasurementMap();
 
147
  // EXPECT_EQ( myBasicMeasurementMap.size(), 8u ); //4 measurements produced
 
148
 
 
149
  // EXPECT_EQ ( myMeasurementMap.ToString(), "Count, Maximum, Mean, Minimum, Sigma, Sum, Variance, approxMedian, \n36172, 99, 13.0911, 0, 16.4065, 473533, 269.173, 12, \n" );
 
150
}