[Insight-users] Iterators and RGB images
Zachary Pincus
zpincus at stanford.edu
Sun Jun 26 20:26:54 EDT 2005
Hello Julian,
First off, have you checked out the ITK Software Guide (from
http://itk.org/HTML/Documentation.htm )? It is a very helpful document
which explains a lot of important ITK concepts, including how to
operate on RGB images and deal with RGB pixel types (see Section II:
User's Guide > Data Representation > Image > RGB Image).
I'll give a brief summary of the concepts:
Like almost everything in ITK, the iterator classes are templated over
the image type. The Set() method then takes as a parameter the pixel
type of the tempated image type. For exampler, say we have the
following:
typedef unsigned char PixelType;
typedef itk::Image<PixelType, 2> ImageType;
typedef itk::ImageRegionIterator<ImageType> IteratorType;
Then, IteratorType objects will have a method:
void Set(ImageType::PixelType &);
which is to say:
void Set(unsigned char);
Now, let's look at a typical definition of an RGB image:
typedef unsigned char PixelComponentType;
typedef itk::RGBPixel<PixelComponentType> RGBPixelType;
typedef itk::Image<RGBPixelType, 2> RGBImageType;
typedef itk::ImageRegionIterator<RGBImageType> RGBIteratorType;
Now, RGBIteratorType objects will have a method:
void Set(RGBImageType::PixelType &);
which is to say:
void Set(itk::RGBPixel<unsigned char> &);
So, the Set() method of an iterator over RGB images requires a RGBPixel
object to be passed to it (reasonably so!). So we need to make an
RGBPixel object as required, and then pass *that* to the iterator's
Set() method. (Note that you could also pass to Set() a data type which
can be converted to an RGBPixel object via a constructor or cast
method, but that's an advanced topic.) So, we just need to figure out
how to make an RGBPixel object with the color that we want.
To do so, let's look at the methods available for an RGB pixel by
consulting the ITK documentation
(http://www.itk.org/Doxygen/html/classes.html ). Specifically:
http://www.itk.org/Doxygen/html/classitk_1_1RGBPixel.html . Note that
there methods SetRed(), SetGreen(), SetBlue(), or a Set(red, green,
blue) method of this pixel type. So here's how I would make a blue
image via an iterator (somewhat expanded for clarity):
RGBIteratorType iterator(someRGBImage,
someRGBImage->GetRequestedRegion());
for (iterator.GoToBegin(); !iterator.IsAtEnd(); ++iterator) {
RGBPixelType newPixel;
newPixel.SetRed(0);
newPixel.SetGreen(0);
newPixel.SetBlue(255);
iterator.Set(newPixel);
}
Does this answer your question? If I've misinterpreted what you're
asking, all apologies.
Zach Pincus
Department of Biochemistry and Program in Biomedical Informatics
Stanford University School of Medicine
On Jun 26, 2005, at 1:01 PM, Julian Tam wrote:
> Hello,
>
> I have a series of black and white input images. I am hoping to
> output an RGB image based on the intensity of the input image (eg.
> within a certain intensity range, the pixel should appear as red, and
> within another, blue).
>
> For the sake of efficiency, I would be interested in using an iterator
> to access every pixel and check if the processing is needed. However,
> I can't seem to find a suitable method to do this. I've been able
> to use the Set() method to modify the intensity of a black and white
> image, but haven't come across a way to set the individual red, green,
> and blue values of the image.
>
> Could someone please suggest a possible approach for my problem?
>
> Thank you,
> Julian Tam
> _______________________________________________
> Insight-users mailing list
> Insight-users at itk.org
> http://www.itk.org/mailman/listinfo/insight-users
>
More information about the Insight-users
mailing list