[ITK-users] Fwd: [ITK] Using C++ functions in ITK pipeline

Nicolas Gallego nicgallego at gmail.com
Thu Jul 17 09:46:19 EDT 2014


Hi Elena,
I compiled and tested your example code.
I found that there was a conflict between the return of the function and
the argument (output),
also I think output allocation on the function is not necesary since you
assign it a valid output.
Here a version of your code that worked.


Nicolás Gallego-Ortiz
Université catholique de Louvain, Belgium


2014-07-17 14:43 GMT+02:00 elena bresciani <elena.bresciani87 at gmail.com>:

> Hello guys,
>
> I'm developing a really complicated pipeline and to avoid repetitions and
> unreadeble code I thought of writing the different pieces of code as
> functions, each with a single specific purpose.
> Since I'm not really a C++ master I started with a very simple pipeline
> (reader -> rescale intensity filter -> writer), where the rescale intensity
> filter is in a separate function, outside the main().
> The code compiles and it even runs but the output is not what it would
> have to be.
>
> Can someone help me find where is the error?
>
> Attached you can find my code, a test image and the respective output.
>
> Thanks in advance
>
> Elena
>
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.php
>
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/insight-users
>
> _______________________________________________
> Community mailing list
> Community at itk.org
> http://public.kitware.com/mailman/listinfo/community
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/insight-users/attachments/20140717/1115dd32/attachment.html>
-------------- next part --------------
PROJECT(funzioni)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

FIND_PACKAGE(ITK)
IF(ITK_FOUND)
    INCLUDE(${ITK_USE_FILE})
ELSE(ITK_FOUND)
    MESSAGE(FATAL_ERROR
            "ITK not found. Please set ITK_DIR.")
ENDIF(ITK_FOUND)

ADD_EXECUTABLE( testFunzioni testFunzioni.cxx )
TARGET_LINK_LIBRARIES( testFunzioni ${ITK_LIBRARIES} )
-------------- next part --------------
A non-text attachment was scrubbed...
Name: TEST.jpg
Type: image/jpeg
Size: 3641 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/insight-users/attachments/20140717/1115dd32/attachment.jpg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: myTestOut.jpg
Type: image/jpeg
Size: 2342 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/insight-users/attachments/20140717/1115dd32/attachment-0001.jpg>
-------------- next part --------------
#include <iostream>
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkRescaleIntensityImageFilter.h"

typedef itk::Image < unsigned char, 2 > ImageType;

// may be a conflict between return value and outImage as argument by reference
//ImageType::Pointer RescaleIntensity (ImageType::Pointer inputImage, ImageType::Pointer outputImage);
ImageType::Pointer RescaleIntensity (ImageType::Pointer inputImage);

int main (int argc, char * argv[])

    {
    typedef itk::ImageFileReader < ImageType > ReaderType;
    ReaderType::Pointer reader = ReaderType::New();
    
    reader -> SetFileName (argv[1]);
    try {
        reader -> Update();
    } catch (itk::ExceptionObject & excp)
    {
            std::cerr << "Problem encountered while reading file: " << std::endl;
            std::cerr << excp << std::endl;
            return EXIT_FAILURE;
    }

    ImageType::Pointer inputImg = reader -> GetOutput();
    
//    ImageType::Pointer outputImage = ImageType::New();
    ImageType::Pointer outputImage = RescaleIntensity( inputImg );
//    RescaleIntensity (inputImg, outputImage);

    typedef itk::ImageFileWriter < ImageType > WriterType;
    WriterType::Pointer writer = WriterType::New();
    
    writer -> SetFileName (argv[2]);
    writer -> SetInput (outputImage);
//    writer->SetInput( rescale->GetOutput() );
    
    try
        {
        writer->Update();
        }
    catch (itk::ExceptionObject & excp)
        {
        std::cerr << "Problem encountered while writing file: " << std::endl;
        std::cerr << excp << std::endl;
        return EXIT_FAILURE;
        }
    
    return EXIT_SUCCESS;
    
    }
    
//ImageType::Pointer RescaleIntensity (ImageType::Pointer inputImage, ImageType::Pointer outputImage)
ImageType::Pointer RescaleIntensity(ImageType::Pointer inputImage)
{
//    std::cout << inputImage << std::endl;
//    outputImage -> SetRegions (inputImage -> GetLargestPossibleRegion());
//    outputImage -> SetSpacing (inputImage ->GetSpacing());
//    outputImage -> Allocate();
    
    typedef itk::RescaleIntensityImageFilter < ImageType, ImageType > RescaleType;
    RescaleType::Pointer rescale = RescaleType::New();
    
    rescale -> SetInput (inputImage);
    rescale -> SetOutputMinimum (0);
    rescale -> SetOutputMaximum (255);
    rescale -> Update();
    return rescale->GetOutput();
//    outputImage = rescale -> GetOutput();
    
//    return outputImage;
}

//////////////
//typedef itk::RescaleIntensityImageFilter < ImageType, ImageType > RescaleType;
//RescaleType::Pointer rescale = RescaleType::New();

//rescale -> SetInput ( reader->GetOutput() );
//rescale -> SetOutputMinimum (0);
//rescale -> SetOutputMaximum (255);


More information about the Insight-users mailing list