# assignment2_homework **Repository Path**: duan-qi-rui/assignment2_homework ## Basic Information - **Project Name**: assignment2_homework - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-03-15 - **Last Updated**: 2025-03-15 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README import skimage.io as skio # 导入skimage库的io模块,用于图像读取 import matplotlib.pyplot as plt # 导入matplotlib绘图模块 import numpy as np # 导入numpy数值计算库 image1 = skio.imread('x.jpg') # 读取x.jpg图片到image1变量 print(f"图像1的形状: {image1.shape}") # 打印图像1的维度信息(H,W,C) height1, width1, channels1 = image1.shape # 解包图像高度/宽度/通道数 image1[:5, :, 0] = 255 # 将前5行所有列的第1个通道像素设为255(白色) plt.imshow(image1) # 显示处理后的图像1 plt.show() # 结束当前图像显示并清空画布 image2 = skio.imread('x.jpg') # 重新读取x.jpg到image2 print(f"图像2的形状: {image2.shape}") # 打印图像2的维度信息 height2, width2, channels2 = image2.shape # 解包图像尺寸参数 image2[:, :5, 0] = 255 # 将所有行前5列的第1个通道设为255(横向白条) plt.imshow(image2) # 显示图像2 plt.show() # 结束显示 image3 = skio.imread('x.jpg') # 第三次读取x.jpg print(f"图像3的形状: {image3.shape}") # 打印图像3形状 height3, width3, channels3 = image3.shape # 解包尺寸 image3[:5, :5, 0] = 255 # 在左上角5x5区域设置白色方块 plt.imshow(image3) # 显示图像3 plt.show() # 结束显示 image4 = skio.imread('x.jpg') # 第四次读取x.jpg print(f"图像4的形状: {image4.shape}") # 打印图像4形状 height4, width4, channels4 = image4.shape # 解包尺寸 coordinates = [(3, 4), (3, 5), (4, 3), (4, 6), (5, 3), (5, 6), (6, 4), (6, 5)] # 定义8个坐标点 for y, x in coordinates: # 遍历坐标列表 image4[y, x, 0] = 255 # 在指定坐标点设置白色像素(星形图案) plt.imshow(image4) # 显示图像4 plt.show() # 结束显示 image5 = skio.imread('x.jpg') # 第五次读取x.jpg print(f"图像5的形状: {image5.shape}") # 打印图像5形状 height5, width5, channels5 = image5.shape # 解包尺寸 for col in range(1, width5 - 1): # 遍历所有列(除去边界) for row in range(1, height5 - 1): # 遍历所有行(除去边界) if np.all(image5[row, col] == 255): # 如果是白色像素 image5[row, col] = [0, 0, 255] # 改为蓝色 else: # 否则 image5[row, col] = [0, 255, 0] # 改为绿色 image5[7, 7] = [0, 0, 255] # 在(7,7)位置设置蓝色像素 plt.imshow(image5) # 显示图像5 plt.show() # 结束显示 image6 = skio.imread('galaxy-full.jpg') # 读取银河系图片 red = image6.copy() # 创建红色通道副本 red[:, :, 1:] = 0 # 关闭绿蓝通道(只保留红色) green = image6.copy() # 创建绿色通道副本 green[:, :, [0, 2]] = 0 # 关闭红蓝通道(只保留绿色) blue = image6.copy() # 创建蓝色通道副本 blue[:, :, :2] = 0 # 关闭红绿通道(只保留蓝色) fig, axes = plt.subplots(1, 3) # 创建1行3列的子图布局 axes[0].imshow(red) # 显示红色通道 axes[0].set_title('Red Channel') # 设置子图标题 axes[1].imshow(green) # 显示绿色通道 axes[1].set_title('Green Channel') # 设置标题 axes[2].imshow(blue) # 显示蓝色通道 axes[2].set_title('Blue Channel') # 设置标题 plt.show() # 显示最终的三通道分离图像 import matplotlib.pyplot as plt # 再次导入matplotlib(代码复用) from skimage import io # 从skimage导入io模块 import numpy as np # 导入numpy库 img = io.imread('earth.jpg') # 读取earth.jpg图片 height, width, _ = img.shape # 获取图像高度/宽度/通道数 stripe_width = 3 # 定义条纹宽度为3像素 new_img = np.zeros_like(img) # 创建与原图相同形状的全零矩阵 for i in range(0, width, 3 * stripe_width): # 按3倍条纹宽度循环 new_img[:, i:i + stripe_width, 0] = img[:, i:i + stripe_width, 0] # 复制原图红色通道到新图 new_img[:, i + stripe_width:i + 2 * stripe_width, 1] = img[:, i + stripe_width:i + 2 * stripe_width, 1] # 复制绿色通道 new_img[:, i + 2 * stripe_width:i + 3 * stripe_width, 2] = img[:, i + 2 * stripe_width:i + 3 * stripe_width, 2] # 复制蓝色通道 new_img[:, i:i + stripe_width, 1] = img[:, i:i + stripe_width, 1] * 0.5 # 红色区域绿色通道透明度50% new_img[:, i:i + stripe_width, 2] = img[:, i:i + stripe_width, 2] * 0.5 # 红色区域蓝色通道透明度50% mean_brightness = np.mean(new_img) # 计算新图平均亮度 if mean_brightness < 128: # 如果亮度低于128 factor = 128 / mean_brightness if mean_brightness > 0 else 1 # 计算亮度补偿因子 new_img = np.clip(new_img * factor, 0, 255).astype(np.uint8) # 亮度标准化处理 plt.imshow(new_img) # 显示最终图像 plt.axis('off') # 关闭坐标轴 plt.show() # 结束显示