【问题标题】:Axis Aligned Bounding Box Skimage轴对齐边界框 Skimage
【发布时间】:2019-08-15 08:58:40
【问题描述】:

如何为具有任意形状掩码的 numpy 数组生成轴对齐边界框?

我知道 OpenCV 中的等价物是 minAreaRect。他们在 skimage 中是这样的吗?

【问题讨论】:

    标签: python-3.x numpy opencv scikit-image


    【解决方案1】:

    您可以使用以下示例代码: 示例图片: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

    【讨论】:

    • 你的回答肯定会产生图像吗?请考虑添加图像以使答案对其他人更有用。谢谢。
    • 您确实会生成边界框,但它们不是轴对齐的。 opencv minAreaRect 了解详情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-07
    • 2021-05-03
    相关资源
    最近更新 更多