Page MenuHomePhabricator

DirectOverlayTest.cpp

Authored By
morency
May 10 2013, 6:14 PM
Size
3 KB
Referenced Files
None
Subscribers
None

DirectOverlayTest.cpp

#include <mitkTestingMacros.h>
#include <mitkImage.h>
#include <mitkIOUtil.h>
#include <mitkImageCast.h>
#include <mitkITKImageImport.h>
#include <mitkImageAccessByItk.h>
#include <itkTestingComparisonImageFilter.h>
#include <itkBinaryThresholdImageFilter.h>
#include <itkImageFileWriter.h>
#include <boost/lexical_cast.hpp>
class DirectOverlayTestClass
{
public:
template<typename TPixel, unsigned int VDimensions>
static void InternalThreshold(
const itk::Image<TPixel, VDimensions>* image,
mitk::Image::Pointer& output,
const double th[])
{
typedef itk::Image<TPixel, VDimensions> InputImageType;
typedef itk::Image<unsigned int, VDimensions> OutputImageType;
typedef itk::BinaryThresholdImageFilter<
InputImageType, OutputImageType> BinaryThresholdFilterType;
typename BinaryThresholdFilterType::Pointer thresholder =
BinaryThresholdFilterType::New();
thresholder->SetInput(image);
thresholder->SetLowerThreshold(th[0]);
thresholder->SetUpperThreshold(th[1]);
thresholder->SetInsideValue(255);
thresholder->SetOutsideValue(0);
thresholder->Update();
try
{
output = mitk::ImportItkImage(thresholder->GetOutput());
//mitk::IOUtil::SaveImage(output, "/tmp/output_InternalThreshold.nii");
}
catch(itk::ExceptionObject&)
{
MITK_TEST_FAILED_MSG(<<"Thresholding computation failed");
}
}
static void TestOverlay(mitk::Image::Pointer original,
mitk::Image::Pointer truth,
const double lower,
const double upper)
{
mitk::Image::Pointer overlayImage;
const double th[] = {lower, upper};
AccessByItk_2(original, InternalThreshold,
overlayImage,
th);
//mitk::IOUtil::SaveImage(overlayImage, "/tmp/overlayImage_TestOverlay.nii");
//mitk::IOUtil::SaveImage(truth, "/tmp/truth_TestOverlay.nii");
typedef itk::Image<unsigned int, 3> InputImageType;
InputImageType::Pointer overlayItk;
mitk::CastToItkImage(overlayImage, overlayItk);
//typedef itk::ImageFileWriter< InputImageType > WriterType;
//WriterType::Pointer writer = WriterType::New();
//writer->SetFileName("/tmp/overlayITK_TestOverlay.nii");
//writer->SetInput(overlayItk);
//writer->Update();
InputImageType::Pointer truthItk;
mitk::CastToItkImage(truth, truthItk);
typedef itk::Testing::ComparisonImageFilter
<InputImageType, InputImageType> ComparisonImageFilterType;
ComparisonImageFilterType::Pointer comp =
ComparisonImageFilterType::New();
comp->SetValidInput(truthItk);
comp->SetTestInput(overlayItk);
comp->Update();
MITK_TEST_CONDITION_REQUIRED(
comp->GetNumberOfPixelsWithDifferences() == 0,
"Comparing overlay with ground truth")
}
static void TestDirectOverlay(char* in, char* gt,
const int lower, const int upper)
{
mitk::Image::Pointer original = mitk::IOUtil::LoadImage(in);
mitk::Image::Pointer truth = mitk::IOUtil::LoadImage(gt);
if(original.IsNotNull() && original->GetDimension() == 3
&& truth.IsNotNull() && truth->GetDimension() == 3
&& upper > lower)
{
TestOverlay(original, truth, lower, upper);
}
else
{
MITK_TEST_FAILED_MSG(<<"Invalid parameters");
}
}
};
int DirectOverlayTest(int argc, char* argv[])
{
MITK_TEST_BEGIN("DirectOverlay")
MITK_TEST_CONDITION_REQUIRED(argc >= 5,
"File to load has been specified on the command line");
unsigned int lower = 0,
upper = 0;
try
{
lower = boost::lexical_cast<double>(argv[3]);
upper = boost::lexical_cast<double>(argv[4]);
}
catch(std::exception& e)
{
MITK_TEST_FAILED_MSG(<<e.what());
}
DirectOverlayTestClass::TestDirectOverlay(argv[1], argv[2], lower, upper);
MITK_TEST_END()
}

File Metadata

Mime Type
text/x-c++src
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
961
Default Alt Text
DirectOverlayTest.cpp (3 KB)