如何使用 opencv 读取 RGBA

How can you read RGBA using opencv

我目前正在从一组图像中读取 BGR 值,

我使用了多种 imread 标志,但我似乎无法将其作为 BGRA 提取。

我当前的密码是

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

这个 returns 一个 array([245, 247, 255], dtype=uint8) 我期待的是 array([245, 247, 255, 0.2], dtype=uint8)

标志 cv2.IMREAD_UNCHANGED 不会 添加 alpha 通道,它只会 保留 现有通道。

由于您的图片都是JPG格式,您需要通过cvtColor添加第四个通道:

img = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)