[Insight-users] binary image generating

Luis Ibanez luis.ibanez at kitware.com
Tue Feb 10 17:48:50 EST 2009


Hi Ming,

Thanks for your clear description of the problem and for posting the source
code.

I just reformatted your code a bit, and ...
it works for me.... :-)

Well,
I also replaced your RNG object with a call to the GNU/Linux function
"random()".

It produces an image of the correct size (33x33) and the pixel values are
indeed
0 and 1000, and distributed close to 50% versus 50%.

I'm wondering if the problem may be with the tool that you are using for
looking
at the output test.vtk file. It may be that the tool is not interpreting
correctly the
fact that you have 16-bits values.

Independent of the problem that you are reporting, please not that your
method
for accessing pixel values is quite inefficient. I have converted your code
to use
ITK iterators (which is the fastest way of visiting pixel data in ITK).

Please find attached to this email the following files:


   - Random.cxx: your source code reformatted
   - Random2.cxx: converted to use ITK iterators
   - CMakeLists.txt file for building both


Please give it a try to building these two files and let us know if you
still see any problem.

(in which case, please let us know what tool you are using for looking
at the output image).


     Thanks


           Luis


------------------------------------------

On Mon, Feb 9, 2009 at 11:57 AM, Ming Chao <mingchao2005 at gmail.com> wrote:

> 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;
> }
>
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.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/20090210/64e6d230/attachment-0001.htm>
-------------- next part --------------
CMAKE_MINIMUM_REQUIRED(VERSION 2.4)
IF(COMMAND CMAKE_POLICY)
  CMAKE_POLICY(SET CMP0003 NEW)
ENDIF(COMMAND CMAKE_POLICY)

PROJECT(Random)

FIND_PACKAGE(ITK REQUIRED)
INCLUDE(${ITK_USE_FILE})

ADD_EXECUTABLE(Random Random.cxx )
TARGET_LINK_LIBRARIES(Random ITKIO ITKCommon)

ADD_EXECUTABLE(Random2 Random2.cxx )
TARGET_LINK_LIBRARIES(Random2 ITKIO ITKCommon)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Random.cxx
Type: text/x-c++src
Size: 1855 bytes
Desc: not available
URL: <http://www.itk.org/pipermail/insight-users/attachments/20090210/64e6d230/attachment-0002.cxx>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Random2.cxx
Type: text/x-c++src
Size: 1522 bytes
Desc: not available
URL: <http://www.itk.org/pipermail/insight-users/attachments/20090210/64e6d230/attachment-0003.cxx>


More information about the Insight-users mailing list