[Insight-users] binary image generating

Ming Chao mingchao2005 at gmail.com
Mon Feb 9 11:57:35 EST 2009


I tried to generate a binary image based on a random number. I observed
something I haven't understood. What I did was quite simple. I generated a
uniform random number between 0 and 1. If the random number is smaller than
0.5, then the image intensity value was set to zero, otherwise 1000. I
tested the idea by generating a 2D image. What I found was that when the
image sizes (both x and y) were set to 2^N (N= 1, 2, 3, 4....), the
generated image was fine. With other image sizes, there were two interesting
observations: (1) the generated image was not a binary image, ie, the image
intensities were not 0 and 1000 but there were some intermediate values; (2)
the sizes of the generated image were not the sizes I specified. For
instance, if I specified a 3X3 region, but the generated image would have
4X4 sizes. I don't know what I did wrong here. Any help or suggestions would
be appreciated!

Here is the code I used to generate the image.

#include "itkImage.h"

#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"

#include <fstream>
//#include <cstdlib>
//#include <ctime>

#include "rng.h"

//using namespace std;

//---------------------------------------------------------------------------------------
int main(   )
{

const   unsigned int   Dimension = 2;

typedef unsigned short  PixelType;
typedef itk::Image< PixelType, Dimension >  ImageType;

ImageType::Pointer image = ImageType::New();

ImageType::IndexType start;
start[0] = 0; start[1] = 0; //start[2] = 0;

ImageType::SizeType size;
size[0] = 33; size[1] = 33; //size[2] = 1;

ImageType::RegionType region;
region.SetSize( size ); region.SetIndex( start );

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

/*
ImageType::SpacingType spacing;
spacing[0] = 1.0; spacing[1] = 1.0; spacing[2] = 0.0;
image->SetSpacing( spacing );

ImageType::PointType origin;
origin[0] = 0.0; origin[1] = 0.0; origin[2] = 0.0;
image->SetOrigin( origin );
*/

RNG rdm;
int counter=0;
for ( int i = 0; i < size[0]; i++) {
for ( int j = 0; j < size[1]; j++ ) {
//for ( int k = 0; k < size[2]; k++ ) {
 ImageType::IndexType pixelIndex;
pixelIndex[0] = i;
pixelIndex[1] = j;
//pixelIndex[2] = k;

double prob = rdm.uniform();
//std::cout << "probability = " << prob << std::endl;
 if (prob <= 0.5) {
ImageType::PixelType pixelValue = itk::NumericTraits < PixelType >::Zero;
image->SetPixel( pixelIndex, pixelValue );
counter++;
} else {
ImageType::PixelType pixelValue = 1000;
image->SetPixel( pixelIndex, pixelValue );
}
 //}
}
}

 for ( int i = 0; i < size[0]; i++) {
for ( int j = 0; j < size[1]; j++ ) {
//for ( int k = 0;  k < size[2]; k++ ) {
 ImageType::IndexType pixelIndex;
pixelIndex[0] = i;
pixelIndex[1] = j;
//pixelIndex[2] = k;

ImageType::PixelType outPixel = image->GetPixel(pixelIndex);
//std::cout << "pixel value = " << outPixel << std::endl;

//}
}
}

std::cout << counter << " pixels of " << size[0]*size[1] << " have zero
values." << std::endl;


typedef itk::ImageFileWriter<  ImageType >  WriterType;
WriterType::Pointer imgWriter = WriterType::New();
imgWriter->SetInput( image );
imgWriter->SetFileName( "test.vtk" );
imgWriter->Update();





  return EXIT_SUCCESS;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20090209/cb87414f/attachment-0001.htm>


More information about the Insight-users mailing list