[Insight-users] [insight-users]How to read jpg format and convert to png

Luis Ibanez luis.ibanez at kitware.com
Sun Oct 31 10:42:43 EDT 2010


Hi Jihan,

Please do the following:


A)  Remove the lines


  itk::PNGImageIO::Pointer io;
  io = itk::PNGImageIO::New();

  PNGwriter->SetImageIO(io);

  PNGreader->SetImageIO(io);


They are confusing the ImageIO factory,
since you are telling it to read a JPEG
image, but you are forcing to use a
PNG reader. (and of course, the PNG reader
doesn't know how to read a JPEG image).


The ITK image IO factory will figure
out automatically what file format to
use. You don't need to (and in general
should not) specify what ImageIO to use.





B) Replace:

  PNGreader->Update();

with:

  try
     {
     PNGreader->Update();
     }
   catch ( itk::ExceptionObject & excp )
     {
     std::cerr << excp << std::endl;
     return 1;
     }


This will give you an informative error message,
if something goes wrong.


C)  Replace:

  PNGwriter->Update();

with:

  try
     {
     PNGwriter->Update();
     }
   catch ( itk::ExceptionObject & excp )
     {
     std::cerr << excp << std::endl;
     return 1;
     }


This will give you an informative error message,
if something goes wrong.


----

So, the final code should be:


  itk::ImageFileReader<RGBImageType>::Pointer PNGreader;
  PNGreader = itk::ImageFileReader<RGBImageType>::New();

  PNGreader->SetFileName( "demoMeanFilter.jpg" );

   itk::ImageFileWriter<OutputImageType>::Pointer PNGwriter;
  PNGwriter = itk::ImageFileWriter<OutputImageType>::New();

  PNGwriter->SetInput( PNGreader->GetOutput() );
  PNGwriter->SetFileName("demoMeanFilter.png" );

  try
     {
     PNGwriter->Update();
     }
   catch ( itk::ExceptionObject & excp )
     {
     std::cerr << excp << std::endl;
     return 1;
     }



Note that we have removed the PNGreader->Update(),
since that call will be triggered anyways by the call to
Update() in the writer.



     Regards,


            Luis


-----------------------------------------------------------------------------------------
On Fri, Oct 29, 2010 at 3:50 AM, 汪济航 <wangjihang88 at gmail.com> wrote:

> Hi,
>
> I meet a run-time problem when I try to read the jpg format file and save
> it as png. I just simulate the coding in itkPNGImageIOTest.cxx.
> The problem may happen in the coding below
>
> // Read in the image
>   itk::PNGImageIO::Pointer io;
>   io = itk::PNGImageIO::New();
>   itk::ImageFileReader<RGBImageType>::Pointer PNGreader;
>   PNGreader = itk::ImageFileReader<RGBImageType>::New();
>   PNGreader->SetFileName( "demoMeanFilter.jpg" );
>   PNGreader->SetImageIO(io);
>   PNGreader->Update();
>   itk::ImageFileWriter<OutputImageType>::Pointer PNGwriter;
>   PNGwriter = itk::ImageFileWriter<OutputImageType>::New();
>   PNGwriter->SetInput(PNGreader->GetOutput());
>   PNGwriter->SetFileName("demoMeanFilter.png" );
>   PNGwriter->SetImageIO(io);
>   PNGwriter->Write();
>
> Thanks,
>
> Jihang
>
> _____________________________________
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20101031/b15858fa/attachment.htm>


More information about the Insight-users mailing list