# 图像基础处理 **Repository Path**: zealous-ox/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-15 - **Last Updated**: 2025-03-15 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 数字图像处理实验一:图像基础操作README ## 一、环境要求 本实验代码运行依赖于以下Python库: - `OpenCV`(`cv2`):用于图像的读取、处理和颜色空间转换等操作。安装方式:在命令行中执行`pip install opencv-python`。 - `NumPy`(`np`):提供高效的数值计算功能,在创建掩码、处理图像数据时发挥重要作用。安装方式:`pip install numpy`。 - `Matplotlib`:用于图像的显示。安装方式:`pip install matplotlib`。 - `Pillow`(`PIL`):提供了丰富的图像操作功能,如打开图像、绘制条纹等。安装方式:`pip install Pillow`。 ## 二、文件说明 `app.py`为核心代码文件,实现了多种图像基础处理操作,包括图像特定区域处理、颜色通道分离与显示、添加条纹效果等。实验过程中需准备相应的图像文件,如`x.jpg`、`galaxy-full.jpg`、`earth.jpg`等,这些图像文件应与`app.py`位于同一目录下,否则需修改代码中的文件路径。 ## 三、代码示例说明 ### 1. 图像上半部分处理 ```python img = cv2.imread('x.jpg') height, width = img.shape[:2] upper_half = img[:height//2, :] black_mask = np.all((upper_half <= [30, 30, 30]), axis=2) img[:height//2][black_mask] = [0, 0, 255] img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) plt.axis('off') plt.imshow(img_rgb) plt.show() ``` - 读取`x.jpg`图像,获取图像的高度和宽度。 - 提取图像的上半部分。 - 创建黑色掩码,通过设定像素值小于等于`[30, 30, 30]`来识别黑色区域。 - 将上半部分中的黑色区域颜色修改为红色`[0, 0, 255]`。 - 将图像从BGR颜色空间转换为RGB颜色空间,以便使用`matplotlib`显示图像。 ### 2. 图像左半部分处理 ```python img = cv2.imread('x.jpg') height, width = img.shape[:2] left_half = img[:, :width//2] black_mask = np.all((left_half <= [30, 30, 30]), axis=2) img[:, :width//2][black_mask] = [0, 0, 255] img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) plt.axis('off') plt.imshow(img_rgb) plt.show() ``` - 读取`x.jpg`图像,获取图像尺寸。 - 提取图像的左半部分。 - 创建黑色掩码识别左半部分的黑色区域。 - 将左半部分的黑色区域颜色修改为红色。 - 转换颜色空间并显示图像。 ### 3. 图像左上四分之一处理 ```python img = cv2.imread('x.jpg') height, width = img.shape[:2] quarter = img[:height//2, :width//2] black_mask = np.all((quarter <= [30, 30, 30]), axis=2) img[:height//2, :width//2][black_mask] = [0, 0, 255] img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) plt.axis('off') plt.imshow(img_rgb) plt.show() ``` - 读取`x.jpg`图像,获取图像高度和宽度。 - 提取图像的左上四分之一部分。 - 创建黑色掩码识别左上四分之一部分的黑色区域。 - 将该区域的黑色部分颜色修改为红色。 - 转换颜色空间并显示图像。 ### 4. 小区域像素处理 ```python img = cv2.imread('x.jpg') height, width = img.shape[:2] center_y = height // 2 center_x = width // 2 start_y = center_y - 2 end_y = center_y + 2 start_x = center_x - 2 end_x = center_x + 2 center_region = img[start_y:end_y, start_x:end_x] black_mask = np.all((center_region <= [30, 30, 30]), axis=2) img[start_y:end_y, start_x:end_x][black_mask] = [0, 0, 255] img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) plt.axis('off') plt.imshow(img_rgb) plt.show() ``` - 读取`x.jpg`图像,获取图像尺寸,计算图像中心点坐标。 - 定义以中心点为中心的4x4区域。 - 创建黑色掩码识别该区域内的黑色像素。 - 将黑色像素颜色修改为红色。 - 转换颜色空间并显示图像。 ### 5. 指定区域颜色处理 ```python img = cv2.imread('x.jpg') height, width = img.shape[:2] center_y = height // 2 center_x = width // 2 start_y = center_y - 4 end_y = center_y + 4 start_x = center_x - 4 end_x = center_x + 4 center_region = img[start_y:end_y, start_x:end_x] black_mask = np.all((center_region <= [30, 30, 30]), axis=2) white_mask = np.all((center_region >= [225, 225, 225]), axis=2) img[start_y:end_y, start_x:end_x][black_mask] = [0, 255, 0] img[start_y:end_y, start_x:end_x][white_mask] = [0, 255, 255] img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) plt.axis('off') plt.imshow(img_rgb) plt.show() ``` - 读取`x.jpg`图像,获取图像尺寸,确定中心点坐标。 - 定义以中心点为中心的8x8区域。 - 创建黑色掩码和白色掩码,分别识别区域内的黑色和白色像素。 - 将黑色像素颜色修改为绿色`[0, 255, 0]`,白色像素颜色修改为黄色`[0, 255, 255]`。 - 转换颜色空间并显示图像。 ### 6 - 8. 星系图像颜色通道处理 ```python image = cv2.imread('galaxy-full.jpg') image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) b_channel, g_channel, r_channel = cv2.split(image_rgb) red_image = cv2.merge([r_channel, np.zeros_like(g_channel), np.zeros_like(b_channel)]) green_image = cv2.merge([np.zeros_like(r_channel), g_channel, np.zeros_like(b_channel)]) blue_image = cv2.merge([np.zeros_like(r_channel), np.zeros_like(g_channel), b_channel]) plt.figure(figsize=(8, 6)) plt.imshow(red_image) plt.title('红色通道图') plt.axis('off') plt.show() plt.figure(figsize=(8, 6)) plt.imshow(green_image) plt.title('绿色通道图') plt.axis('off') plt.show() plt.figure(figsize=(8, 6)) plt.imshow(blue_image) plt.title('蓝色通道图') plt.axis('off') plt.show() ``` - 读取`galaxy-full.jpg`图像,将其从BGR颜色空间转换为RGB颜色空间。 - 分离图像的红色、绿色和蓝色通道。 - 分别创建只包含单一颜色通道的图像。 - 使用`matplotlib`分别显示红色、绿色和蓝色通道图。 ### 9. 条纹效果处理 ```python from PIL import Image, ImageDraw def add_stripes(image_path, stripe_width=2): img = Image.open(image_path) draw = ImageDraw.Draw(img) width, height = img.size for x in range(0, width, stripe_width): draw.line((x, 0, x, height), fill=(255, 255, 255)) return img input_image_path = 'earth.jpg' try: output_image = add_stripes(input_image_path) output_image.show() except FileNotFoundError: print(f"未找到 {input_image_path} 文件,请检查文件路径和文件名。") except Exception as e: print(f"发生了未知错误: {e}") ``` - 定义`add_stripes`函数,用于在图像上添加垂直条纹。 - 打开`earth.jpg`图像,获取图像尺寸。 - 使用`ImageDraw`在图像上绘制白色垂直条纹。 - 显示添加条纹后的图像,若文件未找到或发生其他错误,会给出相应提示。 ## 四、使用方法 1. 确保已按照“环境要求”部分安装好所需的Python库。 2. 将`app.py`文件与实验所需的图像文件(`x.jpg`、`galaxy-full.jpg`、`earth.jpg`等)放置在同一目录下。 3. 在命令行中进入存放`app.py`的目录,执行`python app.py`命令,即可依次看到各个图像处理操作的结果展示。 ## 五、学习要点 1. 掌握使用`OpenCV`读取、处理和显示图像的基本方法,包括颜色空间转换、区域提取等操作。 2. 理解`NumPy`在图像处理中的作用,学会使用`NumPy`创建掩码,对图像像素进行筛选和修改。 3. 熟悉`Matplotlib`库的图像显示功能,以及如何根据需求调整图像显示的样式。 4. 了解`Pillow`库中`Image`和`ImageDraw`模块的使用,掌握在图像上绘制图形(如条纹)的方法。