[Insight-users] get the maximum pixel value using GetPixel() for 3D image

Brecht Heyde Brecht.Heyde at med.kuleuven.be
Tue Jun 7 11:21:14 EDT 2011


Hi John,

 

The following code should do (for 2D images)

 

int main(void)

       {

             //Find maximum of 2D image

             typedef unsigned char PixelType;

             const unsigned int dimension = 2;

             typedef itk::Image<PixelType,dimension> ImageType;

             typedef itk::ImageFileReader<ImageType> ReaderType;

             ReaderType::Pointer reader = ReaderType::New();

             std::string filename =
"C://Code//ITK320//src//InsightToolkit-3.20.0//Examples//Data//BrainProtonDe
nsitySliceShifted13x17y.mhd";

             reader->SetFileName(filename);

             try

             {

                    reader->Update();

             }

             catch(itk::ExceptionObject &err)

             {

                    std::cerr << "Error: " << err << std::endl;

                    return EXIT_FAILURE;

             }

             

             ImageType::Pointer image = reader->GetOutput();

 

             ImageType::SizeType size =
reader->GetOutput()->GetLargestPossibleRegion().GetSize();

             ImageType::IndexType index =
reader->GetOutput()->GetLargestPossibleRegion().GetIndex();

 

             ImageType::PixelType maxValue;

             for(int i = index[0]; i<size[0]; i++)

             {

                    for(int j = index[1]; j<size[1]; j++)

                    {

                           ImageType::IndexType currentIndex;

                           currentIndex[0] = i;

                           currentIndex[1] = j;

                           ImageType::PixelType currentValue =
image->GetPixel(currentIndex);

                           if(currentValue>maxValue)

                           {

                                  maxValue = currentValue;

                           }

                    }

             }

             std::cout << "maxValue: " << (int)maxValue << std::endl;

             return EXIT_SUCCESS;

       }

 

I would however strongly suggest using the imageIterators as Roberto already
pointed out. His code snippet should do the exact same thing, only more
efficient. Have a look in the software guide chapter 11 for examples in
using image iterators.

 

Regards,

Brecht

 

 

From: john smith [mailto:mkitkinsightuser at gmail.com] 
Sent: dinsdag 7 juni 2011 16:52
To: Brecht Heyde; insight-users at itk.org
Subject: Re: [Insight-users] get the maximum pixel value using GetPixel()
for 3D image

 

You have right.Using Iteretors was really easy, but I am not yet used to
them.Could somebody give me a small code of finding the max value without
using iterators, in order to compare the two methods?

Thanks a lot for all your replies.They are very useful.

2011/6/7 Brecht Heyde <Brecht.Heyde at med.kuleuven.be>

You are probably trying to access a pixel location in image->GetPixel()
which doesn't exist. 

 

Try removing the lines, which I think are not needed:

  image->SetRegions( region );
  image->Allocate();

 

You should also modify the following as your image's origin might not be [0
0 0]: 

index = inputRegion.GetIndex();

for ( i=index[0];i<size_x;i++) {
      for ( j=index[1];i<size_y;j++){
          for ( k=index[2];i<size_z;k++){

          /*.*/

         }

     }

}

 

start_x, start_y and start_z are not needed anymore.

 

Try debugging to see at what line of code you are getting the violation
error or printing the i,j,k to see where it goes wrong. Any C++ basic book
can tell you how to do that.

 

Regards,

Brecht

 

From: john smith [mailto:mkitkinsightuser at gmail.com] 
Sent: dinsdag 7 juni 2011 16:24
To: Brecht Heyde; insight-users at itk.org


Subject: Re: [Insight-users] get the maximum pixel value using GetPixel()
for 3D image

 

OK, this is the exception that I get

Unhandled exception at 0x00286a67 in mainwindow.exe: 0xC0000005: Access
violation reading location 0x04960548.

2011/6/7 john smith <mkitkinsightuser at gmail.com>

Well,

When I debug the code, then I am taking an exception:
Unhandled exception at 0x00856a67 in mainwindow.exe: 0xC0000005: Access
violation reading location 0x050f0548.

Do yoy know why?Should I change something in my code?Is there wrong in the
logic of the code?
Thanks for the replies



ReaderType::Pointer reader = ReaderType::New();

     reader->SetFileName( fileName.toStdString()  );
     reader->Update();

      InputImageType::RegionType inputRegion =
           reader->GetOutput()->GetLargestPossibleRegion();

  
    //typedef itk::Image< short, 3 > InputImageType;
  
     InputImageType::Pointer image = reader->GetOutput();

  

  InputImageType::IndexType start;
  InputImageType::SizeType  size;

 size = inputRegion.GetSize();
  // get the size of the hole 3D image
  size_x = size[0];
  size_y = size[1];
  size_z = size[2];
  

  start[0] =   0;  // first index on X
  start[1] =   0;  // first index on Y
  start[2] =   0;  // first index on Z

  InputImageType::RegionType region;
  region.SetSize( size );
  region.SetIndex( start );
  
  // Pixel data is allocated
  image->SetRegions( region );
  image->Allocate();

   int i,j,k;
 int max_value=0;



  for ( i=0;i<size_x;i++) {
      for ( j=0;i<size_y;j++){
          for ( k=0;i<size_z;k++){

              InputImageType::IndexType pixelIndex;
                pixelIndex[0] = i; // x position
                pixelIndex[1] = j; // y position
                pixelIndex[2] = k; // z position

                InputImageType::PixelType pixelValue = image->GetPixel(
pixelIndex );

                if(pixelValue>max_value){max_value=pixelValue;}
                
          }
      }
  }

   ui->label_22->setText(QString("maximum_value_raw%1").arg(max_value));

2011/6/7 john smith <mkitkinsightuser at gmail.com>

I have added this



  size = inputRegion.GetSize();
  // get the size of the hole 3D image
  size_x = size[0];
  size_y = size[1];
  size_z = size[2];

I am getting into the loop, but now I am getting an exception.Do you know
what I am doing wrong? I have declared the size_x,size_y,size_z in an other
function as it seems bellow, and declare them as global variable.Is it this
declaration responcible for the exception?

Thanks


  

2011/6/7 Brecht Heyde <Brecht.Heyde at med.kuleuven.be>

Sorry, it should have been:

size = inputRegion.GetSize();

 

Regards,

Brecht Heyde

 

From: insight-users-bounces at itk.org [mailto:insight-users-bounces at itk.org]
On Behalf Of Brecht Heyde
Sent: dinsdag 7 juni 2011 15:34
To: 'john smith'; 'robert tamburo'; insight-users at itk.org


Subject: Re: [Insight-users] get the maximum pixel value using GetPixel()
for 3D image

 

Hi,

 

Your size_x, size_y and size_z are not representing the actual  size of the
image.

 

Add:

size = inputRegion->GetSize();

Before 

size_x = .

size_y = .

size_z = .

 

I would however also suggest to use an imageIterator.

 

Regards,

Brecht Heyde

 

From: insight-users-bounces at itk.org [mailto:insight-users-bounces at itk.org]
On Behalf Of john smith
Sent: dinsdag 7 juni 2011 15:27
To: robert tamburo; insight-users at itk.org
Subject: Re: [Insight-users] get the maximum pixel value using GetPixel()
for 3D image

 

