# numpy_calculation_question_set **Repository Path**: cwtxchggta/numpy_calculation_question_set ## Basic Information - **Project Name**: numpy_calculation_question_set - **Description**: 此作业包含一系列的Python代码,用于处理和分析图像。 - **Primary Language**: Python - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 153 - **Created**: 2024-05-09 - **Last Updated**: 2024-05-09 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 项目描述 此项目包含一系列的Python代码,用于处理和分析图像。代码主要执行以下操作: 1. 读取图像文件(例如 'dog.jpg')并将其转换为灰度和RGB格式。 2. 对图像应用随机噪声。 3. 计算图像各个通道(RGB)的各种统计数据,如最大值、最小值、平均值、标准偏差以及第25、50(中位数)、75百分位数。 4. 显示原始图像和噪声图像。 5. 通过局部阈值处理实现绿屏特效 6. 将处理后的图像保存为文件(例如 'out.png')。 1 import cv2 import numpy as np import matplotlib.pyplot as plt # 读取图像并转换为二值图像 img = cv2.imread("plant.png", 0) # 以灰度模式读取图像 img_copy = img.copy() # 创建原始图像的副本,用于最后的显示 _, img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) # 对图像进行二值化处理,阈值为127 # 创建骨架图像和临时图像 skel = np.zeros(img.shape, np.uint8) # 创建一个与原始图像大小相同的空骨架图像 temp = np.zeros(img.shape, np.uint8) # 创建一个与原始图像大小相同的空临时图像 # 定义结构元素 element = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3)) # 定义一个3x3的十字形结构元素 # 主循环 done = False # 标志变量,用于判断是否完成骨架提取 process_imgs = {} # 用于存储处理过程中的图像 while not done: img_cp = img.copy() # 创建原始图像的副本,用于显示处理过程 fg, ax = plt.subplots(1, 5, figsize=(20, 5)) # 创建一个1行6列的子图,用于显示处理过程 ax[0].imshow(img, cmap='gray') ax[0].set_title('Original Image') cv2.morphologyEx(img, cv2.MORPH_OPEN, element, temp) # 进行开操作,用来消除图像中的小白点 ax[1].imshow(temp, cmap='gray') ax[1].set_title('Open Operation') temp = cv2.bitwise_not(cv2.bitwise_and(cv2.bitwise_not(img_cp), temp)) # 进行模拟XOR操作,用于得到图像中的小白点 ax[2].imshow(temp, cmap='gray') ax[2].set_title('XOR Operation') skel = cv2.bitwise_or(skel, temp) # 进行OR操作,用于得到骨架图像 ax[3].imshow(skel, cmap='gray') ax[3].set_title('OR Operation') img = cv2.erode(img, element) # 对原始图像进行腐蚀操作 ax[4].imshow(img, cmap='gray') ax[4].set_title('Erode Operation') # 关闭所有子图的坐标轴显示 for a in ax: a.axis('off') plt.show() # 显示处理过程的子图 done = cv2.countNonZero(img) == 0 # 判断是否完成骨架提取,当原始图像中不再有白色像素时,提取完成 fig, ax = plt.subplots(1, 2, figsize=(10, 5)) # 创建一个1行2列的子图,用于显示最终结果 ax[0].imshow(img_copy, cmap='gray') # 显示原始图像 ax[0].set_title('Original Image') # 设置子图标题 ax[0].axis('off') # 关闭坐标轴显示 ax[1].imshow(skel, cmap='gray') # 显示最终的骨架图像 ax[1].set_title('Skeleton Image') # 设置子图标题 ax[1].axis('off') # 关闭坐标轴显示 plt.show() # 显示最终结果的子图 2 from PIL import Image import numpy as np import matplotlib.pyplot as plt # 加载图像 monkey_img = Image.open("monkey.jpg") moon_img = Image.open("moon.jpg") # 调整图像大小以匹配 moon_size = moon_img.size monkey_img = monkey_img.resize(moon_size) # 将图像转换为NumPy数组 monkey_arr = np.array(monkey_img) moon_arr = np.array(moon_img) # 计算平均值 avg = monkey_arr.mean(axis=2) # 根据条件替换像素值 mask = (monkey_arr[:,:,2] < 0.9 * avg) moon_arr[mask] = monkey_arr[mask] # 将NumPy数组转换回Image对象 result_img = Image.fromarray(moon_arr.astype('uint8')) # 创建画布 fig, axs = plt.subplots(1, 3, figsize=(15, 5)) # 显示原始图像 axs[0].imshow(monkey_img) axs[0].set_title("Monkey") # 显示背景图像 axs[1].imshow(moon_img) axs[1].set_title("Moon") # 显示合成图像 axs[2].imshow(result_img) axs[2].set_title("Monkey Moon") # 去除子图的坐标轴 for ax in axs: ax.axis('off') # 调整子图之间的间距 plt.tight_layout() # 显示图像 plt.show() 3 import numpy as np from PIL import Image import matplotlib.pyplot as plt # 使用Image.open()函数读取图片 img_rgb = Image.open("dog.jpg") # 将图片转化为数组 img_np = np.array(img_rgb) #分离RGB三通道 r, g, b = img_np[:, :, 0], img_np[:, :, 1], img_np[:, :, 2] #创建RGB三通道彩图 r_img = np.zeros_like(img_np) r_img[:, :, 0] = r g_img = np.zeros_like(img_np) g_img[:, :, 1] = g b_img = np.zeros_like(img_np) b_img[:, :, 2] = b #RGB单通道灰度图 r_gray = Image.fromarray(r).convert("L") g_gray = Image.fromarray(g).convert("L") b_gray = Image.fromarray(b).convert("L") # 创建画布 fig, axs = plt.subplots(3, 3, figsize=(15, 15)) # 第一行:RGB原图、第二行图合成RGB图,第三行图合成RGB图 axs[0, 0].imshow(img_rgb) axs[0, 0].set_title("Original Image") img_new_rgb = np.dstack((r, g, b)) axs[0, 1].imshow(img_new_rgb) axs[0, 1].set_title("Merged Image") img_new_rgb_gray = np.dstack((r_gray, g_gray, b_gray)) axs[0, 2].imshow(img_new_rgb_gray) axs[0, 2].set_title("Merged_gray Image") # 第二行:R、G、B三通道彩图 axs[1, 0].imshow(r_img) axs[1, 0].set_title("R Channel ") axs[1, 1].imshow(g_img) axs[1, 1].set_title("G Channel ") axs[1, 2].imshow(b_img) axs[1, 2].set_title("B Channel ") # 第三行:R、G、B单通道灰度图 axs[2, 0].imshow(r_gray, cmap='gray') axs[2, 0].set_title("R Channel Gray Image") axs[2, 1].imshow(g_gray, cmap='gray') axs[2, 1].set_title("G Channel Gray Image") axs[2, 2].imshow(b_gray, cmap='gray') axs[2, 2].set_title("B Channel Gray Image") plt.show() 4 # 读取图像并转换为RGB格式 img = Image.open('dog.jpg').convert('RGB') img_array = np.array(img) # 生成符合20倍标准正态分布的噪音 noise = 20 * np.random.normal(size=img_array.shape) # 将噪音添加到RGB图像上 noisy_img_array = img_array + noise # 对加上噪音的彩图进行极小极大值归一化处理 noisy_img_array = (noisy_img_array - noisy_img_array.min()) / (noisy_img_array.max() - noisy_img_array.min()) # 显示原图 plt.subplot(1, 2, 1) plt.imshow(img_array) plt.title('Original Image') # 显示添加噪音后的图像 plt.subplot(1, 2, 2) plt.imshow(noisy_img_array) plt.title('Noisy Image') # 显示图像 plt.show() 5 from PIL import Image import numpy as np from tabulate import tabulate # 读取图片并转换为numpy数组 img = Image.open("dog.jpg") img_array = np.array(img) # 计算每个通道的最大值、最小值、平均值、标准差以及分位数 max_values = np.max(img_array, axis=(0, 1)) min_values = np.min(img_array, axis=(0, 1)) mean_values = np.mean(img_array, axis=(0, 1)) std_values = np.std(img_array, axis=(0, 1)) percentile_25 = np.percentile(img_array, 25, axis=(0, 1)) percentile_50 = np.percentile(img_array, 50, axis=(0, 1)) percentile_75 = np.percentile(img_array, 75, axis=(0, 1)) # 生成表格数据 table_data = [ ["max", max_values[0], max_values[1], max_values[2]], ["min", min_values[0], min_values[1], min_values[2]], ["mean", mean_values[0], mean_values[1], mean_values[2]], ["std", std_values[0], std_values[1], std_values[2]], ["25%", percentile_25[0], percentile_25[1], percentile_25[2]], ["50%", percentile_50[0], percentile_50[1], percentile_50[2]], ["75%", percentile_75[0], percentile_75[1], percentile_75[2]], ] # 使用tabulate生成表格 table = tabulate(table_data, headers=["meta", "R", "G", "B"], tablefmt="grid") print(table) 6 import matplotlib.pyplot as plt import numpy as np import cv2 # 加载原始图像 img = cv2.imread('dog.jpg', cv2.IMREAD_GRAYSCALE) # 添加噪音 noise = np.random.normal(0, 20, size=img.shape) noisy_img = img + noise # 创建子图 fig, axs = plt.subplots(1, 2, figsize=(10, 5)) # 显示原始图像 axs[0].imshow(img, cmap='gray') axs[0].set_title('Original Image') # 显示添加噪音后的图像 axs[1].imshow(noisy_img, cmap='gray') axs[1].set_title('Noisy Image') # 去除子图的坐标轴 for ax in axs: ax.axis('off') # 调整子图之间的间距 plt.tight_layout() # 显示图像 plt.show() # 项目运行效果截图 [TODO: 此处上传效果截图] '![alt text](4bc467485ae998ed740148adf653e1a.png)' '![alt text](ab3773405e02435d162ff1def230c7d.png)' '![alt text](ab3773405e02435d162ff1def230c7d-1.png)' '![alt text](a931c20ae53a899a35ae6bab7812b2b.png)' '![alt text](4a1c60b9b0672ca4ac20e32f1415270.png)' # 功能 - 图像读取:能够读取灰度和RGB图像。 - 噪声添加:能够为图像添加随机噪声。 - 统计分析:能够计算并打印图像各个通道(RGB)的统计数据。 - 图像显示和保存:能够显示原始图像和噪声图像。 # 依赖 本项目依赖以下Python库: - Matplotlib - NumPy - OpenCV - skimage - tabulate # 使用 1. 克隆或下载此项目到本地。 2. 确保已安装上述所有Python库。 3. 执行 `np_calculation_questions.ipynb` Jupyter notebook。 # 注意 - 图像文件(如 'dog.jpg')应放在与notebook相同的目录下。 - 随机噪声的添加可能会导致图像出现许多全白点,建议进行适当的像素值归一化。 # 个人信息 [TODO: 此处填写个人信息] - 学号: 202252320102 - 年级: 2022 - 专业: 智能科学与技术 - 班级: 1 班