酷代码 AI
菜单
服务商
DeepSeek字节豆包讯飞星火
更多选项

用Numpy、Skimage和Matplotlib:从灰蓝图到黄红图再到二值图的图像变换秘籍

```python import numpy as np from skimage import io from skimage.color import rgb2gray import matplotlib.pyplot as plt # Generate image with grey background and blue square in the middle image = np.full((600, 600, 3), [127, 127, 127], dtype=np.uint8) image[150:450, 150:450] = [0, 0, 255] # Change colors to yellow and red yellow_mask = (image[..., 0] == 127) & (image[..., 1] == 127) & (image[..., 2] == 127) image[yellow_mask] = [255, 255, 0] red_mask = (image[..., 0] == 0) & (image[..., 1] == 0) & (image[..., 2] == 255) image[red_mask] = [255, 0, 0] # Convert image to grayscale gray_image = rgb2gray(image) # Binarize image threshold = 0.5 binary_image = np.where(gray_image >= threshold, 255, 0).astype(np.uint8) # Display the images fig, axs = plt.subplots(1, 3, figsize=(15, 5)) axs[0].imshow(image) axs[0].set_title('Original Image') axs[1].imshow(gray_image, cmap='gray') axs[1].set_title('Gray Image') axs[2].imshow(binary_image, cmap='binary') axs[2].set_title('Binary Image') plt.show() ``` ######[AI写代码神器 | 685点数解答 | 2024-10-17 10:32:40]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]