0

画像を前処理したい

生画像

内側の長方形のセグメントのみが保持されるようにします (つまり、周囲の背景を削除します)。しかし、次のように示された正しい結果が得られません

トリミングされた画像.

私のため。

コードは非常に単純です。

def labelim(img):
    #labeling image
    gray = rgb2gray(img) #translate rgb to gray
    val = filters.threshold_local(gray,5)
    mask = gray > val
    clean_border = segmentation.clear_border(mask)
    labeled = label(clean_border)
    signle_labeled = np.where(labeled == 0,labeled, 1)#ensure all assigned label return as 1.
    return single_labeled
def crop_img(img, labeled):    
    cropped_images = []
    pad = 20
    for region in regionprops(labeled):
        if region.area < 2000:
            continue
        minr,minc,maxr,maxc = region.bbox
        cropped_images.append(gray[minr-pad:maxr+pad, minc-pad:maxc+pad])
    for c, cropped_image in enumerate(cropped_images):
        cropim = cropped_image
    return cropim

labeled = labelim(img)
cropped_image = crop_img(img, labeled)

テスト コードは別の画像では機能しますが、ほとんどの画像では機能しません。ヘルプ/提案をありがとう。

4

1 に答える 1