酷代码 AI
菜单
服务商

用NumPy、SKImage和Matplotlib:从灰蓝图到红黄图再到二值图的图像变换

```python import numpy as np from skimage import io, color import matplotlib.pyplot as plt # 1. 生成一个600行,600列,为灰色([127,127,127]),中间300行,300列为蓝色的图。 image = np.ones((600, 600, 3), dtype=np.uint8) * 127 image[150:450, 150:450] = [0, 0, 255] # 2. 将生成的图灰色变为黄色,蓝色变为红色 image[image == [127, 127, 127]] = [255, 255, 0] image[image == [0, 0, 255]] = [255, 0, 0] # 3. 将改变后的图二值化,中间为白,周围为黑(彩转灰度图,公式自选) gray_image = color.rgb2gray(image) binary_image = (gray_image > 0.5).astype(np.uint8) * 255 # 显示图像 plt.imshow(binary_image, cmap='gray') plt.show() ``` ######[AI写代码神器 | 320点数解答 | 2024-10-17 10:33:15]

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