如何为具有任意形状蒙版的numpy数组生成与轴对齐的边界框?
我知道OpenCV中的等效项是minAreaRect
。他们在skimage中是这样的吗?
最佳答案
您可以使用以下示例代码:
样本图片: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()
输出:
bounding box image
关于python-3.x - 轴对齐边界框Skimage,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57507234/