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
|
/*=========================================================================
*
* 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 "itkSliceBySliceImageFilter.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkSimpleFilterWatcher.h"
#include "itkPipelineMonitorImageFilter.h"
#include "itkTestingMacros.h"
#include "itkMedianImageFilter.h"
void
sliceCallBack(itk::Object * object, const itk::EventObject &, void *)
{
// the same type alias than in the main function - should be done in a nicer way
constexpr int Dimension = 3;
using PixelType = unsigned char;
using ImageType = itk::Image<PixelType, Dimension>;
using FilterType = itk::SliceBySliceImageFilter<ImageType, ImageType>;
using MedianType = itk::MedianImageFilter<FilterType::InternalInputImageType, FilterType::InternalOutputImageType>;
// real stuff begins here
// get the slice by slice filter and the median filter
auto * filter = dynamic_cast<FilterType *>(object);
auto * median = dynamic_cast<MedianType *>(filter->GetModifiableInputFilter());
// std::cout << "callback! slice: " << filter->GetSliceIndex() << std::endl;
// set half of the slice number as radius
MedianType::InputSizeType radius;
radius.Fill(filter->GetSliceIndex() / 2);
median->SetRadius(radius);
}
int
itkSliceBySliceImageFilterTest(int argc, char * argv[])
{
if (argc != 4)
{
std::cerr << "usage: " << itkNameOfTestExecutableMacro(argv) << " input output slicingDimension" << std::endl;
return EXIT_FAILURE;
}
constexpr int Dimension = 3;
using PixelType = unsigned char;
using ImageType = itk::Image<PixelType, Dimension>;
using ReaderType = itk::ImageFileReader<ImageType>;
auto reader = ReaderType::New();
reader->SetFileName(argv[1]);
using FilterType = itk::SliceBySliceImageFilter<ImageType, ImageType>;
auto filter = FilterType::New();
ITK_EXERCISE_BASIC_OBJECT_METHODS(filter, SliceBySliceImageFilter, ImageToImageFilter);
filter->DebugOn();
filter->SetInput(reader->GetOutput());
using MedianType = itk::MedianImageFilter<FilterType::InternalInputImageType, FilterType::InternalOutputImageType>;
auto median = MedianType::New();
filter->SetFilter(median);
ITK_TEST_SET_GET_VALUE(median, filter->GetFilter());
using MonitorType = itk::PipelineMonitorImageFilter<FilterType::InternalOutputImageType>;
auto monitor = MonitorType::New();
itk::CStyleCommand::Pointer command = itk::CStyleCommand::New();
command->SetCallback(*sliceCallBack);
filter->AddObserver(itk::IterationEvent(), command);
itk::SimpleFilterWatcher watcher(filter, "filter");
using WriterType = itk::ImageFileWriter<ImageType>;
auto writer = WriterType::New();
writer->SetInput(filter->GetOutput());
writer->SetFileName(argv[2]);
unsigned int slicingDimension;
std::istringstream istrm(argv[3]);
istrm >> slicingDimension;
filter->SetDimension(slicingDimension);
ITK_TEST_SET_GET_VALUE(slicingDimension, filter->GetDimension());
try
{
writer->Update();
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
// set up a requested region of just one pixel and verify that was
// all that was produced.
std::cout << "Testing with requested region..." << std::endl;
ImageType::Pointer temp = filter->GetOutput();
temp->DisconnectPipeline();
temp = nullptr;
ImageType::RegionType rr = reader->GetOutput()->GetLargestPossibleRegion();
for (unsigned int i = 0; i < ImageType::ImageDimension; ++i)
{
rr.SetIndex(i, rr.GetIndex(i) + rr.GetSize(i) / 2);
rr.SetSize(i, 1);
}
monitor->SetInput(median->GetOutput());
filter->SetOutputFilter(monitor);
ITK_TEST_SET_GET_VALUE(monitor, filter->GetOutputFilter());
filter->GetOutput()->SetRequestedRegion(rr);
try
{
filter->Update();
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
// check that one slice executed is just one pixel and the input
// filter just update that region
ITK_TEST_EXPECT_EQUAL(monitor->GetNumberOfUpdates(), 1);
ITK_TEST_EXPECT_EQUAL(monitor->GetOutputRequestedRegions()[0].GetNumberOfPixels(), 1);
ITK_TEST_EXPECT_TRUE(monitor->VerifyAllInputCanStream(1));
//
// Test that a sliced version of the input image information is passed
// through to the internal filters, with proper origin and
// spacing. We are setting the input image to have a non-zero
// starting index.
//
auto image = ImageType::New();
{
ImageType::RegionType region = reader->GetOutput()->GetLargestPossibleRegion();
region.SetIndex(0, 10);
image->SetRegions(region);
image->AllocateInitialized();
}
ImageType::SpacingType spacing;
ImageType::PointType origin;
for (unsigned int i = 0; i < ImageType::ImageDimension; ++i)
{
spacing[i] = i + 0.1;
origin[i] = i + 0.2;
}
image->SetSpacing(spacing);
image->SetOrigin(origin);
filter->SetInput(image);
filter->Update();
FilterType::InternalInputImageType::SpacingType expectedInternalSpacing;
FilterType::InternalInputImageType::PointType expectedInternalOrigin;
for (unsigned int i = 0, internal_i = 0; internal_i < FilterType::InternalImageDimension; ++i, ++internal_i)
{
if (i == slicingDimension)
{
++i;
}
expectedInternalSpacing[internal_i] = spacing[i];
expectedInternalOrigin[internal_i] = origin[i];
}
ITK_TEST_EXPECT_EQUAL(monitor->GetUpdatedOutputSpacing(), expectedInternalSpacing);
ITK_TEST_EXPECT_EQUAL(monitor->GetUpdatedOutputOrigin(), expectedInternalOrigin);
//
// Exercise exceptions
//
bool caughtException;
auto badFilter = FilterType::New();
std::cout << "Testing with no filter set..." << std::endl;
badFilter->SetInput(reader->GetOutput());
caughtException = false;
try
{
badFilter->Update();
}
catch (const itk::ExceptionObject & excp)
{
std::cout << "Caught expected exception" << std::endl;
std::cout << excp << std::endl;
caughtException = true;
}
if (!caughtException)
{
return EXIT_FAILURE;
}
std::cout << "Testing with no output filter set..." << std::endl;
badFilter->SetInput(reader->GetOutput());
badFilter->SetInputFilter(median);
ITK_TEST_SET_GET_VALUE(median, badFilter->GetInputFilter());
caughtException = false;
try
{
badFilter->Update();
}
catch (const itk::ExceptionObject & excp)
{
std::cout << "Caught expected exception" << std::endl;
std::cout << excp << std::endl;
caughtException = true;
}
if (!caughtException)
{
return EXIT_FAILURE;
}
// check nullptr input/output
ITK_TRY_EXPECT_EXCEPTION(badFilter->SetInputFilter(nullptr));
ITK_TRY_EXPECT_EXCEPTION(badFilter->SetOutputFilter(nullptr));
return EXIT_SUCCESS;
}
|