ocsvm_segmentation.py 文件源码

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

项目:initialisation-problem 作者: georgedeath 项目源码 文件源码
def extract_RGB_LBP_features(image, labels, size=5, P=8, R=2):
    n_sp = np.max(labels)+1
    hs = size//2
    img_superpixel = np.zeros_like(labels, dtype='int')

    # calculate lbp for entire region
    lbp_img = np.empty((3, ), dtype='object')
    for d in range(3):
        lbp_img[d] = local_binary_pattern(image[..., d], P=P, R=R, method='uniform')

    feat_desc_size = P+1

    feat_descs = np.zeros((n_sp, feat_desc_size*3))

    for i in range(n_sp):
        # get centroid of i'th superpixel
        img_superpixel[:] = labels == i
        cy, cx = [np.rint(x).astype('int') for x in regionprops(img_superpixel)[0].centroid]

        # extract lbp values in sizeXsize region centred on the centroid
        x0, y0, x1, y1 = cx-hs, cy-hs, cx+hs+1, cy+hs+1

        # clip to boundaries of image
        x0 = 0 if x0 < 0 else x0
        y0 = 0 if y0 < 0 else y0
        x1 = image.shape[1]-1 if x1 > image.shape[1]-2 else x1
        y1 = image.shape[0]-1 if y1 > image.shape[0]-2 else y1

        # fill in the feature vector for each image channel
        for d in range(3):
            j, k = d*feat_desc_size, (1+d)*feat_desc_size
            patch = lbp_img[d][y0:y1, x0:x1].flat

            fv = np.histogram(patch, bins=np.arange(0, feat_desc_size+1), range=(0, feat_desc_size+1))[0]
            feat_descs[i, j:k] = fv

    return feat_descs
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号