[Insight-users] change every voxel value of a 3d image

Sergio Vera sergio.vera at alma3d.com
Mon May 17 11:49:36 EDT 2010


Hello Marco,

As i don't know what type of output image your code is generating right now
(you may want to upload an simple 2d-example on a public site so other users
can see it and try to figure what's happening) I will guess that maybe the
problem is that you are working with unsigned char images (0-255 possible
values) but you are adding 100 to some pixels...

Maybe some values are causing overflow of the maximum unsigned char value
(say for instance, your input image has intensity = 158 and you are adding
100 for a total of 258, a value that cannot be stored in a unsigned char...
I don't know how the compiler or ITK will handle this, but I suppose it may
be a source of problems.

You can try to use unsigned short or unsigned int as PixelType and see what
happens

Regards

On Mon, May 17, 2010 at 4:29 PM, Marco Gheza <
marcogheza4mailinglists at gmail.com> wrote:

> Hi Sergio,
> i tried your solution but the result is the same. it is more fast but i
> obtain the same result.
>
>   // Verify the number of parameters on the command line.
>   if ( argc < 7 )
>     {
>       std::cerr << "Missing parameters. " << std::endl;
>       std::cerr << "Usage: " << std::endl;
>       std::cerr << argv[0]
>                 << " inputImageFile outputImageFile startX startY startZ
> sizeX sizeY sizeZ"
>                 << std::endl;
>       return -1;
>     }
>
>   const unsigned int Dimension = 3;
>
>   typedef unsigned char                      PixelType;
>   typedef itk::Image< PixelType, Dimension > ImageType;
>
>   typedef itk::ImageRegionConstIterator< ImageType > ConstIteratorType;
>   typedef itk::ImageRegionIterator< ImageType>       IteratorType;
>
>   typedef itk::ImageFileReader< ImageType > ReaderType;
>   typedef itk::ImageFileWriter< ImageType > WriterType;
>
>   ImageType::RegionType inputRegion;
>
>   ImageType::RegionType::IndexType inputStart;
>   ImageType::RegionType::SizeType  size;
>
>   inputStart[0] = ::atoi( argv[3] );
>   inputStart[1] = ::atoi( argv[4] );
>   inputStart[2] = ::atoi( argv[5] );
>
>   size[0]  = ::atoi( argv[6] );
>   size[1]  = ::atoi( argv[7] );
>   size[2]  = ::atoi( argv[8] );
>
>   inputRegion.SetSize( size );
>   inputRegion.SetIndex( inputStart );
>
>   ImageType::RegionType outputRegion;
>
>   ImageType::RegionType::IndexType outputStart;
>
>   outputStart[0] = 0;
>   outputStart[1] = 0;
>   outputStart[2] = 0;
>
>   outputRegion.SetSize( size );
>   outputRegion.SetIndex( outputStart );
>
>   ReaderType::Pointer reader = ReaderType::New();
>   reader->SetFileName( argv[1] );
>   try
>     {
>     reader->Update();
>     }
>   catch ( itk::ExceptionObject &err)
>     {
>     std::cerr << "ExceptionObject caught !" << std::endl;
>     std::cerr << err << std::endl;
>     return -1;
>     }
>
>     // Check that the region is contained within the input image.
>   if ( ! reader->GetOutput()->GetRequestedRegion().IsInside( inputRegion )
> )
>     {
>     std::cerr << "Error" << std::endl;
>     std::cerr << "The region " << inputRegion << "is not contained within
> the input image region "
>               << reader->GetOutput()->GetRequestedRegion() << std::endl;
>     return -1;
>     }
>
>   ImageType::Pointer outputImage = ImageType::New();
>   outputImage->SetRegions( outputRegion );
>   const ImageType::SpacingType& spacing =
> reader->GetOutput()->GetSpacing();
>   const ImageType::PointType& inputOrigin =
> reader->GetOutput()->GetOrigin();
>   double   outputOrigin[ Dimension ];
>
>   for(unsigned int i=0; i< Dimension; i++)
>     {
>     outputOrigin[i] = inputOrigin[i] + spacing[i] * inputStart[i];
>     }
>
>   outputImage->SetSpacing( spacing );
>   outputImage->SetOrigin(  outputOrigin );
>   outputImage->Allocate();
>
>   ConstIteratorType inputIt(   reader->GetOutput(), inputRegion  );
>   IteratorType      outputIt(  outputImage,         outputRegion );
>
>   inputIt.GoToBegin();
>   outputIt.GoToBegin();
>
>   while( !inputIt.IsAtEnd() )
>     {
> if(inputIt.Get()!=0)
>  {
> outputIt.Set(  inputIt.Get() + 100);
> }
>  else
> outputIt.Set(  inputIt.Get()  );
> ++inputIt;
>     ++outputIt;
>     }
>
>   WriterType::Pointer writer = WriterType::New();
>   writer->SetFileName( argv[2] );
>   writer->SetInput( outputImage );
>
>   try
>     {
>     writer->Update();
>     }
>   catch ( itk::ExceptionObject &err)
>     {
>     std::cerr << "ExceptionObject caught !" << std::endl;
>     std::cerr << err << std::endl;
>     return -1;
>     }
>
>   return 0;
>
> I don't really know which is the problem.
> Thanks, bye
>
>
>
>
> 2010/5/17 Sergio Vera <sergio.vera at alma3d.com>
>
> Hi Marco,
>> The correct way of traversing elements of a image in ITK is through
>> iterators:
>>
>> typedef itk::ImageRegionIterator<TITKImgType> IteratorType;
>> IteratorType it(image,image->GetRequestedRegion());
>>
>> for (it.GoToBegin(); !it.IsAtEnd(); ++it) {
>>   it.SetPixel(it.GetPixel()+100);
>> }
>>
>> This is an example and I don't know if it will compile or not, but you get
>> the idea on how to use an iterator to traverse all image pixels. Not only
>> the code is much simpler but also faster.
>>
>> For more info you can look at the itkSoftwareGuide, that has a part
>> dealing with iterators
>>
>> Regards
>>
>> On Mon, May 17, 2010 at 4:00 PM, Marco Gheza <
>> marcogheza4mailinglists at gmail.com> wrote:
>>
>>> Hi,
>>> i'm trying to access every single voxel of a 3d image and change some
>>> values. In particular, i want to change that values that are not black;
>>> every value has to be increased by a value of 100 for example. If a voxel
>>> has value of 10, i want to give it value of 110.
>>>
>>> Here is my code:
>>>
>>> imageCT->DisconnectPipeline();
>>> for(i=0;i<512;i++)
>>>    {
>>> for(j=0;j<512;j++)
>>>  {
>>> for(k=0;k<311;k++)
>>> {
>>>  pixelIndex[0] = i;   // x position
>>> pixelIndex[1] = j;   // y position
>>>  pixelIndex[2] = k;   // z position
>>>
>>> ImageType::PixelType   pixelValuePT = imagePT->GetPixel( pixelIndex );
>>>
>>> ImageType::PixelType  newValue = pixelValueCT+100;
>>>
>>> if(pixelValueCT!=0)
>>>  {
>>> imageCT->SetPixel(   pixelIndex,   newValue  );
>>>  }
>>> }
>>> }
>>>    }
>>>
>>> This code doesn't do the work well. Do you know how to change it?
>>> Thank you,
>>> bye,
>>>
>>> Marco
>>>
>>> _____________________________________
>>> 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
>>>
>>>
>>
>>
>> --
>> Sergio Vera
>>
>> Alma IT Systems
>> C/ Vilana, 4B, 4º 1ª
>> 08022 Barcelona
>> T. (+34) 932 380 592
>> www.alma3d.com
>>
>
>


-- 
Sergio Vera

Alma IT Systems
C/ Vilana, 4B, 4º 1ª
08022 Barcelona
T. (+34) 932 380 592
www.alma3d.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20100517/b2cf4e63/attachment.htm>


More information about the Insight-users mailing list