[Insight-users] Why GradientMagnitudeImageFilter produce zero value image?

Luis Ibanez luis.ibanez@kitware.com
Fri, 06 Dec 2002 07:29:58 -0500


Hi Fucang,

You seem to be using "unsigned char" for
representing the pixels of both the input
image and the output image. If your image
is very smooth it is likely that the values
on the output are being truncated.

Have you tried instantiating the GradientMagnitude
filter with both images being FloatImageType ?

like:

typedef itk::GradientMagnitudeImageFilter<
                FloatImageType,
                FloatImageType
                                >  FilterType;


Note that you will need a casting to <unsigned
short> or to <unsingned char> pixel type before
writing the resulting image to a PNG file. These
are the only pixel types supported by PNG.

You can save the image in floats using a MetaImageIO
and then view it with the MetaImageViewer in
Insight/Applications.

You may want to take a look at the example in:

  Insight/Examples/Filtering/
             GradientMagnitudeImageFilter.cxx


Note that you no longer need to provide the ImageIO
object to the writer. The ImageFileWriter is now
using the same factory mechanism of the reader in
order to find an ImageIO based on the image filename
extension.


Please let us know if you encounter further problems,


  Thanks

    Luis


============================================

Fucang Jia wrote:

> Hi, everyone,
> 
> I am studying an example in the ITK tutorial to calculate the gradient magnitude of an image, the ouput image is all zero valued pixel. I do not know the reason, I also think about the rescaling, but rescale filter does not work. Can anyone tell me the error?
> 
> Thanks!
> 
> Fucang
> 
> =======================================================
> 
> Here is the example code:
> 
> #include "itkImage.h"
> #include "itkImageFileWriter.h"
> #include "itkImageFileReader.h"
> #include "itkPNGImageIO.h"
> #include "itkMetaImageIO.h"
> #include "itkGradientMagnitudeImageFilter.h"
> 
> 
> int main( int argc, char **argv ) {
> typedef itk::Image<unsigned char, 2>      ImageType;
> typedef itk::Image<float,2>     FloatImageType;
> typedef itk::ImageFileReader<ImageType>    ReaderType;
> typedef itk::ImageFileWriter<ImageType>    WriterType;
> typedef itk::GradientMagnitudeImageFilter<
>                      ImageType,ImageType>  FilterType;
> 
> ReaderType::Pointer reader = ReaderType::New();
> WriterType::Pointer writer = WriterType::New();
> FilterType::Pointer filter = FilterType::New();
> 
> reader->SetFileName( argv[1] );
> filter->SetInput( reader->GetOutput() );
> writer->SetImageIO( itk::PNGImageIO::New() );
> writer->SetFileName( argv[2] );
> writer->SetInput( filter->GetOutput() );
> writer->Update();
> 
> return 0;
> }