[Insight-users] strange problem about ITK

宋涛 prog_st at 163 . com
Sun, 7 Sep 2003 14:04:24 +0800 (CST)


--Boundary-=_iHBWpVMuGhxqDPnjuTlmweyCQomO
Content-Type: text/plain
Content-Transfer-Encoding: 8bit

Hi Luis,
 
source code 
=====================================================================================
const unsigned int IMAGE_DIMENSION = 2 ;
const int   IMAGE_WIDTH  = 400;
const int   IMAGE_HEIGHT = 240;
const unsigned int NUMBEROFPIXELS = IMAGE_WIDTH * IMAGE_HEIGHT ;
 
typedef unsigned char                                                  GrayPixelType   ;
typedef itk::Image< GrayPixelType, IMAGE_DIMENSION >                  GrayImageType   ;
typedef itk::ImportImageFilter< GrayPixelType, IMAGE_DIMENSION >       GrayImportFilterType ;
typedef itk::ImageFileReader< GrayImageType >            GrayReaderType   ;
typedef itk::ImageFileWriter< GrayImageType >            GrayWriterType   ;
 
typedef itk::RGBPixel< unsigned char >              RGBPixelType   ;
typedef itk::Image< RGBPixelType, IMAGE_DIMENSION >           RGBImageType   ;
typedef itk::ImportImageFilter< RGBPixelType, IMAGE_DIMENSION >        RGBImportFilterType  ;
typedef itk::ImageFileReader< RGBImageType >            RGBReaderType   ;
typedef itk::ImageFileWriter< RGBImageType >            RGBWriterType   ;


 RGBReaderType::Pointer reader = RGBReaderType::New();
  reader->SetFileName( "E:/InputRGB.png");
 try
 {
  reader->Update();
 }
 catch( itk::ExceptionObject & exp ) 
 {
  std::cerr << "Exception caught !" << std::endl;
  std::cerr << exp << std::endl;
 }

 RGBImageType::Pointer image = reader->GetOutput();
 RGBImageType::IndexType pixelIndex;
 RGBImageType::PixelType pixelValue;
 
 int FileSize   = NUMBEROFPIXELS * 3;
 unsigned char *Value = new unsigned char[FileSize];
 if( Value == NULL)
 {
  printf( "Memmory is limit!\n" );
 }
 
 for(unsigned int y = 0; y < IMAGE_HEIGHT; y++)
 {
  for(unsigned int x = 0; x < IMAGE_WIDTH; x++)
  {
   pixelIndex[0] = x;   // x position
   pixelIndex[1] = y;   // y position
 
   pixelValue = image->GetPixel( pixelIndex );
 
   // B
   Value[ ((IMAGE_HEIGHT - 1 -y) * IMAGE_WIDTH + x)*3 + 0] = pixelValue.GetBlue();
 
   // G
   Value[ ((IMAGE_HEIGHT - 1 -y) * IMAGE_WIDTH + x)*3 + 1] = pixelValue.GetGreen();
 
   // R
   Value[ ((IMAGE_HEIGHT - 1 -y) * IMAGE_WIDTH + x)*3 + 2] = pixelValue.GetRed();
  }
 }
 
 FILE *stream   = NULL;
 int NumRead    = 0;
 
 if( (stream = fopen( "E:/OutputRGB.200", "w+" )) != NULL )
 {
  NumRead = fwrite( Value, sizeof( unsigned char ), FileSize, stream );
  printf( "Number of items write = %d\n", NumRead );
 
  fclose( stream );
 }
 else
 {
  printf( "File could not be opened\n" );
  return 1;
 }
 
 delete[] Value;
 
========================================================================================================
 
this is a very strange problem! 
 
i think the size of file("OutputRGB.200") is FileSize,
the function (printf( "Number of items write = %d\n", NumRead );) also output FileSize,
 
but ACTUALLY the size of file("OutputRGB.200") is greater than FileSize!!!!!!!!!!!!!!!!!
 
WHY?
--Boundary-=_iHBWpVMuGhxqDPnjuTlmweyCQomO
Content-Type: text/html
Content-Transfer-Encoding: 8bit

