简体   繁体   中英

Obtain box2d from superpixels segments

I am trying to use SLIC to obtain superpixels and get semantic segmentation of an image.

img = cv2.imread(img_name)
segments = slic(image, n_segments = numSegments, sigma = 3,convert2lab=True,max_iter=25)

How do I get the box2d for each of the segments? and if there a hierarchical tree of the segments how do I fetch that?

I did not read the original paper, but according to documentation it does not return a hierarchy.

I assume that you mean bounding boxes, so used the skimage example of Regionprops to get bounding boxes for each superpixel returned by SLIC.

Result: 在此处输入图片说明

Code:

from skimage.segmentation import slic
from skimage.data import astronaut

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

from skimage.measure import label
from skimage.measure import regionprops
from skimage.color import label2rgb

img = astronaut()
segments = slic(img, n_segments=50, compactness = 100)

image_label_overlay = label2rgb(segments, image=img)

fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
ax.imshow(image_label_overlay)

for region in regionprops(segments):

    minr, minc, maxr, maxc = region.bbox
    rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr,
                              fill=False, edgecolor='red', linewidth=2)
    ax.add_patch(rect)

plt.show()

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