[Insight-users] extract slice from RGB image

Juan Vidal juanvidalallende at gmail.com
Fri Jul 1 04:01:42 EDT 2011


Ok, I guess that you just want to extract an RGB 2D slice from a 3D RGB
image.

Then, your mistake is OutputPixelType. RGBPixels are vectors of unsigned
chars, and can't be directly converted to scalars (just unsigned chars). The
fastest fix could be this:

  typedef itk::Image<RGBPixelType,2>       OutputPixelType_extract_RGB;

By the way, you probably missed writer->SetFileName() in your code ;)

HTH



2011/7/1 john smith <mkitkinsightuser at gmail.com>

> Well, to explain what I am trying to do. I have created a 3D RGB image as a
> result of a segmentation procedure. To create this image I had used
> itkLabelOverlayImageFilter.So this image was created by a greyscale image
> and a label image, so I get the result of the initial greyscale image and
> the label image with blue color. When I have tried to extract a slice from
> the image  I got this error
> *error C2440: 'static_cast' : cannot convert from
> 'itk::RGBPixel<TComponent>' to 'unsigned char' *. I have included my code
> in the begining of this topic. I also cannot understand the second step very
> well.Why I should do something like this? What should I do for this task?
>
> Thanks for your response
>
>
>
> 2011/7/1 Juan Vidal <juanvidalallende at gmail.com>
>
>> Well, I don't know exactly what your are trying to do, but in my opinion
>> you could probably split your problem:
>>
>> 1) Extract the desired 2D RGB slice from the 3D RGB image
>> (ExtractImageFilter should work).
>> 2) Convert the 2D RGB slice to unsigned char
>>
>> The second point depends on the purpose of your application. You might
>> want to extract one of the channels, or perhaps convert the RGB image to
>> grayscale in some fashion...
>>
>> HTH
>>
>>
>>
>>
>> 2011/6/30 john smith <mkitkinsightuser at gmail.com>
>>
>>> Could somebody explain in some words what must be done in order to
>>> extract a 2D slice from a RGB 3D image? I am trying to find an example (but
>>> I didn't find anything) and I am reading the docs but I am totally lost.
>>> This task may be very easy but I really need some help.
>>> Any response would be very useful.
>>>
>>> Thanks in advace
>>>
>>>
>>> 2011/6/30 john smith <mkitkinsightuser at gmail.com>
>>>
>>>> You may have right as it should work properly.It is the first time I am
>>>> working with RGB images. But what changes should be done?
>>>>
>>>> Thanks for yoyr response
>>>>
>>>>
>>>> 2011/6/30 robert tamburo <robert.tamburo at gmail.com>
>>>>
>>>>> Do you really expect the filter to extract a 2D image of unsigned char
>>>>> pixels from a 3D image of RGB pixels?
>>>>>
>>>>> On Thu, Jun 30, 2011 at 3:07 PM, john smith <
>>>>> mkitkinsightuser at gmail.com> wrote:
>>>>>
>>>>>> Hello to all,
>>>>>>
>>>>>> I am trying to read an RGB image, then use the ExtractImageFilter and
>>>>>> finally, write the RGB slice. I have written the following code, but I get
>>>>>> this error
>>>>>> *error C2440: 'static_cast' : cannot convert from
>>>>>> 'itk::RGBPixel<TComponent>' to 'unsigned char' *. Do you know what I
>>>>>> am doing wrong?
>>>>>>
>>>>>> Thanks in advance
>>>>>>
>>>>>> ///////// code //////////////
>>>>>> typedef itk::RGBPixel<unsigned char> RGBPixelType;
>>>>>>   typedef itk::Image<RGBPixelType,3> RGBImageType;
>>>>>>
>>>>>>    typedef  itk::ImageFileReader< RGBImageType > ReaderType_RGB;
>>>>>>
>>>>>>      ReaderType_RGB::Pointer reader_RGB = ReaderType_RGB::New();
>>>>>>
>>>>>>      reader_RGB->SetFileName( "output.hdr"  );
>>>>>>      reader_RGB->Update();
>>>>>>
>>>>>>
>>>>>>   typedef unsigned char       OutputPixelType_extract_RGB;
>>>>>>   typedef itk::Image< OutputPixelType_extract_RGB, 2 >
>>>>>> OutputImageType_extract_RGB;
>>>>>>    typedef itk::ExtractImageFilter< RGBImageType,
>>>>>> OutputImageType_extract_RGB > FilterType_extract_RGB;
>>>>>>     FilterType_extract_RGB::Pointer filter_extract_RGB =
>>>>>> FilterType_extract_RGB::New();
>>>>>>
>>>>>>   RGBImageType::RegionType inputRegion =
>>>>>>           reader_RGB->GetOutput()->GetLargestPossibleRegion();
>>>>>>
>>>>>>
>>>>>>   RGBImageType::SizeType size = inputRegion.GetSize();
>>>>>>
>>>>>>   // get the size of the hole 3D image
>>>>>>   size_x = size[0];
>>>>>>   size_y = size[1];
>>>>>>    size_z = size[2];
>>>>>>
>>>>>>   // get slices of z coordiante
>>>>>>   size[2] = 0;
>>>>>>
>>>>>>    RGBImageType::IndexType start = inputRegion.GetIndex();
>>>>>>   ui->verticalScrollBar_z->setRange(0,size_z-1);
>>>>>>   unsigned int sliceNumber = ui->verticalScrollBar_z->value();
>>>>>>   start[2] = 10;
>>>>>>
>>>>>>
>>>>>>   RGBImageType::RegionType desiredRegion;
>>>>>>   desiredRegion.SetSize( size );
>>>>>>   desiredRegion.SetIndex( start );
>>>>>>
>>>>>>   filter_extract_RGB->SetExtractionRegion( desiredRegion );
>>>>>>
>>>>>>     typedef  itk::ImageFileWriter< OutputImageType_extract_RGB  >
>>>>>> WriterType_RGB;
>>>>>>      WriterType_RGB::Pointer writer_RGB = WriterType_RGB::New();
>>>>>>     writer_RGB->SetFileName( "z.png" );
>>>>>>
>>>>>>    filter_extract_RGB->SetInput( reader_RGB->GetOutput() );
>>>>>>    writer_RGB->SetInput( filter_extract_RGB->GetOutput() );
>>>>>>
>>>>>>     try
>>>>>>     {
>>>>>>     writer_RGB->Update();
>>>>>>     }
>>>>>>   catch( itk::ExceptionObject & err )
>>>>>>     {
>>>>>>     std::cerr << "ExceptionObject caught !" << std::endl;
>>>>>>     std::cerr << err << std::endl;
>>>>>>     }
>>>>>>
>>>>>> _____________________________________
>>>>>> 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
>>>>>>
>>>>>>
>>>>>
>>>>
>>>
>>> _____________________________________
>>> 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
>>>
>>>
>>
>>
>> --
>> Juan Vidal Allende
>>
>
>


-- 
Juan Vidal Allende
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20110701/1b5d0b47/attachment.htm>


More information about the Insight-users mailing list