酷代码 AI
菜单

1. 生成一个600行,600列,背景为灰色([127,127,127]),中间300行,300列为蓝色的图。 2. 将生成的图灰色变为黄色,蓝色变为红色 3. 将改变后的图二值化,中间为白,周围为黑(彩**转灰度图,公式自选) 用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() ``` [2024-10-17 10:32:40 | AI写代码神器 | 685点数解答]

服务商
相关提问
发财导航,免费问AI
实用工具查看更多