1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#include "itkGTest.h"
#include "itkImage.h"
#include "itkLabelImageToStatisticsLabelMapFilter.h"
#include "itkImageRegionIterator.h"
#include <algorithm>
namespace
{
class StatisticsLabelMapFixture : public ::testing::Test
{
public:
StatisticsLabelMapFixture() = default;
~StatisticsLabelMapFixture() override = default;
protected:
void
SetUp() override
{}
void
TearDown() override
{}
template <unsigned int D, typename TPixelType = unsigned short>
struct FixtureUtilities
{
static const unsigned int Dimension = D;
using PixelType = TPixelType;
using ImageType = itk::Image<PixelType, Dimension>;
using LabelPixelType = unsigned char;
using LabelImageType = itk::Image<LabelPixelType, Dimension>;
using LabelObjectType = itk::StatisticsLabelObject<LabelPixelType, Dimension>;
using StatisticsLabelMapType = itk::LabelMap<LabelObjectType>;
static typename ImageType::Pointer
CreateImage()
{
auto image = ImageType::New();
typename ImageType::SizeType imageSize;
imageSize.Fill(25);
image->SetRegions(typename ImageType::RegionType(imageSize));
image->Allocate();
image->FillBuffer(0);
return image;
}
static typename ImageType::Pointer
CreateImageRandom(PixelType randMax = 500, unsigned int randSeed = 0)
{
auto image = ImageType::New();
typename ImageType::SizeType imageSize;
imageSize.Fill(25);
image->SetRegions(typename ImageType::RegionType(imageSize));
image->Allocate();
image->FillBuffer(0);
srand(randSeed);
itk::ImageRegionIterator<ImageType> it(image, image->GetLargestPossibleRegion());
while (!it.IsAtEnd())
{
it.Set(rand() % randMax);
++it;
}
return image;
}
static typename LabelImageType::Pointer
CreateLabelImage()
{
auto image = LabelImageType::New();
typename LabelImageType::SizeType imageSize;
imageSize.Fill(25);
image->SetRegions(typename ImageType::RegionType(imageSize));
image->Allocate();
image->FillBuffer(0);
return image;
}
static typename LabelObjectType::ConstPointer
ComputeLabelObject(const LabelImageType * labelImage,
const ImageType * image,
const PixelType label = 1,
const size_t numberOfBins = 0)
{
auto l2s = itk::LabelImageToStatisticsLabelMapFilter<LabelImageType, ImageType>::New();
l2s->SetInput1(labelImage);
l2s->SetFeatureImage(image);
l2s->ComputeFeretDiameterOn();
l2s->ComputePerimeterOn();
// l2s->ComputeOrientedBoundingBoxOn();
l2s->ComputeHistogramOn();
if (numberOfBins != 0)
{
l2s->SetNumberOfBins(numberOfBins);
}
l2s->Update();
return l2s->GetOutput()->GetLabelObject(label);
}
static double
ComputeExactMedian(const LabelObjectType * labelObject, const ImageType * image)
{
std::vector<PixelType> values;
typename LabelObjectType::ConstIndexIterator it(labelObject);
while (!it.IsAtEnd())
{
const typename ImageType::IndexType & idx = it.GetIndex();
values.push_back(image->GetPixel(idx));
++it;
}
std::sort(values.begin(), values.end());
assert(!values.empty());
auto n1 = values.size() / 2;
if (values.size() % 2 == 0)
{
return 0.5 * (static_cast<double>(values[n1]) + static_cast<double>(values[n1 - 1]));
}
return values[n1];
}
};
};
} // namespace
TEST_F(StatisticsLabelMapFixture, 2D_zero)
{
using Utils = FixtureUtilities<2, unsigned char>;
using namespace itk::GTest::TypedefsAndConstructors::Dimension2;
auto image = Utils::CreateImage();
auto labelImage = Utils ::CreateLabelImage();
Utils::LabelPixelType label = 1;
labelImage->FillBuffer(label);
Utils::LabelObjectType::ConstPointer labelObject = Utils::ComputeLabelObject(labelImage, image, 1, 1 << 8);
ASSERT_GT(labelObject->Size(), 0);
EXPECT_NEAR(0.0, labelObject->GetMinimum(), 1e-12);
EXPECT_NEAR(0.0, labelObject->GetMaximum(), 1e-12);
EXPECT_NEAR(Utils::ComputeExactMedian(labelObject, image), labelObject->GetMedian(), 1e-12);
EXPECT_NEAR(0.0, labelObject->GetSum(), 1e-12);
EXPECT_NEAR(0.0, labelObject->GetVariance(), 1e-12);
EXPECT_NEAR(0.0, labelObject->GetStandardDeviation(), 0.5);
if (::testing::Test::HasFailure())
{
labelObject->Print(std::cout);
}
}
TEST_F(StatisticsLabelMapFixture, 2D_ones_with_outliers)
{
using Utils = FixtureUtilities<2, short>;
using namespace itk::GTest::TypedefsAndConstructors::Dimension2;
auto image = Utils::CreateImage();
Utils::PixelType value = 1;
image->FillBuffer(value);
// Test with outliers outside the label.
image->SetPixel(itk::MakeIndex(0, 0), 32000);
image->SetPixel(itk::MakeIndex(0, 1), -32000);
auto labelImage = Utils ::CreateLabelImage();
Utils::LabelPixelType label = 1;
labelImage->FillBuffer(label);
labelImage->SetPixel(itk::MakeIndex(0, 0), 0);
labelImage->SetPixel(itk::MakeIndex(0, 1), 0);
Utils::LabelObjectType::ConstPointer labelObject = Utils::ComputeLabelObject(labelImage, image, label, 1 << 16);
ASSERT_GT(labelObject->Size(), 0);
EXPECT_NEAR(value, labelObject->GetMinimum(), 1e-12);
EXPECT_NEAR(value, labelObject->GetMaximum(), 1e-12);
EXPECT_NEAR(Utils::ComputeExactMedian(labelObject, image), labelObject->GetMedian(), 1e-12);
EXPECT_NEAR(25 * 25 - 2, labelObject->GetSum(), 1e-12);
EXPECT_NEAR(0.0, labelObject->GetVariance(), 1e-12);
EXPECT_NEAR(0.0, labelObject->GetStandardDeviation(), 1e-12);
if (::testing::Test::HasFailure())
{
labelObject->Print(std::cout);
}
}
TEST_F(StatisticsLabelMapFixture, 2D_rand_with_outliers)
{
using Utils = FixtureUtilities<2, short>;
using namespace itk::GTest::TypedefsAndConstructors::Dimension2;
auto image = Utils::CreateImageRandom(500, 0);
auto labelImage = Utils ::CreateLabelImage();
// Test with outliers outside the label.
image->SetPixel(itk::MakeIndex(0, 0), 32000);
image->SetPixel(itk::MakeIndex(0, 1), -2000);
// Set min/max in label
image->SetPixel(itk::MakeIndex(0, 2), 0);
image->SetPixel(itk::MakeIndex(0, 3), 500);
Utils::LabelPixelType label = 1;
labelImage->FillBuffer(label);
labelImage->SetPixel(itk::MakeIndex(0, 0), 0);
labelImage->SetPixel(itk::MakeIndex(0, 1), 0);
Utils::LabelObjectType::ConstPointer labelObject = Utils::ComputeLabelObject(labelImage, image, label, 1 << 16);
ASSERT_GT(labelObject->Size(), 0);
EXPECT_NEAR(0.0, labelObject->GetMinimum(), 1e-12);
EXPECT_NEAR(500.0, labelObject->GetMaximum(), 1e-12);
EXPECT_NEAR(Utils::ComputeExactMedian(labelObject, image), labelObject->GetMedian(), 1e-12);
if (::testing::Test::HasFailure())
{
labelObject->Print(std::cout);
}
}
TEST_F(StatisticsLabelMapFixture, 2D_even)
{
using Utils = FixtureUtilities<2, unsigned char>;
using namespace itk::GTest::TypedefsAndConstructors::Dimension2;
auto image = Utils::CreateImage();
auto labelImage = Utils ::CreateLabelImage();
// Set label with two elements far apart, the median should be average
image->SetPixel(itk::MakeIndex(0, 0), 10);
image->SetPixel(itk::MakeIndex(0, 1), 100);
image->SetPixel(itk::MakeIndex(0, 2), 1);
image->SetPixel(itk::MakeIndex(0, 3), 200);
Utils::LabelPixelType label = 1;
labelImage->SetPixel(itk::MakeIndex(0, 0), label);
labelImage->SetPixel(itk::MakeIndex(0, 1), label);
labelImage->SetPixel(itk::MakeIndex(0, 2), label);
labelImage->SetPixel(itk::MakeIndex(0, 3), label);
Utils::LabelObjectType::ConstPointer labelObject = Utils::ComputeLabelObject(labelImage, image, label, 1 << 8);
ASSERT_GT(labelObject->Size(), 0);
EXPECT_NEAR(1.0, labelObject->GetMinimum(), 1e-12);
EXPECT_NEAR(200.0, labelObject->GetMaximum(), 1e-12);
EXPECT_NEAR(Utils::ComputeExactMedian(labelObject, image), labelObject->GetMedian(), 1e-12);
EXPECT_NEAR(311.0, labelObject->GetSum(), 1e-12);
EXPECT_NEAR(8640.25, labelObject->GetVariance(), 1e-10);
EXPECT_NEAR(92.95294, labelObject->GetStandardDeviation(), 1e-5);
if (::testing::Test::HasFailure())
{
labelObject->Print(std::cout);
}
}
TEST_F(StatisticsLabelMapFixture, 2D_three)
{
using Utils = FixtureUtilities<2, unsigned char>;
using namespace itk::GTest::TypedefsAndConstructors::Dimension2;
auto image = Utils::CreateImage();
auto labelImage = Utils ::CreateLabelImage();
// Set label with two elements far apart, the median should be average
image->SetPixel(itk::MakeIndex(0, 0), 1);
image->SetPixel(itk::MakeIndex(0, 1), 3);
image->SetPixel(itk::MakeIndex(0, 2), 10);
Utils::LabelPixelType label = 1;
labelImage->SetPixel(itk::MakeIndex(0, 0), label);
labelImage->SetPixel(itk::MakeIndex(0, 1), label);
labelImage->SetPixel(itk::MakeIndex(0, 2), label);
Utils::LabelObjectType::ConstPointer labelObject = Utils::ComputeLabelObject(labelImage, image, label, 1 << 8);
ASSERT_GT(labelObject->Size(), 0);
EXPECT_NEAR(1.0, labelObject->GetMinimum(), 1e-12);
EXPECT_NEAR(10.0, labelObject->GetMaximum(), 1e-12);
EXPECT_NEAR(Utils::ComputeExactMedian(labelObject, image), labelObject->GetMedian(), 1e-12);
EXPECT_NEAR(14.0, labelObject->GetSum(), 1e-12);
EXPECT_NEAR(22.33333, labelObject->GetVariance(), 1e-5);
EXPECT_NEAR(4.725815, labelObject->GetStandardDeviation(), 1e-5);
if (::testing::Test::HasFailure())
{
labelObject->Print(std::cout);
}
}
|