1
/*=========================================================================
3
* Copyright Insight Software Consortium
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
9
* http://www.apache.org/licenses/LICENSE-2.0.txt
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.
17
*=========================================================================*/
18
#include <SimpleITKTestHarness.h>
19
#include <SimpleITK.h>
21
#include "itkVectorImage.h"
25
TEST(LabelStatistics,Simple) {
26
itk::simple::ImageFileReader reader;
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();
32
itk::simple::LabelStatisticsImageFilter lsFilter;
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());
45
lsFilter.Execute ( intensityImage, labelImage );
46
} catch ( itk::ExceptionObject e ) {
47
std::cout << "LabelStatistics failed: " << e.what() << std::endl;
50
std::vector<int64_t> labels = lsFilter.GetLabels();
51
for(std::vector<int64_t>::const_iterator i = labels.begin(); i != labels.end(); ++i)
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 );
63
ASSERT_EQ(lsFilter.GetSum (0) , 0 );
64
ASSERT_EQ(lsFilter.GetCount(0) , 33390u );
68
TEST(LabelStatistics,Commands) {
69
namespace sitk = itk::simple;
71
sitk::Image image = sitk::ReadImage ( dataFinder.GetFile ( "Input/cthead1.png" ) );
72
sitk::Image labels = sitk::ReadImage ( dataFinder.GetFile ( "Input/2th_cthead1.mha" ) );
74
sitk::LabelStatisticsImageFilter stats;
77
ProgressUpdate progressCmd(stats);
78
stats.AddCommand(sitk::sitkProgressEvent, progressCmd);
80
CountCommand abortCmd(stats);
81
stats.AddCommand(sitk::sitkAbortEvent, abortCmd);
83
CountCommand deleteCmd(stats);
84
stats.AddCommand(sitk::sitkDeleteEvent, deleteCmd);
86
CountCommand endCmd(stats);
87
stats.AddCommand(sitk::sitkEndEvent, endCmd);
89
CountCommand iterCmd(stats);
90
stats.AddCommand(sitk::sitkIterationEvent, iterCmd);
92
CountCommand startCmd(stats);
93
stats.AddCommand(sitk::sitkStartEvent, startCmd);
95
CountCommand userCmd(stats);
96
stats.AddCommand(sitk::sitkUserEvent, userCmd);
100
stats.Execute ( image, labels );
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 ) );
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 );
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] );
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 );
133
// internal filter does not get deleted since there are active measurements
134
EXPECT_EQ ( 0, deleteCmd.m_Count );
136
const std::vector<int64_t> myLabels = stats.GetLabels();
137
EXPECT_EQ ( myLabels.size() , 3u);
139
// const sitk::LabelStatisticsImageFilter::LabelStatisticsMap myMap = stats.GetLabelStatisticsMap();
140
// EXPECT_EQ( myLabels.size() , myMap.size() );
142
// const sitk::MeasurementMap myMeasurementMap = stats.GetMeasurementMap(0);
143
// EXPECT_EQ( myMeasurementMap.size(), 8u ); //4 measurements produced
145
// const sitk::BasicMeasurementMap myBasicMeasurementMap =
146
// myMeasurementMap.GetBasicMeasurementMap();
147
// EXPECT_EQ( myBasicMeasurementMap.size(), 8u ); //4 measurements produced
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" );