如何使用 numpy 读取灰度蒙版图像?

How to how to read grayscale mask image using numpy?

我正在阅读某人关于语义分割的代码,试图学习一些技术,但我可以找出一个特定的部分,我真的需要一个解释 这是整个函数

def DataGen():
    img_ = []
    mask_ = []
    c1 = []
    y1 = []
    for i in range(len(image_)):
        image = cv2.imread(image_[i])
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        image = image / 255
        image = cv2.resize(image, (height, width), interpolation=cv2.INTER_AREA)  # image
        cc1 = cv2.resize(image, (height // 2, width // 2), interpolation=cv2.INTER_AREA)  # resize image

        mask = cv2.imread(mask_id[i], 0)
        mask[np.where(mask == 0)] = 198
        target = np.zeros([966, 1296, 2])

        target[:, :, 1][np.where(mask == 149)] = 1
        target[:, :, 0][np.where(mask == 76)] = 1

        mask = cv2.resize(target, (height, width), interpolation=cv2.INTER_AREA)
        yy1 = cv2.resize(target, (height // 2, width // 2), interpolation=cv2.INTER_AREA)
        mask = np.expand_dims(mask, axis=-1)
        print(mask)
        yy1 = np.expand_dims(yy1, axis=-1)
        img_.append(image)
        mask_.append(mask)
        c1.append(cc1)
        y1.append(yy1)
    img_ = np.array(img_)
    C1 = np.array(c1)
    Y1 = np.array(y1)
    mask_ = np.array(mask_)
    mask_[np.where(mask_ != 0)] = 1
    Y1[np.where(Y1 != 0)] = 1
    return img_, C1, mask_, Y1

这是我有点困惑的地方

mask = cv2.imread(mask_id[i], 0) # mask is read in grayscale Point-1
mask[np.where(mask == 0)] = 198

target = np.zeros([966, 1296, 2]) # numpy is define Point-2
target[:, :, 1][np.where(mask == 149)] = 1
target[:, :, 0][np.where(mask == 76)] = 1

mask = cv2.resize(target, (height, width), interpolation=cv2.INTER_AREA)

掩码如何进入Point-2初始化的目标变量中的numpy数组?

我早就想到了,所以我决定回答它...也许它会对其他人有所帮助。

`目标[:, :, 1][np.where(掩码== 149)] = 1

目标[:, :, 0][np.where(掩码== 76)] = 1 `

这个“np.where(mask == 149)”可以简单地删除并替换为任何整数值作为索引。作者从 MxN 掩模图像数组中选择该索引并进行设置。它只是 numpy 中的一个变量声明,可以重写为

` 目标 = np.zeros([966, 1296, 2])

目标[:, :, 1][100] = 1

目标[:, :, 0][75] = 1 `