Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove some connected components in python?

Tags:

python

ndimage

Consider a binarized image, I use scipy.ndimage.label() on it and then apply the find_objects() on the result.

Now I've got a tuple list consists of N tuples, each of them is two slice, like:

index  value
 0   (slice(0, 21, None) slice(0, 12, None)) 
 1   (slice(0, 42, None) slice(7, 31, None))
 .   (...., ....)

which describes a x-y boundary coordinate due to the connected component.

Take index 0 tuple as an example:

slice(0, 21, None) means the row number is from 0~21 and

slice(0, 12, None) means the column number is from 0~12.

So we will know that this cc's area is 21 * 12 = 252.

Now I wanna remove those connected components whose area are smaller than 300.

I already know to do this iterating over all of them. I would like to do it in a more efficient way; does anyone know how to do it?

like image 388
Lancelod Liu Avatar asked Mar 06 '26 14:03

Lancelod Liu


1 Answers

Use this code will solve my problem:

def CC(Map):
    label_img, cc_num = ndimage.label(Map)
    CC = ndimage.find_objects(label_img)
    cc_areas = ndimage.sum(Map, label_img, range(cc_num+1))
    area_mask = (cc_areas < 1500)
    label_img[area_mask[label_img]] = 0
return label_img, CC

area_mask[label_img] is the most important mask trick in my opnion.

like image 57
Lancelod Liu Avatar answered Mar 08 '26 20:03

Lancelod Liu