简体   繁体   中英

How to mask or crop an image region after applying connected components

I have an RGB image (NxMx3 ndarray) having various regions. After applying clustering and connected components ( skimage measure.label ), I got an NxM ndarray with labeled regions.

How can I use these labeled areas for cropping or masking pixels from the original image (NxMx3)? In the end I want to take only the background (with label=0) and to extract some average color out of it.

Assuming image is your source image labels is your label image, you can grab the pixels corresponding to label j with:

pixels_j = image[labels == j]

This will give you an (Nj, 3) array of those pixels. You can then recover the mean color with:

mean_color = np.mean(pixels_j, axis=0)

If you are going to do this for many images, though, you should probably use skimage.measure.regionprops .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM