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

Vincent Garcia vincent.garcia at inria.fr
Tue Jun 7 09:21:16 EDT 2011


I think you should : 

- Get the largest region of the image using GetLargestPossibleRegion () 
- Then you define an itk::ImageRegionIterator on your region 
- With only one loop, you access to all image pixel 

The code should be soemthing like that (if you are in a template function) : 


// Type definition
typedef itk::ImageRegionIterator<ImageType> ImageIteratorType; 
typedef ImageType::RegionType               RegionType;
typedef ImageType::PixelType                PixelType;

// Get image region
RegionType region = image->GetLargestPossibleRegion();

// Create iterator 
ImageIteratorType it( image, region ); 
it.GoToBegin(); 

// Initialization of max_value
int max_value=0;

// Loop 
while(!it.IsAtEnd()) 
{ 
  // Pixel value
  PixelType pixel = it.Get();

  // Is it bigger than the max?
  if (pixel>max_value)
    max_value = pixel;

  // Increment iterator 
  ++it; 
} 



I haven't compile the code neither tested it.
It's just a simple example.
This code assumes that PixelType is a scalar value (e.g. double, int, short, float, etc.), not a vector.

Vincent Garcia



----- Original Message ----- 


From: "john smith" <mkitkinsightuser at gmail.com> 
To: insight-users at itk.org 
Sent: Tuesday, 7 June, 2011 2:36:04 PM 
Subject: [Insight-users] get the maximum pixel value using GetPixel() for 3D image 

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 



More information about the Insight-users mailing list