Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve ITKReader, testITKio and testITKReader #1379

Merged
merged 14 commits into from
Jan 26, 2019
Merged
2 changes: 2 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
out of range. (Kacper Pluta, [#1359](https://github.com/DGtal-team/DGtal/pull/1359))

- *IO*
- Improve ITKReader, testITKio and testITKReader (Boris Mansencal,
[#1379](https://github.com/DGtal-team/DGtal/pull/1379))
- Fix wrong typedef for double case in ITKReader (Adrien Krähenbühl,
[#1259](https://github.com/DGtal-team/DGtal/pull/1322))
- Fix safeguard when using ImageMagick without cmake activation (David Coeurjolly,
Expand Down
1 change: 1 addition & 0 deletions src/DGtal/images/ImageContainerByITKImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#if defined(__GNUG__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#pragma GCC diagnostic ignored "-Wpedantic"
#endif
#if defined(__clang__)
#pragma clang diagnostic push
Expand Down
1 change: 1 addition & 0 deletions src/DGtal/io/readers/DicomReader.ih
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#if defined(__GNUG__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#pragma GCC diagnostic ignored "-Wpedantic"
#endif
#if defined(__clang__)
#pragma clang diagnostic push
Expand Down
14 changes: 14 additions & 0 deletions src/DGtal/io/readers/ITKReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,21 @@
#include "DGtal/images/CImage.h"
#include "DGtal/base/BasicFunctors.h"
#include "DGtal/io/ITKIOTrait.h"
#if defined(__GNUG__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
#endif
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdocumentation"
#endif
#include <itkImageFileReader.h>
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
#if defined(__GNUG__)
#endif
#pragma GCC diagnostic pop

namespace DGtal
{
Expand Down
51 changes: 28 additions & 23 deletions src/DGtal/io/readers/ITKReader.ih
Original file line number Diff line number Diff line change
Expand Up @@ -58,71 +58,74 @@ namespace DGtal {
const TFunctor & aFunctor )
{
typedef typename Image::Domain Domain;
typedef typename Domain::Point Point;
typedef itk::ImageIOBase::IOComponentType IOComponentType;
BOOST_CONCEPT_ASSERT( (concepts::CUnaryFunctor<TFunctor, ValueOut, Value>));
const IOComponentType componentType = getITKComponentType( filename );
typedef unsigned char ElementType;
switch ( componentType )
{

default:
case itk::ImageIOBase::UNKNOWNCOMPONENTTYPE:
std::cerr << "Unknown and unsupported component type!" << std::endl;
trace.warning() << "[ITKReader] Unknown and unsupported component type! File will be loaded with UCHAR component type" << std::endl;
//fallthrough
case itk::ImageIOBase::UCHAR:
{
typedef ImageContainerByITKImage<Domain, unsigned char> DGtalITKImage;
Domain d;
DGtalITKImage im( d );
return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor );
}
case itk::ImageIOBase::CHAR:
{
typedef ImageContainerByITKImage<Domain, char> DGtalITKImage;
Domain d;
DGtalITKImage im( d );
return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor );
}
case itk::ImageIOBase::USHORT:
{
typedef ImageContainerByITKImage<Domain, unsigned short> DGtalITKImage;
Domain d;
DGtalITKImage im( d );
return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor );
}
case itk::ImageIOBase::SHORT:
{
typedef ImageContainerByITKImage<Domain, short> DGtalITKImage;
Domain d;
DGtalITKImage im( d );
return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor );
}
case itk::ImageIOBase::UINT:
{
typedef ImageContainerByITKImage<Domain, unsigned int> DGtalITKImage;
return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor );
}
case itk::ImageIOBase::INT:
{
typedef ImageContainerByITKImage<Domain, int> DGtalITKImage;
return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor );
}
case itk::ImageIOBase::ULONG:
{
typedef ImageContainerByITKImage<Domain, unsigned long> DGtalITKImage;
Domain d;
DGtalITKImage im( d );
return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor );
}
case itk::ImageIOBase::LONG:
{
typedef ImageContainerByITKImage<Domain, long> DGtalITKImage;
Domain d;
DGtalITKImage im( d );
return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor );
}
case itk::ImageIOBase::ULONGLONG:
{
typedef ImageContainerByITKImage<Domain, unsigned long long> DGtalITKImage;
return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor );
}
case itk::ImageIOBase::LONGLONG:
{
typedef ImageContainerByITKImage<Domain, long long> DGtalITKImage;
return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor );
}
case itk::ImageIOBase::FLOAT:
{
typedef ImageContainerByITKImage<Domain, float> DGtalITKImage;
Domain d;
DGtalITKImage im( d );
return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor );
}
case itk::ImageIOBase::DOUBLE:
{
typedef ImageContainerByITKImage<Domain, double> DGtalITKImage;
Domain d;
DGtalITKImage im( d );
return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor );
}
}
Expand All @@ -138,9 +141,11 @@ namespace DGtal {
{
itk::ImageIOBase::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(
filename.c_str(), itk::ImageIOFactory::ReadMode );
imageIO->SetFileName( filename.c_str() );
imageIO->ReadImageInformation();
componentType = imageIO->GetComponentType();
if (imageIO) {
BorisMansencal marked this conversation as resolved.
Show resolved Hide resolved
imageIO->SetFileName( filename.c_str() );
imageIO->ReadImageInformation();
componentType = imageIO->GetComponentType();
}
}
catch ( itk::ExceptionObject & e )
{
Expand All @@ -160,7 +165,7 @@ namespace DGtal {
typedef typename TypeDGtalImage::ITKImagePointer ITKImagePointer;
typedef typename Image::Domain Domain;

ITKImagePointer itk_image = TypeDGtalImage::ITKImage::New();
ITKImagePointer itk_image = nullptr;

try
{
Expand Down
1 change: 1 addition & 0 deletions src/DGtal/io/writers/ITKWriter.ih
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#if defined(__GNUG__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#pragma GCC diagnostic ignored "-Wpedantic"
#endif
#if defined(__clang__)
#pragma clang diagnostic push
Expand Down
17 changes: 17 additions & 0 deletions tests/io/readers/testITKReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,23 @@ TEST_CASE( "Testing ITKReader" )
testPath + "samples/lobsterCroped16b.mhd", resc );
REQUIRE( ( im( Z3i::Point( 35, 29, 3 ) ) == resc( 60400 ) ) );
}

SECTION(
BorisMansencal marked this conversation as resolved.
Show resolved Hide resolved
"Testing behavior of ITKReader on non existent image file" )
{
bool caughtException = false;
const std::string filename = testPath + "samples/null.mhd"; //non existent file
try {
BorisMansencal marked this conversation as resolved.
Show resolved Hide resolved
Image3DUC im = ITKReader<Image3DUC>::importITK(filename);
}
catch(exception &) {
BorisMansencal marked this conversation as resolved.
Show resolved Hide resolved
caughtException = true;
trace.info() <<"Exception was correctly caught" << std::endl;
}
REQUIRE( caughtException == true);
}


}

/** @ingroup Tests **/
41 changes: 23 additions & 18 deletions tests/io/testITKio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ test_image(const string& filename)

typedef typename std::vector<typename Image::Value> Values;
Values values;
values.reserve(domain.size());
for (typename Domain::Size kk=0; kk<domain.size(); kk++)
values.push_back(rand());

Expand Down Expand Up @@ -103,31 +104,35 @@ bool testITKio()
unsigned int nbok = 0;
unsigned int nb = 0;

nbok += 6;
nb += 8;
trace.beginBlock ( "Testing 2D ITK image value types ..." );
nb += test_image<ImageSelector<Z2i::Domain, int>::Type>("image_2d_int.mha");
nb += test_image<ImageSelector<Z2i::Domain, bool>::Type>("image_2d_bool.mha");
nb += test_image<ImageSelector<Z2i::Domain, unsigned int>::Type>("image_2d_unsigned_int.mha");
nb += test_image<ImageSelector<Z2i::Domain, unsigned char>::Type>("image_2d_unsigned_char.mha");
nb += test_image<ImageSelector<Z2i::Domain, float>::Type>("image_2d_float.mha");
nb += test_image<ImageSelector<Z2i::Domain, double>::Type>("image_2d_double.mha");
nbok += test_image<ImageSelector<Z2i::Domain, int>::Type>("image_2d_int.mha");
nbok += test_image<ImageSelector<Z2i::Domain, bool>::Type>("image_2d_bool.mha");
nbok += test_image<ImageSelector<Z2i::Domain, unsigned int>::Type>("image_2d_unsigned_int.mha");
nbok += test_image<ImageSelector<Z2i::Domain, unsigned char>::Type>("image_2d_unsigned_char.mha");
nbok += test_image<ImageSelector<Z2i::Domain, unsigned long>::Type>("image_2d_unsigned_long.mha");
nbok += test_image<ImageSelector<Z2i::Domain, long>::Type>("image_2d_long.mha");
nbok += test_image<ImageSelector<Z2i::Domain, float>::Type>("image_2d_float.mha");
nbok += test_image<ImageSelector<Z2i::Domain, double>::Type>("image_2d_double.mha");
trace.endBlock();

nbok += 6;
nb += 8;
trace.beginBlock ( "Testing 3D ITK image value types ..." );
nb += test_image<ImageSelector<Z3i::Domain, int>::Type>("image_3d_int.mha");
nb += test_image<ImageSelector<Z3i::Domain, bool>::Type>("image_3d_bool.mha");
nb += test_image<ImageSelector<Z3i::Domain, unsigned int>::Type>("image_3d_unsigned_int.mha");
nb += test_image<ImageSelector<Z3i::Domain, unsigned char>::Type>("image_3d_unsigned_char.mha");
nb += test_image<ImageSelector<Z3i::Domain, float>::Type>("image_3d_float.mha");
nb += test_image<ImageSelector<Z3i::Domain, double>::Type>("image_3d_double.mha");
nbok += test_image<ImageSelector<Z3i::Domain, int>::Type>("image_3d_int.mha");
nbok += test_image<ImageSelector<Z3i::Domain, bool>::Type>("image_3d_bool.mha");
nbok += test_image<ImageSelector<Z3i::Domain, unsigned int>::Type>("image_3d_unsigned_int.mha");
nbok += test_image<ImageSelector<Z3i::Domain, unsigned char>::Type>("image_3d_unsigned_char.mha");
nbok += test_image<ImageSelector<Z3i::Domain, unsigned long>::Type>("image_3d_unsigned_long.mha");
nbok += test_image<ImageSelector<Z3i::Domain, long>::Type>("image_3d_long.mha");
nbok += test_image<ImageSelector<Z3i::Domain, float>::Type>("image_3d_float.mha");
nbok += test_image<ImageSelector<Z3i::Domain, double>::Type>("image_3d_double.mha");
trace.endBlock();

nbok += 3;
nb += 3;
trace.beginBlock ( "Testing 2D ITK image formats ..." );
nb += test_image<ImageSelector<Z2i::Domain, unsigned char>::Type>("image_unsigned_char.jpg"); nbok--; // jpg is lossy
nb += test_image<ImageSelector<Z2i::Domain, unsigned char>::Type>("image_unsigned_char.png");
nb += test_image<ImageSelector<Z2i::Domain, unsigned char>::Type>("image_unsigned_char.bmp");
nbok += test_image<ImageSelector<Z2i::Domain, unsigned char>::Type>("image_unsigned_char.jpg"); nb--; // jpg is lossy
nbok += test_image<ImageSelector<Z2i::Domain, unsigned char>::Type>("image_unsigned_char.png");
nbok += test_image<ImageSelector<Z2i::Domain, unsigned char>::Type>("image_unsigned_char.bmp");
trace.endBlock();

trace.info() << "(" << nbok << "/" << nb << ") " << endl;
Expand Down