【发布时间】:2019-08-15 08:58:40
【问题描述】:
如何为具有任意形状掩码的 numpy 数组生成轴对齐边界框?
我知道 OpenCV 中的等价物是 minAreaRect。他们在 skimage 中是这样的吗?
【问题讨论】:
标签: python-3.x numpy opencv scikit-image
如何为具有任意形状掩码的 numpy 数组生成轴对齐边界框?
我知道 OpenCV 中的等价物是 minAreaRect。他们在 skimage 中是这样的吗?
【问题讨论】:
标签: python-3.x numpy opencv scikit-image
您可以使用以下示例代码: 示例图片:stars
1.读取图片:
from skimage.measure import find_contours
from skimage.io import imread
import matplotlib.pyplot as plt
from skimage.color import rgb2gray
orig_img = imread('resources/stars.jpg')
gray_img = rgb2gray(orig_img)
plt.imshow(gray_img,interpolation='nearest', cmap=plt.cm.gray)
2.查找计数:
contours = find_contours(gray_img, 0.8)
fig, ax = plt.subplots()
ax.imshow(gray_img, interpolation='nearest', cmap=plt.cm.gray)
for n, contour in enumerate(contours):
ax.plot(contours[n][:, 1], contours[n][:, 0], linewidth=2)
plt.show()
输出:countors 3.绘制边界框:
import numpy as np
from skimage.draw import polygon_perimeter
bounding_boxes = []
for contour in contours:
Xmin = np.min(contour[:,0])
Xmax = np.max(contour[:,0])
Ymin = np.min(contour[:,1])
Ymax = np.max(contour[:,1])
bounding_boxes.append([Xmin, Xmax, Ymin, Ymax])
with_boxes = np.copy(gray_img)
for box in bounding_boxes:
#[Xmin, Xmax, Ymin, Ymax]
r = [box[0],box[1],box[1],box[0], box[0]]
c = [box[3],box[3],box[2],box[2], box[3]]
rr, cc = polygon_perimeter(r, c, with_boxes.shape)
with_boxes[rr, cc] = 1 #set color white
plt.imshow(with_boxes, interpolation='nearest', cmap=plt.cm.gray)
plt.show()
【讨论】:
minAreaRect 了解详情。