skimage.segmentation的mark_boundary如何画方框计算长宽
时间: 2023-10-01 13:03:11 浏览: 151
`skimage.segmentation.mark_boundary`函数是用来在图像上标记出分割边界的,它并不会画方框或计算长宽。如果您想要画方框并计算长宽,可以使用下面的代码:
```python
import numpy as np
import matplotlib.pyplot as plt
from skimage import segmentation
# 读入图像
image = plt.imread('image.png')
# 做分割
labels = segmentation.slic(image, compactness=30, n_segments=400)
# 画出边界
boundary = segmentation.mark_boundaries(image, labels, color=(1, 0, 0))
# 找到最小外接矩形的左上角坐标、宽度和高度
regions = segmentation.find_boundaries(labels)
nonzero_rows, nonzero_cols = np.nonzero(regions)
x, y = np.min(nonzero_cols), np.min(nonzero_rows)
width, height = np.max(nonzero_cols) - x, np.max(nonzero_rows) - y
# 在图片上画出最小外接矩形
plt.imshow(boundary)
plt.plot([x, x+width], [y, y], 'r', linewidth=2)
plt.plot([x+width, x+width], [y, y+height], 'r', linewidth=2)
plt.plot([x+width, x], [y+height, y+height], 'r', linewidth=2)
plt.plot([x, x], [y+height, y], 'r', linewidth=2)
plt.show()
# 输出长宽
print('width:', width)
print('height:', height)
```
这里使用了`skimage.segmentation.find_boundaries`函数找到分割边界,然后找到最小外接矩形的左上角坐标和宽度、高度。最后使用`matplotlib`画出最小外接矩形。
阅读全文
相关推荐
















