Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

python - Filtering a labeled image by particle area

I have a labeled image of detected particles and a dataframe with the corresponding area of each labeled particle. What I want to do is filter out every particle on the image with an area smaller than a specified value.

I got it working with the example below, but I know there must be a smarter and especially faster way. For example skipping the loop by comparing the image with the array.

Thanks for your help!

Example:

labels = df["label"][df.area > 5000].to_numpy()
mask = np.zeros(labeled_image.shape)
for label in labels:
    mask[labeled_image == label] = 1

Dataframe:

      label  centroid-0  centroid-1  area
0         1          15        3681   191
1         2          13        1345   390
2         3          43        3746   885
3         4          32        3616   817
4         5          20        4250   137
    ...         ...         ...   ...
3827   3828        4149        1620   130
3828   3829        4151         852    62
3829   3830        4155         330   236
3830   3831        4157         530   377
3831   3832        4159        3975    81
question from:https://stackoverflow.com/questions/65599349/filtering-a-labeled-image-by-particle-area

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use isin to check equality to several labels. The resulting boolean array can be directly used as the mask after casting to the required type (e.g. int):

labels = df.loc[df.area.gt(5000), 'label']
mask = np.isin(labeled_image, labels).astype(int)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...