Search code examples
pythonopencvcolorsalphacolor-space

Difference between ARGB, RGBA Color Space in Python OpenCV


I have a lot of questions about RGBA and ARGB color models.

  1. What is the difference between RGBA and ARGB color space. Is it all about the arrangement of Alpha, Red, Green and Blue Channels?

  2. In my case, I want to read an image with jpg extension. Can I extract alpha channel from the image?

  3. Using an image with jpg extension. How will I convert the image into RGBA color space in python? Below is my initial code:

    frame = cv2.imread("image.jpg", -1)
    print( frame[0,0])
    

The print statement above only display RGB values. How will I include the alpha channel?


Solution

  • What is the difference between RGBA and ARGB color space. Is it all about the arrangement of Alpha, Red, Green and Blue Channels?

    Yes, it's all about the arrangement.

    In my case, I want to read an image with jpg extension. Can I extract alpha channel from the image?

    No. JPG doesn't support transparency. You need other formats like PNG to store the alpha channel.

    Using an image with jpg extension. How will I convert the image into RGBA color space in python?

    You are loading your image with IMREAD_UNCHANGED (-1), which "If set, return the loaded image as is (with alpha channel, otherwise it gets cropped)."

    If your image has alpha channel (e.g. PNG) you are done. Otherwise, like in your case, read the image as BGR (default, IMREAD_COLOR) and then convert to BGRA:

    frame = cv2.imread("image.jpg")
    bgra = cv.cvtColor(frame, cv.COLOR_BGR2BGRA)
    

    You can check the number of channels of your image with:

     height, width, channels = img.shape 
    

    channels must be 4 for BGRA.