![](/img/trans.png)
[英]Splitting region of interest iteratively with python and populating list of latitudes
[英]Adding a border around Region of Interest using python
我有一個代碼,它從一個文件夾中獲取圖像,使用 ROI function 裁剪它周圍的感興趣區域,然后使用 rembg 庫刪除背景。 但我想要圍繞該圖像的邊框,圍繞特定的 object 本身,就像我們在分割中得到的那樣,除了保持顏色和 object 完好無損。 [不是矩形邊框]。 任何人都可以幫助並告訴我該怎么做嗎?
這是我的代碼供參考:
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)
正如我假設你的意思是一個簡單的大綱,我建議如下:
import cv2
import numpy as np
# Threshold the image
thresh = img > threshold
# Use binary dilation to widen the area
thresh_dil = cv2.dilate(thresh, np.ones((3, 3)) , iterations=1)
# Get the outlines by substracting the dilated image with the original area
outlines = thresh_dil - thresh
# Superimpose the outlines on your original image
img_with_outlines = img.copy()
img_with_outlines[outlines > 0] = 255
這應該在您檢測到的 object 周圍畫一條白線。
注意:此方法適用於灰度圖像。 對於全彩色圖像,您可以將其單獨應用於每個通道。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.