三通道图像分解

2024-06-16 11:08:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试实现一个函数,该函数将图像分解为通道:R、G和B,并返回不包含指定通道的图像。但我得到的只是黑色的图像。由于我是图像处理新手,帮助将是巨大的。你知道吗

import math
from PIL import Image
import numpy as np
from skimage import color, io
import matplotlib.pyplot as plt


def load(image_path):
    out = plt.imread(image_path)

    out = out.astype(np.float64) / 255
    return out

def display(img):
    # Show image
    plt.figure(figsize = (5,5))
    plt.imshow(img)
    plt.axis('off')
    plt.show()


def rgb_exclusion(image, channel):
    out = image
    if channel == 'R':
        out[:, :, 0] = 0
    elif channel == 'G':
        out[:, :, 1] = 0
    elif channel == 'B':
        out[:, :, 2] = 0


image1 = load(image1_path)
image2 = load(image2_path)

display(image1)
display(image2)

without_red = rgb_exclusion(image1, 'R')
without_blue = rgb_exclusion(image1, 'B')
without_green = rgb_exclusion(image1, 'G')

print("Below is the image without the red channel.")
display(without_red)

print("Below is the image without the green channel.")
display(without_green)

print("Below is the image without the blue channel.")
display(without_blue)

Tags: thepath图像imageimportdefdisplaychannel