library(OpenImageR)
#-----------
# read image
#-----------
path = system.file("tmp_images", "slic_im.png", package = "OpenImageR")
im = readImage(path)
im = im[,, 1:3]
#--------------------
# compute superpixels
#--------------------
res = superpixels(input_image = im, method = "slic", superpixel = 200,
compactness = 20, return_labels = TRUE)
#-------------------------
# compute the bounding box
#-------------------------
bbox = superpixel_bbox(res$labels, non_overlapping_superpixels = FALSE)
#-------------------------------------------
# plot the bounding boxes of the superpixels ( for illustration purposes )
#-------------------------------------------
graphics::plot(1:ncol(im), type='n', xlim = c(ncol(im), 1), ylim = c(1, nrow(im)))
graphics::rasterImage( flipImage(im), 1, 1, ncol(im), nrow(im))
for (i in 1:nrow(bbox)) {
# the order of the bounding box is c('xmin', 'ymin', 'xmax', 'ymax')
graphics::rect(bbox[i,3], bbox[i,1], bbox[i,4], bbox[i,2], border = "red", lwd = 2)
}
Run the code above in your browser using DataLab