Page MenuHomePhabricator

mitkImageCastingTest.cpp

Authored By
engelm
Jul 17 2013, 10:12 AM
Size
3 KB
Referenced Files
None
Subscribers
None

mitkImageCastingTest.cpp

#include "mitkTestingMacros.h"
#include <mitkDataNode.h>
#include <mitkDataNodeFactory.h>
#include "mitkImageCaster.h"
#include "mitkImageWriter.h"
#include <QFile>
#include <QIODevice>
#include <QByteArray>
#include <QCryptographicHash>
class mitkImageCastingTestHelperClass
{
public:
void CastImage( mitk::Image::Pointer image )
{
m_Image = image;
mitk::CastToItkImage( m_Image, m_ItkImage );
mitk::CastToMitkImage( m_ItkImage, m_Image );
}
void CastImageNoMember( mitk::Image::Pointer image )
{
SegmentationImageType::Pointer itkImage;
mitk::CastToItkImage( image, itkImage );
mitk::CastToMitkImage( itkImage, image );
}
static QString CalculateMD5FromFile( QString filename )
{
QFile outputFile( filename );
outputFile.open( QIODevice::ReadOnly );
QByteArray md5 = QCryptographicHash::hash( outputFile.readAll(), QCryptographicHash::Md5);
outputFile.close();
QString md5fromNewFile(md5.toHex());
return md5fromNewFile;
}
typedef itk::Image<unsigned char, 3> SegmentationImageType;
SegmentationImageType::Pointer m_ItkImage;
mitk::Image::Pointer m_Image;
};
/**
* This unittest loads an image that is then casted to an itk::Image and back to an mitk::Image.
* The test then compares the loaded mitk::Image to the resulting, casted mitk::Image.
* The two images must not differ..
*
* The first parameter is the image that is loaded.
* The second parameter is the path where the results will be written.
*/
int mitkImageCastingTest(int argc, char* argv[])
{
MITK_TEST_BEGIN("mitkImageCastingTest")
MITK_TEST_CONDITION_REQUIRED(argc >= 2, "enough command line parameters to load an image?");
mitk::DataNodeFactory::Pointer nodeFac = mitk::DataNodeFactory::New();
nodeFac->SetFileName( argv[1] );
nodeFac->Update();
mitk::DataNode::Pointer node = nodeFac->GetOutput();
QString inputMD5 = mitkImageCastingTestHelperClass::CalculateMD5FromFile( argv[1] );
QString outputDir = argv[2];
mitk::Image::Pointer image = dynamic_cast<mitk::Image*>( node->GetData() );
mitk::ImageWriter::Pointer writer = mitk::ImageWriter::New();
writer->SetFileName( qPrintable( outputDir.append( "rewrittenInput" ) ) );
writer->SetExtension( ".nrrd" );
writer->SetInput( image );
writer->Update();
MITK_INFO << "Writing the read inputfile to " << qPrintable( outputDir );
QString blaMD5 = mitkImageCastingTestHelperClass::CalculateMD5FromFile( outputDir.append( ".nrrd" ) );
MITK_TEST_CONDITION( inputMD5 == blaMD5, "MD5 sums of input image and re-written input image have to be identical!" )
mitkImageCastingTestHelperClass testClass;
testClass.CastImageNoMember( image );
//testClass.CastImage( image );
outputDir = argv[2];
writer->SetFileName( qPrintable( outputDir.append( "output" ) ) );
writer->SetExtension( ".nrrd" );
writer->Update();
MITK_INFO << "Writing the ouputfile to " << qPrintable( outputDir );
QString outputMD5 = mitkImageCastingTestHelperClass::CalculateMD5FromFile( outputDir.append( ".nrrd" ) );
MITK_TEST_CONDITION( inputMD5 == outputMD5, "MD5 sums of input image and output image have to be identical!" )
MITK_TEST_END()
}

File Metadata

Mime Type
text/plain
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
986
Default Alt Text
mitkImageCastingTest.cpp (3 KB)

Event Timeline

Unittest that fails due to the error