[ITK-users] Inconsistent results for same histogram on same image when PixelType of image is different
donelron
donelron at web.de
Thu Jul 31 14:58:30 EDT 2014
Dear ITK users,
I have the following function that generates two scalar images with a
constant intensity value, then combines them to a vector image and then
calculates a histogram of the vector image:
template<typename TinputImage>
void TestHistogram()
{
//Create an "empty" image in which all pixels will be 13
TinputImage::Pointer pImage13pixels=TinputImage::New();
CreateImage<TinputImage>(pImage13pixels, 50, 50, 13);
//Create an "empty" image in which all pixels will be 42
TinputImage::Pointer pImage42pixels=TinputImage::New();
CreateImage<TinputImage>(pImage42pixels, 50, 50, 42);
typedef itk::JoinImageFilter<TinputImage, TinputImage> JoinImageFilterType;
JoinImageFilterType::Pointer joinFilter = JoinImageFilterType::New();
joinFilter->SetInput1(pImage13pixels);
joinFilter->SetInput2(pImage42pixels);
joinFilter->Update();
//Calculation of histogram
const unsigned int MeasurementVectorSize = 2; // 2 channels, as image is
combination of two images (see above)
const unsigned int binsPerDimension = 200;
typedef itk::VectorImage<TinputImage::PixelType, 2> Vector2DImageType;
//vector image has the same pixel type as the input image
//When using the join filter, determine the OutputImageType like this:
JoinImageFilterType::OutputImageType
typedef itk::Statistics::ImageToHistogramFilter<
JoinImageFilterType::OutputImageType > ImageToHistogramFilterType;
ImageToHistogramFilterType::HistogramType::MeasurementVectorType
lowerBound(binsPerDimension);
lowerBound.Fill(0);
ImageToHistogramFilterType::HistogramType::MeasurementVectorType
upperBound(binsPerDimension);
upperBound.Fill(binsPerDimension-1);
ImageToHistogramFilterType::HistogramType::SizeType
size(MeasurementVectorSize);
size.Fill(binsPerDimension);
ImageToHistogramFilterType::Pointer imageToHistogramFilter =
ImageToHistogramFilterType::New();
imageToHistogramFilter->SetInput( joinFilter->GetOutput() );
imageToHistogramFilter->SetHistogramBinMinimum( lowerBound );
imageToHistogramFilter->SetHistogramBinMaximum( upperBound );
imageToHistogramFilter->SetHistogramSize( size );
try
{
imageToHistogramFilter->Update();
}
catch( itk::ExceptionObject & error )
{
std::cerr << "Error: " << error << std::endl;
system("pause");
}
ImageToHistogramFilterType::HistogramType* histogram =
imageToHistogramFilter->GetOutput();
// Walking through all of the histogram bins and getting the corresponding
frequencies
typedef ImageToHistogramFilterType::HistogramType HistogramType;
HistogramType::ConstIterator itr = histogram->Begin();
HistogramType::ConstIterator end = histogram->End();
typedef HistogramType::AbsoluteFrequencyType AbsoluteFrequencyType;
TinputImage::PixelType indexElement;
HistogramType::IndexType index; //this is an itk::Array of signed long
elements in the current ITK version
while( itr != end )
{
const AbsoluteFrequencyType frequency = itr.GetFrequency();
if (frequency>0)
{
index = histogram->GetIndex(itr.GetInstanceIdentifier());
std::cout << "index= " << index << ", Frequency:" << frequency <<
std::endl;
}
++itr;
}
}
--------------------------------------------------------------
void CreateImage is defined as:
template <typename TImage>
void CreateImage(TImage* const image, int width, int height, int intensity)
{
typename TImage::IndexType corner = {{0,0}};
unsigned int NumRows = height;
unsigned int NumCols = width;
typename TImage::SizeType size = {{NumRows, NumCols}};
typename TImage::RegionType region(corner, size);
image->SetRegions(region);
image->Allocate();
image->FillBuffer(intensity);
}
----------------------------------------------------------
Then I do some typdefs and call the function twice like::
typedef itk::Image<unsigned int, 2> UintImageType;
typedef itk::Image<unsigned char, 2> UcharImageType;
TestHistogram<UcharImageType>();
TestHistogram<UintImageType>();
The resulting output is then:
index= [13, 42], Frequency:2500
index= [199, 199], Frequency:2500
However, if I change the number of histogram bins like this:
const unsigned int binsPerDimension = 300;
then the resulting output is:
index= [13, 42], Frequency:2500
index= [299, 299], Frequency:2500
So, it works only for the unsigned char image, but not for the unsigned int,
which is the one, that I actually need!
It seems that for the unsigned int image that whatever the intensity values
in the image are, they are always rounded up to the upper bound as defined
for the histogram. i also tried to use the itk::ComposeImageFilter instead
of the itk::JoinImageFilter, but that did not help in any way.
What am I doing wrong? And what do I need to change to calculate a histogram
for an image with unsigned int PixelType???
Thanks in advance!
--
View this message in context: http://itk-users.7.n7.nabble.com/Inconsistent-results-for-same-histogram-on-same-image-when-PixelType-of-image-is-different-tp34366.html
Sent from the ITK - Users mailing list archive at Nabble.com.
More information about the Insight-users
mailing list