[Insight-users] cannot read a tiff image
bear
xgl99 at mails.tsinghua.edu.cn
Thu Jan 6 22:20:59 EST 2005
hi Luis,
I do not change my code.Below is the code I read as tiff and write as png
I use *unsigned short* to read, *unsigned short* to *unsighed char* rescale to
[0,255] and then *unsigned char* to save in png.
I am supprised the saved png file is only about 1k bytes are identify gives the
result
w1.png PNG 1280x1024 PseudoClass 256c 1kb 0.050u 0:01
Histogram:
1310720: ( 0, 0, 0) black
it is all *black* indeed, not *seen* as black in naive image viewer.
save as bmp also get all *black* pixels.
The code "readtiff.cxx" is used as below and run with "./readtiff w1.tiff w1.png"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkImage.h"
#include "itkRescaleIntensityImageFilter.h"
int main(int ac, char* av[])
{
if(ac < 3)
{
std::cerr << "Usage: " << av[0] << " input output\n";
return 1;
}
typedef unsigned short InputPixelType;
typedef itk::Image< InputPixelType, 2 > InputImageType;
typedef itk::ImageFileReader< InputImageType > ReaderType;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName( av[1] );
typedef unsigned char WritePixelType;
typedef itk::Image< WritePixelType, 2 > WriteImageType;
typedef itk::RescaleIntensityImageFilter<
InputImageType, WriteImageType > RescaleFilterType;
RescaleFilterType::Pointer rescaler = RescaleFilterType::New();
rescaler->SetOutputMinimum( 0 );
rescaler->SetOutputMaximum( 255 );
typedef itk::ImageFileWriter< WriteImageType > WriterType;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName( av[2] );
rescaler->SetInput( reader->GetOutput() );
writer->SetInput( rescaler->GetOutput() );
try
{
writer->Update();
}
catch( itk::ExceptionObject & err )
{
std::cout << "ExceptionObject caught !" << std::endl;
std::cout << err << std::endl;
return -1;
}
return 0;
}
>From: Luis Ibanez <luis.ibanez at kitware.com>
>Reply-To:
>To: bear <xgl99 at mails.tsinghua.edu.cn>
>Subject: Re: [Insight-users] cannot read a tiff image
>
>
> Hi Bear,
>
> Thanks for testing the try/catch blocks.
>
> You must however have changed your code when you ran the test,
> since according to your email you are writing the image using
> "unsigned char" pixels, which are indeed supported by the
> JPEG writer.
>
> Please double check the image type that you are using for
> instantiating the ImageFileWriter type.
>
>
> Regarding the fact that you "see" black images when you save
> in BMP and PNG, that reinforces the suspicion above, that
> you are actually using "unsigned short" as pixel type.
>
> It is a common mistake to save images using "unsigned short"
> as pixel type but having actual pixels values in the range
> from 0-255. The result is that most naive viewers show this
> images as 'black' images because they are expecting pixel
> values in the range 0-65535 (which is 2^32-1).
>
>
> Yes, I'm sure that your TIFF image was loaded correctly by ITK.
> I used the ImageViewer application in order to view your image.
> This application is available in the InsightApplications
> checkout. The application normalizes the intensities of images
> before displaying them, and therefore it makes possible to look
> at your image despite the fact that the intensity values do not
> fill the dynamic range of 16 bits (unsigned short).
>
>
>
> Regards,
>
>
>
> Luis
>
>
>
>
>
> ------------
> bear wrote:
>
> > hi Luis,
> > As you suggest, I add try/catch code, and show that Abort message is
indeed
> > raised by JpegIO:
> >
> > ExceptionObject caught !
> >
> > itk::ExceptionObject (0x80f3b28)
> > Location: "Unknown"
> > File: /home/src/Insight/Code/IO/itkJPEGImageIO.cxx
> > Line: 404
> > Description: itk::ERROR: JPEGImageIO(0x80f3cc8): JPEG supports unsigned
char/int
> > only
> >
> > This exception does not appear when saving in bmp or png. However, The result
> > is a completely *black* image which is wrong. I see cells in Matlab.
> > Are you sure TIFF file is already loaded accurately?
> >
> > Regards
> > bear
> >
> >>From: Luis Ibanez <luis.ibanez at kitware.com>
> >>Reply-To:
> >>To: bear <xgl99 at mails.tsinghua.edu.cn>
> >>Subject: Re: [Insight-users] cannot read a tiff image
> >>
> >>
> >>Hi Bear,
> >>
> >>
> >>Your file
> >>
> >> http://166.111.201.234/w1.tiff
> >>
> >>is a non standard TIFF file.
> >>
> >>The "identify" tool from ImageMagick reports the following:
> >>
> >>w1.tiff TIFF 1280x1024+0+0 PseudoClass 65536c 16-bit 2.5m 0.050u 0:01
> >>
> >>e:\program files\imagemagick-5.5.5-q16\identify.exe:
> >>
> >>unknown field with tag 33628 (0x835c) encountered. (w1.tiff).
> >>
> >>
> >>
> >>Note however that ITK read it fine.
> >>
> >>You get the warnings,..
> >>but the image is read from the file without problem.
> >>
> >>
> >>The Abort message that you are receiving is probably produced
> >>by the writer->Update(). This commonly happens if you provide
> >>an output filename that doesn't correspond to a fileformat
> >>recognized by ITK.
> >>
> >>What *exact* filename did you used for the output ?
> >>
> >>
> >>For a simple verification, just add try/catch blocks around
> >>the Update() calls of both the reader and the writer.
> >>
> >>Chances are that you will see the reader passing fine, and
> >>the writer aborting.
> >>
> >>For a list of the image fileformats supported in ITK, please
> >>look at the FAQ
> >>
> >> http://www.itk.org/Wiki/ITK_FAQ
> >>
> >>
> >>
> >>
> >>Regards,
> >>
> >>
> >> Luis
> >>
> >>
> >>
> >>----------------
> >>bear wrote:
> >>
> >>
> >>>hi itk-users,
> >>> I have a tiff image(http://166.111.201.234/w1.tiff) which can be read
> >
> > by
> >
> >>>Matlab's imread() function, but it cannot be read by itk with errors:
> >>>
> >>>w1.tiff: Warning, unknown field with tag 317 (0x13d) ignored.
> >>>w1.tiff: Warning, unknown field with tag 33628 (0x835c) ignored.
> >>>Aborted
> >>>What is the problem then? Thanks!
> >>>
> >>>
> >>>The code:
> >>>
> >>>#include "itkImageFileReader.h"
> >>>#include "itkImageFileWriter.h"
> >>>#include "itkImage.h"
> >>>#include "itkRescaleIntensityImageFilter.h"
> >>>int main(int ac, char* av[])
> >>>{
> >>> if(ac < 3)
> >>> {
> >>> std::cerr << "Usage: " << av[0] << " input output\n";
> >>> return 1;
> >>> }
> >>> typedef unsigned short InputPixelType;
> >>> typedef itk::Image< InputPixelType, 2 > InputImageType;
> >>> typedef itk::ImageFileReader< InputImageType > ReaderType;
> >>> ReaderType::Pointer reader = ReaderType::New();
> >>> reader->SetFileName( av[1] );
> >>>
> >>> reader->Update();
> >>>
> >>> typedef unsigned char WritePixelType;
> >>> typedef itk::Image< WritePixelType, 2 > WriteImageType;
> >>> typedef itk::RescaleIntensityImageFilter<
> >>> InputImageType, WriteImageType > RescaleFilterType;
> >>>
> >>> RescaleFilterType::Pointer rescaler = RescaleFilterType::New();
> >>> rescaler->SetOutputMinimum( 0 );
> >>> rescaler->SetOutputMaximum( 255 );
> >>> typedef itk::ImageFileWriter< WriteImageType > WriterType;
> >>>
> >>> WriterType::Pointer writer = WriterType::New();
> >>>
> >>> writer->SetFileName( av[2] );
> >>>
> >>> rescaler->SetInput( reader->GetOutput() );
> >>> writer->SetInput( rescaler->GetOutput() );
> >>>
> >>> writer->Update();
> >>> return 0;
> >>>
> >>>}
> >>>
> >>>
> >>>_______________________________________________
> >>>Insight-users mailing list
> >>>Insight-users at itk.org
> >>>http://www.itk.org/mailman/listinfo/insight-users
> >>>
> >>>
> >>
> >>
> >>
> >>
> >>
> >
> >
> > _______________________________________________
> > Insight-users mailing list
> > Insight-users at itk.org
> > http://www.itk.org/mailman/listinfo/insight-users
> >
> >
>
>
>
>
>
More information about the Insight-users
mailing list