[Insight-users] How to write the Image after Iterator operation
Luis Ibanez
luis.ibanez@kitware.com
Mon, 06 Jan 2003 08:34:03 -0500
Hi Fucang
The compilation error is telling that
the writer->SetInput() method is expecting
a pointer to an image, not the iterator "it".
You may want to rather call:
writer->SetInput( image );
Your code looks good for the most part,
the only strange thing is that you are
manipulating the image in the pipeline.
This may not be the best approach.
It is not wise to modify the image taken
from the output of a filter since the filter
is capable of changing the image content at
any moment and you will loose any modifications.
For example, as soon as you Update() the writer,
an update message will be propagated to the reader
and your image may be overwritten.
I would suggest you to use the
BinaryThresholdImageFilter:
http://www.itk.org/Insight/Doxygen/html/classitk_1_1BinaryThresholdImageFilter.html
instead of writing the iterator cycle by yourself.
The pipeline could look like
reader --> binaryThreshold --> writer
| ^
| |
+---> Otsu ----+
and you can still use the Otsu calculator
for computing the threshold to be given as
input to the BinaryThreshold image filter.
You may want to take a look at the code
of the itkBinaryThresholdImageFilter
(in Insight/Code/BasicFilters) just to verify
that is doing exactly the same loop that you
already wrote.
A documented example on the use of the
BinaryThreshold filter is availabe under
Insight/Examples/BinaryThresholdImageFilter.cxx
Note that the OtsuCalculator (as any other of the
ITK calculators) is not a pipeline object. So,
your code updating the reader and manually
invoking "Compute()" on the calculator is still
relevant and necessary before invoking Update()
on the writer.
Note also that you no longer need to provide
the ImageIO object to the ImageFileReader.
The filename extension will be used to find an
appropiate ImageIO at run-time.
Please let us know if you have further questions.
Thanks
Luis
---------------------------
Fucang Jia wrote:
>
> Hello, everyone,
>
> I want to threshold a gray image using Otsu method. But I can not write
> the binaried image. I have not understand the iterator, could anyone
> explain what is wrong? Here is the code:
>
>
> typedef unsigned char PixelType;
> PixelType threshold;
>
> typedef itk::Image< PixelType, 2 > ImageType;
> typedef itk::OtsuThresholdImageCalculator< ImageType > CalculatorType;
> typedef itk::ImageFileReader< ImageType > ReaderType;
> typedef itk::ImageFileWriter< ImageType > WriterType;
> typedef itk::ImageRegionIterator< ImageType > IteratorType;
>
> ReaderType::Pointer reader = ReaderType::New();
> CalculatorType::Pointer calculator = CalculatorType::New();
> WriterType::Pointer writer = WriterType::New();
> itk::MetaImageIO::Pointer metaWriter = itk::MetaImageIO::New();
>
> reader->SetFileName( argv[1] );
> reader->Update();
>
> calculator->SetImage(reader->GetOutput());
> calculator->Compute();
> threshold = calculator->GetThreshold();
>
> ImageType::Pointer image=reader->GetOutput();
> ImageType::RegionType region = image->GetBufferedRegion();
>
> IteratorType it(image,region);
> it.GoToBegin();
>
> while(!it.IsAtEnd())
> {
> if(it.Value()<threshold)
> it.Set(0);
> else
> it.Set(255);
> ++it;
> }
>
> writer->SetImageIO(metaWriter);
> writer->SetFileName(argv[2]);
> writer->SetInput(it);
> writer->Write();
>
> ==========================================
> /data2/itkcvs/Insight/Examples/Filtering/OtsuThreshold.cxx: In
> function `int main(int, char**)':
> /data2/itkcvs/Insight/Examples/Filtering/OtsuThreshold.cxx:57: no
> matching function for call to `itk::ImageFileWriter<main(int,
> char**)::ImageType>::SetInput(main(int, char**)::IteratorType&)'
> /data2/itkcvs/Insight/Code/IO/itkImageFileWriter.txx:51: candidates are:
> void
> itk::ImageFileWriter<TInputImage>::SetInput(const TInputImage*) [with
> TInputImage = main(int, char**)::ImageType]
> make[3]: *** [BinaryThresholdImageFilter.o] Error 1
> make[2]: *** [default_target] Error 2
> make[1]: *** [default_target_Filtering] Error 2
> make: *** [default_target] Error 2
>
> I looked some code, the error occurred on line "writer->SetInput(it);",
> but I do not know how to correct this line.
>
> Thanks !
>
> Fucang
>
>
>
>
>
> _________________________________________________________________
> The new MSN 8: smart spam protection and 2 months FREE*
> http://join.msn.com/?page=features/junkmail
>
> _______________________________________________
> Insight-users mailing list
> Insight-users@public.kitware.com
> http://public.kitware.com/mailman/listinfo/insight-users
>