plt.subplot(221),plt.imshow(img,'gray')
时间: 2024-02-24 18:51:36 浏览: 24
plt.subplot(221),plt.imshow(img,'gray')的作用是在一个2x2的网格中创建一个子图,并在该子图中显示灰度图像img。
这段代码的具体功能如下:
- plt.subplot(221):创建一个2x2的网格,并选择第1个子图。
- plt.imshow(img,'gray'):在选定的子图中显示灰度图像img。
这段代码的效果是将灰度图像img显示在一个2x2的网格的第1个子图中。
相关问题
补充代码img_scanned = io.imread("scanned-text-grayscale.tif") plt.subplot(121) plt.imshow(img_scanned) plt.axis('off') plt.title("Original") img_seg = np.zeros_like(img_scanned) ### YOUR CODE HERE ### END YOUR CODE plt.subplot(122) plt.imshow(img_seg) plt.axis('off') plt.title("Segmentation")
好的,这里是完整的代码,包括对图像进行分割的部分:
```python
from skimage import io
import matplotlib.pyplot as plt
import numpy as np
# 读取灰度图像
img_scanned = io.imread("scanned-text-grayscale.tif")
# 显示原始图像
plt.subplot(121)
plt.imshow(img_scanned)
plt.axis('off')
plt.title("Original")
# 图像分割
img_seg = np.zeros_like(img_scanned)
threshold = 128
img_seg[img_scanned < threshold] = 1
# 显示分割后的图像
plt.subplot(122)
plt.imshow(img_seg)
plt.axis('off')
plt.title("Segmentation")
plt.show()
```
其中,`img_seg` 是一个与 `img_scanned` 大小相同的全零数组,然后将阈值以下的像素点赋值为 1,表示将其分为前景。
代码补充b)图像对B1、B2 img1 = np.zeros((100, 100)) img2 = np.zeros((100, 100)) img1[30:70, 30:70] = 1 img2[20:40, 30:70] = 1 img2[60:80, 30:70] = 1 plt.subplot(121) plt.imshow(img1) plt.axis('off') plt.subplot(122) plt.imshow(img2) plt.axis('off') plt.show()
好的,这里是代码补充,完整代码如下:
```python
import numpy as np
import matplotlib.pyplot as plt
# 图像 B1
img1 = np.zeros((100, 100))
img1[30:70, 30:70] = 1
# 图像 B2
img2 = np.zeros((100, 100))
img2[20:40, 30:70] = 1
img2[60:80, 30:70] = 1
# 显示图像
plt.subplot(121)
plt.imshow(img1)
plt.axis('off')
plt.subplot(122)
plt.imshow(img2)
plt.axis('off')
plt.show()
```
其中,`np.zeros((100, 100))` 创建了一个大小为 $100 \times 100$ 的全黑图像,然后通过对数组的赋值操作来绘制出需要的图案。`plt.subplot(121)` 和 `plt.subplot(122)` 将整个画布分成两个区域,分别显示两张图像。`plt.imshow()` 用于显示图像,`plt.axis('off')` 用于隐藏坐标轴。最后,`plt.show()` 显示出整个画布。