From a61153a7ce807f1999c9a8b5c1f017e5e69a65d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=AE=B6=E4=B9=90?= <14352673+xujialellll@user.noreply.gitee.com> Date: Mon, 10 Jun 2024 08:51:13 +0000 Subject: [PATCH] update image_channels.py. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 徐家乐 <14352673+xujialellll@user.noreply.gitee.com> --- image_channels.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/image_channels.py b/image_channels.py index 0b27ee8..4fbac83 100644 --- a/image_channels.py +++ b/image_channels.py @@ -1,8 +1,8 @@ + import numpy as np import image_io from image_utils import logging - def split_image_channels(image): ''' 该函数用于分割图像的颜色通道 @@ -12,20 +12,19 @@ def split_image_channels(image): try: logging.info("开始分割图像颜色通道") - # TODO: 1. 检测多通道图像 image 是否真多通道的,如果是,显示错误信息并返回 - # 提示: 使用 raise 抛出异常信息:图像通道数必须大于等于2 - - - # TODO: 2. 使用numpy的split函数,沿着通道维度分割图像 + # 检查图像是否是多通道的,如果不是,抛出异常信息 + if len(image.shape) != 3 or image.shape[2] < 2: + raise ValueError("图像通道数必须大于等于2") + # 使用numpy的split函数,沿着通道维度分割图像 + channel_list = image[..., :image.shape[2] // 2] - # TODO: 3. 把分割后的通道图像添加到一个列表 channel_list 中,注意要去掉多余的维度 - channel_list = + # 把分割后的通道图像添加到一个列表 channel_list 中,注意要去掉多余的维度 + channel_list = [channel_list[i * 2:(i + 1) * 2] for i in range(channel_list.shape[2])] logging.info(f"成功分割图像颜色通道") return channel_list except Exception as e: - # print(f"在分割图像颜色通道时发生错误: {e}") raise ValueError(f"在分割图像颜色通道时发生错误: {e}") def merge_image_channels(file_paths): @@ -37,10 +36,12 @@ def merge_image_channels(file_paths): try: logging.info("开始合并图像通道") - # TODO: 想办法加载每个图像文件,然后将它们合并成一个多通道图像 merged_image - # 注意: 检查每个图像是否是单通道的,如果不是,raise 一个异常信息: 要合并子图像必须是单通道图像,不能是多通道图像 + # 加载每个图像文件,然后将它们合并成一个多通道图像 merged_image + merged_image = np.concatenate([image_io.load_image(fp) for fp in file_paths], axis=-1) - merged_image = + # 检查每个图像是否是单通道的,如果不是,抛出异常信息 + if merged_image.shape[2] > 1: + raise ValueError("要合并的子图像必须是单通道图像,不能是多通道图像") logging.info("成功合并图像通道") return merged_image -- Gitee