# convert rgb to rgba

**URL:** https://discourse.itk.org/t/convert-rgb-to-rgba/3414
**Category:** Uncategorized
**Created:** [August 28, 2020, 2:15pm UTC](https://discourse.itk.org/t/convert-rgb-to-rgba/3414 "2020-08-28T14:15:12Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Henco](https://discourse.itk.org/letter_avatar_proxy/v4/letter/h/cab0a1/32.png) [@Henco](https://discourse.itk.org/u/Henco)
#### Post date: [August 28, 2020, 2:15pm UTC](https://discourse.itk.org/t/convert-rgb-to-rgba/3414/1 "2020-08-28T14:15:12Z")

</div>

Hi all,  
I’m attempting to convert an RGB pixel type image to a RGBA pixel type image, as I would like to be able to overlay it on another image and therefore need it to be slightly transparent. I tried to cast using itk::CastImageFilter\<RGBImageType, RGBAImageType\>, but I received an ITK error: “Cannot set the size of a RGBAPixel to anything other than 4”. I also receive an error in my IDE from itkCastImageFilter.hxx that “Vector dimensions are required to match!”.  
I searched around and found [this post](https://discourse.itk.org/t/add-alpha-channel-to-an-image-in-simpleitk/2665/2), but it pertains to a simpleITK treatment of the problem. Is there a similar way to do this using regular ITK?  
Edit: I also found this [example code](https://itk.org/Doxygen/html/SphinxExamples_2src_2IO_2ImageBase_2ConvertImageToAnotherType_2Code_8cxx-example.html#_a4) which seems to be ideal for my purpose, but I don’t understand how to adapt it to convert from RGB to RGBA while keeping the RGB values from the original image.

---

<div class="post-metadata">

### Author: ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)
#### Post date: [August 28, 2020, 4:41pm UTC](https://discourse.itk.org/t/convert-rgb-to-rgba/3414/2 "2020-08-28T16:41:08Z")

</div>

It looks like you have to make a copy of your RGB image and convert that copy.

---

<div class="post-metadata">

### Author: ![Henco](https://discourse.itk.org/letter_avatar_proxy/v4/letter/h/cab0a1/32.png) [@Henco](https://discourse.itk.org/u/Henco)
#### Post date: [August 28, 2020, 5:12pm UTC](https://discourse.itk.org/t/convert-rgb-to-rgba/3414/3 "2020-08-28T17:12:42Z")

</div>

Mr. Chen,

Yes I agree, that does sound like what needs to be done. However, my issue is stemming from the conversion from RGB to RGBA. That converter is what is throwing the error. Do you know how to set up the converter to take an RGB image and return it as RGBA?

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [August 28, 2020, 5:28pm UTC](https://discourse.itk.org/t/convert-rgb-to-rgba/3414/4 "2020-08-28T17:28:26Z")

</div>

The best way to do this in ITK would be to create an `RGBtoRGBAAdaptor` and use it in an ImageAdaptor.

Please refer to the ITK Software Guide’s [Image Adaptor](https://itk.org/ITKSoftwareGuide/html/Book1/ITKSoftwareGuide-Book1ch7.html#x52-1220007) chapter for details.

---

<div class="post-metadata">

### Author: ![Henco](https://discourse.itk.org/letter_avatar_proxy/v4/letter/h/cab0a1/32.png) [@Henco](https://discourse.itk.org/u/Henco)
#### Post date: [August 28, 2020, 5:29pm UTC](https://discourse.itk.org/t/convert-rgb-to-rgba/3414/5 "2020-08-28T17:29:03Z")

</div>

Mr. Lowekamp,

Thanks! I’ll take a look at The Guide
