Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a code which takes images from a folder, crops the region of interest around it using the ROI function, and then removes the background using the rembg library. But I want a border around that image, around that specific object itself, like the one we get in segmentation, except keeping the colours and the object intact. [NOT A RECTANGULAR BORDER]. Can anyone help and tell me how to do it?

Here is my code for reference:

import cv2
import numpy as np
import os
from os.path import join
from os import listdir
from PIL import Image
from rembg import remove


path = 'Some path'
folder = 'Some other path'
count = 1

def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA):
    dim = None
    (h, w) = image.shape[:2]

    if width is None and height is None:
        return image
    if width is None:
        r = height / float(h)
        dim = (int(w * r), height)
    else:
        r = width / float(w)
        dim = (width, int(h * r))

    resized = cv2.resize(image, dim, interpolation = inter)

    return resized


for filename in os.listdir(folder):
       img = cv2.imread(os.path.join(folder,filename))
       if img is not None: 
           img = image_resize(img, height = 600)
           roi = cv2.selectROI(img)
           print(roi)
           im_cropped = img[int(roi[1]):int(roi[1]+roi[3]),int(roi[0]):int(roi[0]+roi[2])]
           rs = str(count)
           rem = remove(im_cropped)
           cv2.imshow("Removed Image", rem)
           
           cv2.imwrite(os.path.join(path, rs + '.jpg'), rem)
           count = count + 1
           cv2.waitKey(0)


What I have tried:

This was raising a couple of errors

import geopandas as gpd
import shapely.ops
import shapely.geometry

gdf = gpd.GeoDataFrame.from_features(rem.jpg()).set_crs("epsg:4326")
           ax = gdf.plot()
           ls = shapely.geometry.LineString(shapely.ops.unary_union(gdf["geometry"]).exterior.coords)
           b = ls.bounds
           rem = gpd.GeoSeries(ls).plot(edgecolor="yellow", lw=5, ax=ax)
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900