1

I'm currently reading BGR values from a set of images,

I have used a variety of imread flags however i cant seem to pull it as BGRA.

my current code is

import cv2
import os

#returns an list of images, list of x, list of y, list of BGR
def load_images_from_folder(folder):
    images = []
    for filename in os.listdir(folder):
        img = cv2.imread(os.path.join(folder,filename),flags=cv2.IMREAD_UNCHANGED)
        if img is not None:
            images.append(img)
    return images

This returns an array([245, 247, 255], dtype=uint8) where i was expecting something like array([245, 247, 255, 0.2], dtype=uint8)

3
  • What's the type of images in the folder? Commented Aug 31, 2019 at 9:41
  • @HiI'mFrogatto they are all .jpg images, 360x640pix Commented Aug 31, 2019 at 9:51
  • 3
    JPG doesn't have any alpha channel. Commented Aug 31, 2019 at 9:54

1 Answer 1

7

The flag cv2.IMREAD_UNCHANGED doesn't add an alpha channel, it only preserve the existing one.

Since your images are all JPG format, you would need to add the forth channel by cvtColor:

img = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)
Sign up to request clarification or add additional context in comments.

3 Comments

this has worked and the alpha channel is all 255 which makes sense if jpg images only contain RGB as you mentioned. I'm attempting to do background subtraction on a mp4 video and so i assume i have lost information by converting it to jpg frames. Do you happen to know what image type would be lose the least information?
@TisButaScratch No you've not lost any information by converting a mp4 to jpg frames as neither a mp4 nor a jpg has an alpha channel. The frames are all opaque.
You do lose information when converting to JPG, because it is a lossy compressed format. Best to use a format that does not use lossy compression, such as PNG or TIFF with LZW or ZIP or no compression.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.