[Insight-users] Create an image

Julien Lamy lamy at unistra.fr
Wed Mar 2 10:39:02 EST 2011


Le 02/03/2011 16:06, john smith a écrit :
> Hello,
>
> I am trying to create an image 20x20. Every pixel will have the value 0.
> Then, I have created an other function called OutputImage, with which I
> trying to see all the pixel's value on my window command prompt. The
> project is built correctly, but when I run it from the command window
> prompt I do not take any value. Could anybody find the problem?
<snip>
> typedef itk::Image<unsigned char, 2>  ImageType;
<snip>
> void OutputImage(ImageType::Pointer image)
> {
>
>    for(unsigned int r = 0; r < 20; r++)
>        {
>        for(unsigned int c = 0; c < 20; c++)
>          {
>          ImageType::IndexType pixelIndex;
>          pixelIndex[0] = r;
>          pixelIndex[1] = c;
>
>          std::cout <<  image-> GetPixel(pixelIndex) << std::endl;
>          }
>        }
>
>
> }


Since your image type is unsigned char, std::cout will print it as 
characters, not as numbers. It will then print 20*20 the NUL character, 
which amounts to printing nothing. You can solve this either by using a 
different pixel type, or explicitely casting your values to integers (or 
floats) :
std::cout << static_cast<int>(image->GetPixel(pixelIndex)) << std::endl;

-- 
Julien


More information about the Insight-users mailing list