Santino Santino - 2 months ago 13
Python Question

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: enter image description here


from skimage.segmentation import slic
from 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))

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)