# 图像基础处理 **Repository Path**: ipa-project01-palette/image-basic-processing ## Basic Information - **Project Name**: 图像基础处理 - **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-16 - **Last Updated**: 2025-03-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 姓名:马闯 学号:202352320205 专业:智能科学与技术 班级:2班 # 数字图像处理实验一:图像基础操作 ## 环境要求 - Python 3.12.7 - 安装以下Python库: - numpy - scikit-image - matplotlib ## 文件说明 - Untitled-1.ipynb:包含所有图像处理的代码示例。 ## 代码示例说明 ### 1. 图像上半部分处理 读取图像并将上半部分的非白色区域设为红色。 ```python import numpy as np from skimage import io import matplotlib.pyplot as plt # 读取原始图像 img = io.imread('x.jpg') # 注意反斜杠转义 img1 = img.copy() height, width = img1.shape[:2] white_threshold = 200 # 图1:图像上半部分处理(示例设为红色),不遮挡白色区域 for i in range(height // 2): for j in range(width): if np.all(img1[i, j] < white_threshold): img1[i, j] = [255, 0, 0] plt.imshow(img1) plt.title('图1 图像上半部分处理') plt.axis('off') plt.show() ``` ### 2. 图像左半部分处理 读取图像并将左半部分的非白色区域设为红色。 ```python import numpy as np from skimage import io import matplotlib.pyplot as plt # 读取原始图像 img = io.imread('x.jpg') img2 = img.copy() height, width = img2.shape[:2] white_threshold = 200 # 图2:图像左半部分处理(示例设为红色),不遮挡白色区域 for i in range(height): for j in range(width // 2): if np.all(img2[i, j] < white_threshold): img2[i, j] = [255, 0, 0] plt.imshow(img2) plt.title('图2 图像左半部分处理') plt.axis('off') plt.show() ``` ### 3. 图像左上四分之一处理 读取图像并将左上四分之一的非白色区域设为红色。 ```python import numpy as np from skimage import io import matplotlib.pyplot as plt # 读取原始图像 img = io.imread('x.jpg') img3 = img.copy() height, width = img3.shape[:2] white_threshold = 200 # 图3:图像左上四分之一处理(示例设为红色),不遮挡白色区域 for i in range(height // 2): for j in range(width // 2): if np.all(img3[i, j] < white_threshold): img3[i, j] = [255, 0, 0] plt.imshow(img3) plt.title('图3 图像左上四分之一处理') plt.axis('off') plt.show() ``` ### 4. 小区域像素处理 读取图像并将中心小区域的非白色像素设为红色。 ```python import numpy as np from skimage import io import matplotlib.pyplot as plt # 读取原始图像 img = io.imread('x.jpg') img4 = img.copy() height, width = img4.shape[:2] white_threshold = 200 center_y, center_x = height // 2, width // 2 # 图4:小区域像素处理(示例中心区域设为红色),不遮挡白色区域 for i in range(center_y - 2, center_y + 2): for j in range(center_x - 2, center_x + 2): if np.all(img4[i, j] < white_threshold): img4[i, j] = [255, 0, 0] plt.imshow(img4) plt.title('图4 小区域像素处理') plt.axis('off') plt.show() ``` ### 5. 指定区域颜色处理 读取图像并将中间区域的非白色像素设为绿色,白色部分变为黄色。 ```python import numpy as np from skimage import io import matplotlib.pyplot as plt # 读取原始图像 img = io.imread('x.jpg') img5 = img.copy() height, width = img5.shape[:2] white_threshold = 200 # 图5:指定区域颜色处理(示例中间区域设为绿色),白色部分变为黄色 for i in range(height // 2 - 4, height // 2 + 4): for j in range(width // 2 - 4, width // 2 + 4): if np.all(img5[i, j] < white_threshold): img5[i, j] = [0, 255, 0] else: img5[i, j] = [255, 255, 0] plt.imshow(img5) plt.title('图5 指定区域颜色处理') plt.axis('off') plt.show() ``` ### 6-8. 星系图像颜色通道处理 读取图像并分别保留红色、绿色和蓝色通道。 ```python from skimage import io import matplotlib.pyplot as plt # 读取图像(指定绝对路径) img = io.imread('galaxy-full.jpg') # 保留红色通道,绿蓝通道设为0 red_channel = img.copy() red_channel[:, :, 1] = 0 # 绿色通道置0 red_channel[:, :, 2] = 0 # 蓝色通道置0 # 显示图像 plt.imshow(red_channel) plt.title('红色通道图') plt.show() # 保留绿色通道,红蓝通道设为0 green_channel = img.copy() green_channel[:, :, 0] = 0 # 红色通道置0 green_channel[:, :, 2] = 0 # 蓝色通道置0 # 显示图像 plt.imshow(green_channel) plt.title('绿色通道图') plt.show() # 保留蓝色通道,红绿通道设为0 blue_channel = img.copy() blue_channel[:, :, 0] = 0 # 红色通道置0 blue_channel[:, :, 1] = 0 # 绿色通道置0 # 显示图像 plt.imshow(blue_channel) plt.title('蓝色通道图') plt.show() ``` ### 9. 条纹效果处理 读取图像并对图像进行条纹效果处理。 ```python from skimage import io import matplotlib.pyplot as plt import numpy as np # 读取指定路径的图像 img = io.imread('earth.jpg') img_processed = img.copy().astype(np.float32) # 转换为浮点类型方便计算 stripes_interval = 1 # 条纹间隔设为1,让条纹更密集 brightness_increase = 250 # 大幅增加亮度值 for col in range(img_processed.shape[1]): # 根据列索引对3取余,分配红、绿、蓝通道处理 if col % 3 == 0: img_processed[:, col, 0] = np.clip(img_processed[:, col, 0] + brightness_increase, 0, 255) elif col % 3 == 1: img_processed[:, col, 1] = np.clip(img_processed[:, col, 1] + brightness_increase, 0, 255) else: img_processed[:, col, 2] = np.clip(img_processed[:, col, 2] + brightness_increase, 0, 255) # 放大显示图像 plt.figure(figsize=(15, 15)) plt.imshow(img_processed.astype(np.uint8)) plt.title('图9 条纹效果处理') plt.show() ``` ## 使用方法 1. 确保已安装所需的Python库。 2. 下载并打开`Untitled-1.ipynb`文件。 3. 逐个运行代码单元,观察每个图像处理的效果。 ## 学习要点 - 学习如何使用`numpy`和`scikit-image`库进行图像处理。 - 掌握基本的图像操作,如读取、复制、修改像素值等。 - 了解如何使用`matplotlib`库显示图像处理结果。