Thanks for your reply.I have used the MinimumMaximumImageCalculator, but
know I want to take the result playing with the raw data.But I am trying to
do that without using iterators, just with using for loop. I have added the
largest possible area from reader, but when I am debuging my code, it seems
that I cannot get inside the loop. Do you know why? Here is my code:



 ReaderType::Pointer reader = ReaderType::New();

     reader->SetFileName( fileName.toStdString()  );
     reader->Update();

      InputImageType::RegionType inputRegion =
           reader->GetOutput()->GetLargestPossibleRegion();

  
    //typedef itk::Image< short, 3 > InputImageType;
  
     InputImageType::Pointer image = reader->GetOutput();

  

  InputImageType::IndexType start;
  InputImageType::SizeType  size;

  // get the size of the hole 3D image
  size_x = size[0];
  size_y = size[1];
  size_z = size[2];

  start[0] =   0;  // first index on X
  start[1] =   0;  // first index on Y
  start[2] =   0;  // first index on Z

  InputImageType::RegionType region;
  region.SetSize( size );
  region.SetIndex( start );
  
  // Pixel data is allocated
  image->SetRegions( region );
  image->Allocate();

   int i,j,k;
 int max_value=0;

  for ( i=0;i<size_x;i++) {
      for ( j=0;i<size_y;j++){
          for ( k=0;i<size_z;k++){

              InputImageType::IndexType pixelIndex;
                pixelIndex[0] = i; // x position
                pixelIndex[1] = j; // y position
                pixelIndex[2] = k; // z position

                InputImageType::PixelType pixelValue = image->GetPixel(
pixelIndex );

                if(pixelValue>max_value){max_value=pixelValue;}
                
          }
      }
  }

   ui->label_22->setText(QString("maximum_value_raw%1").arg(max_value));

2011/6/7 robert tamburo <robert.tamburo at gmail.com>

GetPixel() belongs to itkImage, not the reader. Try
reader->GetOutput()->GetPixel(index).  

 

Two other things: 1) You may want to consider using an ImageRegionIterator
instead of for loops, and 2) You can use the MinimumMaximumImageCalculator
to get the maximum intensity in an image.

 

There are examples on how to use each class at
http://www.itk.org/Wiki/ITK/Examples

 

 

On Tue, Jun 7, 2011 at 8:36 AM, john smith <mkitkinsightuser at gmail.com>
wrote:

Hello,

I am trying to find the maximum pixel value of a 3D  image, and I am using
the GetPixel method with 3 loops.I want to load my image from a file so I
created a reader pointer.Does this pointer include the raw data of my image?
I have created the following code but I get an error: GetPixel() method is
not a member of reader. Could somebody tell me what I am doing wrong? How I
could find the maximum pixel value of a loaded image with the method of
GetPixel()?

Thanks

  typedef short               InputPixelType;
  const   unsigned int        Dimension = 3;

  typedef itk::Image< InputPixelType,  Dimension >    InputImageType;
  typedef itk::ImageFileReader< InputImageType  >  ReaderType;


  ReaderType::Pointer reader = ReaderType::New();

     reader->SetFileName( fileName.toStdString()  );
     reader->Update();

       InputImageType::RegionType inputRegion =
           reader->GetOutput()->GetLargestPossibleRegion();

  InputImageType::SizeType size = inputRegion.GetSize();
 
  // get the size of the hole 3D image
  size_x = size[0];
  size_y = size[1];
  size_z = size[2];

   InputImageType::IndexType start = inputRegion.GetIndex();
  start[0] =   0;  // first index on X
  start[1] =   0;  // first index on Y
  start[2] =   0;  // first index on Z

 int i,j,k;
 int max_value=0;
  for ( i=0;i<size_x;i++) {
      for ( j=0;i<size_y;j++){
          for ( k=0;i<size_z;k++){

              InputImageType::IndexType pixelIndex;
                pixelIndex[0] = i; // x position
                pixelIndex[1] = j; // y position
                pixelIndex[2] = k; // z position

                InputImageType::PixelType pixelValue = reader->GetPixel(
pixelIndex );

                if(pixelValue>max_value){max_value=pixelValue;}
                
          }
      }
  }

_____________________________________


Powered by www.kitware.com

Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html

Kitware offers ITK Training Courses, for more information visit:
http://www.kitware.com/products/protraining.html

Please keep messages on-topic and check the ITK FAQ at:
http://www.itk.org/Wiki/ITK_FAQ

Follow this link to subscribe/unsubscribe:
http://www.itk.org/mailman/listinfo/insight-users

 

 

 

 

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20110607/d495ab6a/attachment-0001.htm>


More information about the Insight-users mailing list