I am trying to isolate part of an image using connected component labeling. I am using scipy's ndimage.label on this image: enter image description here
for some reason the result is this: enter image description here
The part of the image that actually interests me is the white triangle in the upper right corner, which for some reason is labelled as background. What am I doing wrong?
The code I'm using is:
import numpy as np
from scipy import misc
from scipy import ndimage
import matplotlib.pyplot as plt
from skimage.filters import threshold_otsu
from skimage.measure import label
from skimage.morphology import closing, square
from skimage.color import label2rgb
from scipy.ndimage.measurements import label
im = misc.imread('20588046_024ee3569b2605dc_MG_R_ML_ANON.jpg')
med_im = ndimage.median_filter(im1, 3)
bw = closing(med_im > 170, square(3))
fill_bw = ndimage.binary_fill_holes(bw)
s = [[1, 1, 1],
[1, 1, 1],
[1, 1, 1]]
label_im, nb_labels = ndimage.label(fill_bw, structure=s)
sizes = ndimage.sum(fill_bw, label_im, range(nb_labels + 1))
mask_size = sizes < 1000
remove_pixel = mask_size[label_im]
label_im[remove_pixel] = 0
plt.imshow(label_im, cmap=plt.cm.spectral)
plt.show()
I've also tried using it without the structure, and I'm getting the same result. I am new to python so I apologiza if the code is a little messy