# digital-image-processing **Repository Path**: zhangjia1234/digital-image-processing ## Basic Information - **Project Name**: digital-image-processing - **Description**: 1234567890 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-03-11 - **Last Updated**: 2025-04-11 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 数字图像处理 #### 实验1:图像基础操作 ##### 环境要求 - Python 3.x - scikit-image - matplotlib - PIL (Pillow) ##### 文件说明 - `assignment1_answer.ipynb`: 包含所有实验代码的Jupyter Notebook文件。 ##### 代码示例说明 1. **图像上半部分处理** ```python from skimage import io import matplotlib.pyplot as plt img = io.imread('x.jpg') h, w, c = img.shape for i in range(w): for j in range(h//2): img[j, i][0] = 255 plt.imshow(img) plt.show() ``` 2. **图像左半部分处理** ```python from skimage import io import matplotlib.pyplot as plt img = io.imread('x.jpg') h, w, c = img.shape for i in range(w//2): for j in range(h): img[j, i][0] = 255 plt.imshow(img) plt.show() ``` 3. **图像左上四分之一处理** ```python from skimage import io import matplotlib.pyplot as plt img = io.imread('x.jpg') h, w, c = img.shape for i in range(w//2): for j in range(h//2): img[j, i][0] = 255 plt.imshow(img) plt.show() ``` 4. **小区域像素处理** ```python from skimage import io import matplotlib.pyplot as plt img = io.imread('x.jpg') start_x, start_y = 2, 2 patch_width, patch_height = 6, 6 h, w, c = img.shape end_x = min(start_x + patch_width, w) end_y = min(start_y + patch_height, h) for i in range(start_x, end_x): for j in range(start_y, end_y): img[j, i][0] = 255 plt.imshow(img) plt.show() ``` 5. **指定区域颜色处理** ```python from skimage import io import matplotlib.pyplot as plt img = io.imread('x.jpg') start_x, start_y = 2, 2 patch_width, patch_height = 6, 6 h, w, c = img.shape end_x = min(start_x + patch_width, w) end_y = min(start_y + patch_height, h) for i in range(start_x, end_x): for j in range(start_y, end_y): r, g, b = img[j, i][0], img[j, i][1], img[j, i][2] if r == 0 and g == 0 and b == 0: img[j, i] = [255, 255, 0] else: img[j, i] = [0, 255, 0] plt.imshow(img) plt.show() ``` 6. **星系图像红色通道处理** ```python from PIL import Image import matplotlib.pyplot as plt image = Image.open('galaxy-full.jpg') r, g, b = image.split() r_image = Image.merge('RGB', (r, Image.new('L', r.size), Image.new('L', r.size))) plt.figure(figsize=(15, 5)) plt.imshow(r_image) plt.title('Red Channel') plt.show() ``` 7. **星系图像绿色通道处理** ```python from PIL import Image import matplotlib.pyplot as plt image = Image.open('galaxy-full.jpg') r, g, b = image.split() g_image = Image.merge('RGB', (Image.new('L', g.size), g, Image.new('L', g.size))) plt.figure(figsize=(5, 5)) plt.imshow(g_image) plt.title('Green Channel') plt.show() ``` 8. **星系图像蓝色通道处理** ```python from PIL import Image import matplotlib.pyplot as plt image = Image.open('galaxy-full.jpg') r, g, b = image.split() b_image = Image.merge('RGB', (Image.new('L', b.size), Image.new('L', b.size), b)) plt.figure(figsize=(15, 5)) plt.imshow(b_image) plt.title('Blue Channel') plt.show() ``` 9. **条纹效果处理** ```python from PIL import Image import numpy as np import matplotlib.pyplot as plt image = Image.open('earth.jpg') image_np = np.array(image) height, width, _ = image_np.shape new_image_np = image_np.copy() stripe_opacity = 0.3 for i in range(width): if i % 3 == 0: stripe = np.zeros((height, width, 3), dtype=np.uint8) stripe[:, :, 0] = image_np[:, :, 0] new_image_np[:, :, 0] = (1 - stripe_opacity) * new_image_np[:, :, 0] + stripe_opacity * stripe[:, :, 0] elif i % 3 == 1: stripe = np.zeros((height, width, 3), dtype=np.uint8) stripe[:, :, 1] = image_np[:, :, 1] new_image_np[:, :, 1] = (1 - stripe_opacity) * new_image_np[:, :, 1] + stripe_opacity * stripe[:, :, 1] else: stripe = np.zeros((height, width, 3), dtype=np.uint8) stripe[:, :, 2] = image_np[:, :, 2] new_image_np[:, :, 2] = (1 - stripe_opacity) * new_image_np[:, :, 2] + stripe_opacity * stripe[:, :, 2] processed_img = Image.fromarray(new_image_np.astype(np.uint8)) plt.figure(figsize=(10, 10)) plt.imshow(processed_img) plt.axis('off') plt.show() ``` ##### 使用方法 1. 安装所需的Python库: ```bash pip install scikit-image matplotlib pillow ``` 2. 下载并运行`assignment1_answer.ipynb`文件。 ##### 学习要点 - 图像的基本操作,如读取、显示和修改像素值。 - 使用不同的颜色通道进行图像处理。 - 处理图像的特定区域。 - 创建和显示图像的不同效果。 个人信息: 姓名:张嘉 学号:202352320229 班级:智能科学与技术2班