Hi&nbsp;Luis,<br>&nbsp;<br>source&nbsp;code&nbsp;<br>=================================================================================<br>====<br>const&nbsp;unsigned&nbsp;int&nbsp;IMAGE_DIMENSION&nbsp;=&nbsp;2&nbsp;;<br>const&nbsp;int&nbsp;&nbsp;&nbsp;IMAGE_WIDTH&nbsp;&nbsp;=&nbsp;400;<br>const&nbsp;int&nbsp;&nbsp;&nbsp;IMAGE_HEIGHT&nbsp;=&nbsp;240;<br>const&nbsp;unsigned&nbsp;int&nbsp;NUMBEROFPIXELS&nbsp;=&nbsp;IMAGE_WIDTH&nbsp;*&nbsp;IMAGE_HEIGHT&nbsp;;<br>&nbsp;<br>typedef&nbsp;unsigned&nbsp;char&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GrayPixelT<br>ype&nbsp;&nbsp;&nbsp;;<br>typedef&nbsp;itk::Image&lt;&nbsp;GrayPixelType,&nbsp;IMAGE_DIMENSION&nbsp;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GrayImageTy<br>pe&nbsp;&nbsp;&nbsp;;<br>typedef&nbsp;itk::ImportImageFilter&lt;&nbsp;GrayPixelType,&nbsp;IMAGE_DIMENSION&nbsp;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GrayImport<br>FilterType&nbsp;;<br>typedef&nbsp;itk::ImageFileReader&lt;&nbsp;GrayImageType&nbsp;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GrayReaderType&nbsp;&nbsp;&nbsp;;<br>typedef&nbsp;itk::ImageFileWriter&lt;&nbsp;GrayImageType&nbsp;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GrayWriterType&nbsp;&nbsp;&nbsp;;<br>&nbsp;<br>typedef&nbsp;itk::RGBPixel&lt;&nbsp;unsigned&nbsp;char&nbsp;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RGBPixelType&nbsp;&nbsp;&nbsp;;<br>typedef&nbsp;itk::Image&lt;&nbsp;RGBPixelType,&nbsp;IMAGE_DIMENSION&nbsp;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RGBImageType&nbsp;&nbsp;&nbsp;;<br>typedef&nbsp;itk::ImportImageFilter&lt;&nbsp;RGBPixelType,&nbsp;IMAGE_DIMENSION&nbsp;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RGBImportF<br>ilterType&nbsp;&nbsp;;<br>typedef&nbsp;itk::ImageFileReader&lt;&nbsp;RGBImageType&nbsp;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RGBReaderType&nbsp;&nbsp;&nbsp;;<br>typedef&nbsp;itk::ImageFileWriter&lt;&nbsp;RGBImageType&nbsp;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RGBWriterType&nbsp;&nbsp;&nbsp;;<br><br><br>&nbsp;RGBReaderType::Pointer&nbsp;reader&nbsp;=&nbsp;RGBReaderType::New();<br>&nbsp;&nbsp;reader-&gt;SetFileName(&nbsp;&quot;E:/InputRGB.png&quot;);<br>&nbsp;try<br>&nbsp;{<br>&nbsp;&nbsp;reader-&gt;Update();<br>&nbsp;}<br>&nbsp;catch(&nbsp;itk::ExceptionObject&nbsp;&amp;&nbsp;exp&nbsp;)&nbsp;<br>&nbsp;{<br>&nbsp;&nbsp;std::cerr&nbsp;&lt;&lt;&nbsp;&quot;Exception&nbsp;caught&nbsp;!&quot;&nbsp;&lt;&lt;&nbsp;std::endl;<br>&nbsp;&nbsp;std::cerr&nbsp;&lt;&lt;&nbsp;exp&nbsp;&lt;&lt;&nbsp;std::endl;<br>&nbsp;}<br><br>&nbsp;RGBImageType::Pointer&nbsp;image&nbsp;=&nbsp;reader-&gt;GetOutput();<br>&nbsp;RGBImageType::IndexType&nbsp;pixelIndex;<br>&nbsp;RGBImageType::PixelType&nbsp;pixelValue;<br>&nbsp;<br>&nbsp;int&nbsp;FileSize&nbsp;&nbsp;&nbsp;=&nbsp;NUMBEROFPIXELS&nbsp;*&nbsp;3;<br>&nbsp;unsigned&nbsp;char&nbsp;*Value&nbsp;=&nbsp;new&nbsp;unsigned&nbsp;char[FileSize];<br>&nbsp;if(&nbsp;Value&nbsp;==&nbsp;NULL)<br>&nbsp;{<br>&nbsp;&nbsp;printf(&nbsp;&quot;Memmory&nbsp;is&nbsp;limit!\n&quot;&nbsp;);<br>&nbsp;}<br>&nbsp;<br>&nbsp;for(unsigned&nbsp;int&nbsp;y&nbsp;=&nbsp;0;&nbsp;y&nbsp;&lt;&nbsp;IMAGE_HEIGHT;&nbsp;y++)<br>&nbsp;{<br>&nbsp;&nbsp;for(unsigned&nbsp;int&nbsp;x&nbsp;=&nbsp;0;&nbsp;x&nbsp;&lt;&nbsp;IMAGE_WIDTH;&nbsp;x++)<br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;pixelIndex[0]&nbsp;=&nbsp;x;&nbsp;&nbsp;&nbsp;//&nbsp;x&nbsp;position<br>&nbsp;&nbsp;&nbsp;pixelIndex[1]&nbsp;=&nbsp;y;&nbsp;&nbsp;&nbsp;//&nbsp;y&nbsp;position<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;pixelValue&nbsp;=&nbsp;image-&gt;GetPixel(&nbsp;pixelIndex&nbsp;);<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;//&nbsp;B<br>&nbsp;&nbsp;&nbsp;Value[&nbsp;((IMAGE_HEIGHT&nbsp;-&nbsp;1&nbsp;-y)&nbsp;*&nbsp;IMAGE_WIDTH&nbsp;+&nbsp;x)*3&nbsp;+&nbsp;0]&nbsp;=&nbsp;pixelValue.GetBlue()<br>;<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;//&nbsp;G<br>&nbsp;&nbsp;&nbsp;Value[&nbsp;((IMAGE_HEIGHT&nbsp;-&nbsp;1&nbsp;-y)&nbsp;*&nbsp;IMAGE_WIDTH&nbsp;+&nbsp;x)*3&nbsp;+&nbsp;1]&nbsp;=&nbsp;pixelValue.GetGreen(<br>);<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;//&nbsp;R<br>&nbsp;&nbsp;&nbsp;Value[&nbsp;((IMAGE_HEIGHT&nbsp;-&nbsp;1&nbsp;-y)&nbsp;*&nbsp;IMAGE_WIDTH&nbsp;+&nbsp;x)*3&nbsp;+&nbsp;2]&nbsp;=&nbsp;pixelValue.GetRed();<br><br>&nbsp;&nbsp;}<br>&nbsp;}<br>&nbsp;<br>&nbsp;FILE&nbsp;*stream&nbsp;&nbsp;&nbsp;=&nbsp;NULL;<br>&nbsp;int&nbsp;NumRead&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;0;<br>&nbsp;<br>&nbsp;if(&nbsp;(stream&nbsp;=&nbsp;fopen(&nbsp;&quot;E:/OutputRGB.200&quot;,&nbsp;&quot;w+&quot;&nbsp;))&nbsp;!=&nbsp;NULL&nbsp;)<br>&nbsp;{<br>&nbsp;&nbsp;NumRead&nbsp;=&nbsp;fwrite(&nbsp;Value,&nbsp;sizeof(&nbsp;unsigned&nbsp;char&nbsp;),&nbsp;FileSize,&nbsp;stream&nbsp;);<br>&nbsp;&nbsp;printf(&nbsp;&quot;Number&nbsp;of&nbsp;items&nbsp;write&nbsp;=&nbsp;%d\n&quot;,&nbsp;NumRead&nbsp;);<br>&nbsp;<br>&nbsp;&nbsp;fclose(&nbsp;stream&nbsp;);<br>&nbsp;}<br>&nbsp;else<br>&nbsp;{<br>&nbsp;&nbsp;printf(&nbsp;&quot;File&nbsp;could&nbsp;not&nbsp;be&nbsp;opened\n&quot;&nbsp;);<br>&nbsp;&nbsp;return&nbsp;1;<br>&nbsp;}<br>&nbsp;<br>&nbsp;delete[]&nbsp;Value;<br>&nbsp;<br>=================================================================================<br>=======================<br>&nbsp;<br>this&nbsp;is&nbsp;a&nbsp;very&nbsp;strange&nbsp;problem!&nbsp;<br>&nbsp;<br>i&nbsp;think&nbsp;the&nbsp;size&nbsp;of&nbsp;file(&quot;OutputRGB.200&quot;)&nbsp;is&nbsp;FileSize,<br>the&nbsp;function&nbsp;(printf(&nbsp;&quot;Number&nbsp;of&nbsp;items&nbsp;write&nbsp;=&nbsp;%d\n&quot;,&nbsp;NumRead&nbsp;);)&nbsp;also&nbsp;output&nbsp;Fil<br>eSize,<br>&nbsp;<br>but&nbsp;ACTUALLY&nbsp;the&nbsp;size&nbsp;of&nbsp;file(&quot;OutputRGB.200&quot;)&nbsp;is&nbsp;greater&nbsp;than&nbsp;FileSize!!!!!!!!!!<br>!!!!!!!<br>&nbsp;<br>WHY?<br><font style='font-size:12pt'>
<br>
==============================================<br>
<a target=_blank href='http://vip . 163 . com' title='http://vip . 163 . com'><font color=green><b>安全稳定大容量,收费伊妹儿免费30日完美体验~</b></a><br>
<a target=_blank href='http://mail . 163 . com' title='http://mail . 163 . com'><font color=red><b>中国最大的免费邮箱在等你 25兆空间 4兆附件!</b></a><br>
<a target=_blank href='http://popo . 163 . com' title='http://popo . 163 . com'><font color=green><b>点击网易泡泡惊喜无限 全免费手机短信任你发!</b></a><br><br>

--Boundary-=_iHBWpVMuGhxqDPnjuTlmweyCQomO--