# basic-processing **Repository Path**: enginx321/basic-processing ## Basic Information - **Project Name**: basic-processing - **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 # 数字图像处理实验一:图像基础操作 #### 环境要求 1. 在Linux下安装Jupyter Lab 2. 把项目克隆到Jupyter Lab 3. Python库依赖 pip install Pillow==9.5.0 # 图像处理核心库(PIL的现代分支) pip install matplotlib==3.7.2 # 图像可视化 pip install numpy==1.24.3 # 数组操作加速(用于条纹效果处理) pip install scikit-image==0.21.0 # 高级图像处理(代码中导入但未实际使用) #### 文件说明 1. x.jpg 2. galaxy-full.jpg 3. earth.jpg #### 代码示例说明 1. 图像上半部分处理 from PIL import Image from skimage import io # 从名为'skimage'的库中导入'io'模块,这个模块通常用于读取图像 import matplotlib.pyplot as plt image = Image.open('x.jpg') # 打开图片 image = image.convert('RGB') # 将图片转换为RGB模式 width, height = image.size# 获取图片的宽度和高度 for x in range(width):# 遍历上半部分的像素 for y in range(height // 2): r, g, b = image.getpixel((x, y)) # 如果像素接近黑色(可以根据需要调整阈值) if r < 50 and g < 50 and b < 50: # 将黑色像素改为红色 image.putpixel((x, y), (255, 0, 0)) plt.imshow(image) # 使用'imshow'函数显示图像# 保存修改后的图片 plt.show() # 显示绘制的图像 2. 图像左半部分处理 from PIL import Image from skimage import io # 从名为'skimage'的库中导入'io'模块,这个模块通常用于读取图像 import matplotlib.pyplot as plt # image = Image.open('x.jpg')# 打开图片 image = image.convert('RGB') # 将图片转换为RGB模式 width, height = image.size # 获取图片的宽度和高度 for x in range(width // 2): # 遍历左半部分的像素 for y in range(height): r, g, b = image.getpixel((x, y)) # 如果像素接近黑色(可以根据需要调整阈值) if r < 50 and g < 50 and b < 50: # 将黑色像素改为红色 image.putpixel((x, y), (255, 0, 0)) plt.imshow(image) # 使用'imshow'函数显示图像 plt.show() # 显示绘制的图像 3. 图像左上四分之一处理 from PIL import Image from skimage import io # 从名为'skimage'的库中导入'io'模块,这个模块通常用于读取图像 import matplotlib.pyplot as plt # image = Image.open('x.jpg') # 打开图片 image = image.convert('RGB') # 将图片转换为RGB模式 width, height = image.size # 获取图片的宽度和高度 for x in range(width // 2): # 遍历左上四分之一的像素 for y in range(height // 2): r, g, b = image.getpixel((x, y)) # 如果像素接近黑色(可以根据需要调整阈值) if r < 50 and g < 50 and b < 50: # 将黑色像素改为红色 image.putpixel((x, y), (255, 0, 0)) plt.imshow(image) # 使用'imshow'函数显示图像 plt.show() # 显示绘制的图像 4. 小区域像素处理 import matplotlib.pyplot as plt import numpy as np from PIL import Image img = Image.open('x.jpg') # 打开图像文件 pixels = np.array(img) height, width, _ = pixels.shape# 获取图像的尺寸 center_x, center_y = width // 2, height // 2 # 将正中间的黑色像素改为红色,白色像素不变 for i in range(center_y - height // 4, center_y + height // 4): for j in range(center_x - width // 4, center_x + width // 4): if np.array_equal(pixels[i, j], [0, 0, 0]): # 黑色 pixels[i, j] = [255, 0, 0] # 红色 elif np.array_equal(pixels[i, j], [255, 255, 255]): # 白色 pixels[i, j] = [255, 255, 255] # 保持不变 img = Image.fromarray(pixels) # 转换回Image对象 plt.imshow(img) plt.show() 5. 指定区域像素处理 from PIL import Image from skimage import io # 从名为'skimage'的库中导入'io'模块,这个模块通常用于读取图像 import matplotlib.pyplot as plt # image = Image.open('x.jpg') # 打开图片 image = image.convert('RGB') # 将图片转换为RGB模式 width, height = image.size # 获取图片的宽度和高度 start_x = (width - 8) // 2 # 计算中间正方形的起始和结束坐标 start_y = (height - 8) // 2 end_x = start_x + 8 end_y = start_y + 8 for x in range(start_x, end_x): # 遍历中间正方形的像素 for y in range(start_y, end_y): r, g, b = image.getpixel((x, y)) # 如果像素接近黑色(可以根据需要调整阈值) if r < 50 and g < 50 and b < 50: # 将黑色像素改为绿色 image.putpixel((x, y), (0, 255, 0)) # 如果像素接近白色(可以根据需要调整阈值) elif r > 200 and g > 200 and b > 200: # 将白色像素改为黄色 image.putpixel((x, y), (255, 255, 0)) plt.imshow(image) # 使用'imshow'函数显示图像 plt.show() # 显示绘制的图像 6. 星系图像红色通道处理 from PIL import Image import matplotlib.pyplot as plt image = Image.open('galaxy-full.jpg') # 打开原始图像 r, g, b = image.split() # 分离图像的通道,得到R、G、B三个通道的图像 new_image = Image.merge("RGB", (r, Image.new("L", r.size, 0), Image.new("L", r.size, 0))) # 创建一个新的图像,只保留红色通道,绿色和蓝色通道设为0 plt.imshow(new_image) plt.show() 7. 星系图像绿色通道处理 from PIL import Image import matplotlib.pyplot as plt image = Image.open('galaxy-full.jpg') # 打开图像文件 r, g, b = image.split() # 分离图像的通道 green_image = Image.merge("RGB", (Image.new("L", g.size, 0), g, Image.new("L", g.size, 0))) # 创建仅包含绿色通道的图像,红色和蓝色通道设为0 plt.imshow(green_image) plt.show() 8. 星系图像蓝色通道处理 from PIL import Image import matplotlib.pyplot as plt img = Image.open('galaxy-full.jpg') # 打开图像 r, g, b = img.split()# 分离通道 blue_img = Image.merge('RGB', (Image.new('L', b.size, 0), Image.new('L', b.size, 0), b)) # 创建仅包含蓝色通道的图像,红色和绿色通道设为0 plt.imshow(blue_img) # 可视化蓝色通道图片 plt.show() 9. 条纹效果处理 import numpy as np from PIL import Image import matplotlib.pyplot as plt img = Image.open('earth.jpg') # 打开图像 img_array = np.array(img) # 将图像转换为numpy数组 height, width, _ = img_array.shape # 获取图像的高度和宽度 colors = [[0, 0, 255], [0, 255, 0], [255, 0, 0], [255, 255, 0], [0, 255, 255], [255, 0, 255]] # 定义颜色列表,包含蓝、绿、红等色调 for x in range(width): # 进行密集竖直条纹效果处理 color_index = x % len(colors) # 根据x坐标选择颜色 for y in range(height): # 以一定的透明度叠加条纹颜色,产生故障效果 alpha = 0.5 # 透明度,可调整 new_color = [int(img_array[y, x, i] * (1 - alpha) + colors[color_index][i] * alpha) for i in range(3)] img_array[y, x] = new_color new_img = Image.fromarray(img_array) # 将处理后的numpy数组转换回图像 plt.figure(figsize=(15, 15)) plt.imshow(new_img) plt.show() #### 使用方法 1. 环境准备: 安装依赖库、下载测试文件(`x.jpg`、`galaxy-full.jpg`、`earth.jpg`)并放置于代码同级目录。 2.调整参数: 修改颜色阈值(如`r < 50`改为`r < 100`)。 修改处理区域(如左半部分改为右半部分)。 调整条纹效果透明度或颜色列表。 #### 学习要点 1. 图像基础操作 像素遍历:掌握如何遍历图像的指定区域(如上半部分、左半部分、中心区域)。 颜色替换:理解RGB颜色模型,学会根据阈值替换特定颜色(如黑色→红色)。 区域裁剪:通过坐标计算实现局部区域处理(如中心8x8区域)。 2. 颜色空间与通道分离 RGB模型:理解红、绿、蓝三通道的分离与合并。 单通道可视化:学会提取并显示单一颜色通道(如仅显示红色通道)。 颜色空间转换:了解HSV/HSL等颜色空间的应用场景(如亮度判断)。 3. 图像处理优化 性能优化:对比逐像素操作与NumPy数组操作的效率差异。 向量化操作:使用NumPy实现批量像素处理,提升运行速度。 代码复用:封装通用函数(如区域处理、颜色替换)以提高代码可维护性。 4. 特效生成 故障艺术效果:通过颜色混合和透明度控制生成条纹故障效果。 动态参数调整:理解如何通过调整参数(如透明度、颜色列表)优化特效效果。 5. 工具与库的使用 Pillow库:掌握图像读取、转换、保存等基础操作。 Matplotlib:学会图像可视化与显示。 NumPy:理解数组操作在图像处理中的应用(如像素批量处理)。