util.py 文件源码

python
阅读 17 收藏 0 点赞 0 评论 0

项目:initialisation-problem 作者: georgedeath 项目源码 文件源码
def superpixel_image(image, bbox, Nsp_min=100, Nsp_max=500, Nsp_npx=50):
    """
    Superpixels an image using slic0 and labels them 1 if they are 100%
    inside the bounding box, 0 otherwise.

    Arguments:
        image = MxNxD
        bbox = 
        Nsp_npx = number of pixels per superpixel (on avg) we're aiming for
        Nsp_min = (approx) min number of superpixels in cropped region
        Nsp_max = (approx) max number of superpixels in cropped region

    Output:
        segments = MxN label image where each pixel's value represents the 
                   superpixel it belongs to.
        sp_label = boolean vector containing segments.max()+1 entries 
                   corresponding to the label of the superpixel, with 1
                   indicating 100% inside the bounding box, 0 otherwise.
    """
    bbox_aa = bbox_to_axis_aligned_bbox(bbox)

    # calculate number of superpixels to aim for
    n_sp = np.rint(bbox_aa[2] * bbox_aa[3] / Nsp_npx).astype('int')
    n_sp = np.max([Nsp_min, np.min([Nsp_max, n_sp])])

    # segment the image using SLIC0
    segments = slic(image, n_segments=n_sp, slic_zero=True, 
                    enforce_connectivity=True)
    n_sp = segments.max()+1 # actual number of superpixels

    # create mask for outside of bounding box
    bbox_mask = np.zeros(image.shape[:2], dtype='bool')
    x, y = polygon(bbox[::2], bbox[1::2])
    bbox_mask[y, x] = True

    # label superpixels - 0 = 100% outside bbox, 1 = some overlap with bbox
    sp_label = np.zeros((n_sp), dtype='bool') 

    for n in range(n_sp):
        # label n'th sp as inside bbox if any of its pixels overlap the bbox
        if np.any((segments == n) & bbox_mask):
            sp_label[n] = True

    return segments, sp_label
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号