gpt4 book ai didi

python - OpenCV 去除背景

转载 作者:太空狗 更新时间:2023-10-29 18:19:12 24 4
gpt4 key购买 nike

我正在尝试删除一些图像的背景,调整一些值并使用一些方法,如 morphologyEx 给我一个可接受的结果,但仍然存在一些漏洞,在最后一种情况下,这些漏洞没有' t fill even 在每个轮廓上迭代并用 -1 绘制它。我可以看到阈值图像非常好,用线条制作整个形状,但我不知道如何继续...

更新我更改了我的代码以便获得更好的结果,但我仍然遇到一些漏洞...如果我可以填补这些漏洞,脚本将是完美的。

def get_contrasted(image, type="dark", level=3):
maxIntensity = 255.0 # depends on dtype of image data
phi = 1
theta = 1

if type == "light":
newImage0 = (maxIntensity/phi)*(image/(maxIntensity/theta))**0.5
newImage0 = array(newImage0,dtype=uint8)
return newImage0
elif type == "dark":
newImage1 = (maxIntensity/phi)*(image/(maxIntensity/theta))**level
newImage1 = array(newImage1,dtype=uint8)

return newImage1

def sharp(image, level=3):
f = cv2.GaussianBlur(image, (level,level), level)
f = cv2.addWeighted(image, 1.5, f, -0.5, 0)
return f

original_image = imread('imagen.jpg')
# 1 Convert to gray & Normalize
gray_img = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY)
gray_img = sharp(get_contrasted(gray_img))
gray_img = normalize(gray_img, None, 0, 255, NORM_MINMAX, CV_8UC1)
imshow("Gray", gray_img)

# 2 Find Threshold
gray_blur = cv2.GaussianBlur(gray_img, (7, 7), 0)
adapt_thresh_im = cv2.adaptiveThreshold(gray_blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 1)
max_thresh, thresh_im = cv2.threshold(gray_img, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
thresh = cv2.bitwise_or(adapt_thresh_im, thresh_im)

# 3 Dilate
gray = cv2.Canny(thresh, 88, 400, apertureSize=3)
gray = cv2.dilate(gray, None, iterations=8)
gray = cv2.erode(gray, None, iterations=8)
imshow("Trheshold", gray)

# 4 Flood
contours, _ = cv2.findContours(gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contour_info = []
for c in contours:
contour_info.append((
c,
cv2.isContourConvex(c),
cv2.contourArea(c),
))
contour_info = sorted(contour_info, key=lambda c: c[2], reverse=True)
max_contour = contour_info[0]
holes = np.zeros(gray_img.shape, np.uint8)
drawContours(holes, max_contour, 0, 255, -1)
imshow("Holes", holes)

mask = cv2.GaussianBlur(holes, (15, 15), 0)
mask = np.dstack([mask] * 3) # Create 3-channel alpha mask

mask = mask.astype('float32') / 255.0 # Use float matrices,
img = original_image.astype('float32') / 255.0 # for easy blending
masked = (mask * img) + ((1 - mask) * (0,0,1)) # Blend
masked = (masked * 255).astype('uint8')

imshow("Maked", masked)
waitKey()

0 原创

enter image description here

1 个阈值

enter image description here

2 个洞

enter image description here

3 最终图像

enter image description here

最佳答案

因为我正在处理同样的问题,并在 Python 中找到了一个解决方案(使用 opencv2),所以我也想在这里分享这个。希望对您有所帮助。

import numpy as np
import cv2

cv2.namedWindow('image', cv2.WINDOW_NORMAL)

#Load the Image
imgo = cv2.imread('koAl2.jpg')
height, width = imgo.shape[:2]

#Create a mask holder
mask = np.zeros(imgo.shape[:2],np.uint8)

#Grab Cut the object
bgdModel = np.zeros((1,65),np.float64)
fgdModel = np.zeros((1,65),np.float64)

#Hard Coding the Rect The object must lie within this rect.
rect = (10,10,width-30,height-30)
cv2.grabCut(imgo,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)
mask = np.where((mask==2)|(mask==0),0,1).astype('uint8')
img1 = imgo*mask[:,:,np.newaxis]

#Get the background
background = imgo - img1

#Change all pixels in the background that are not black to white
background[np.where((background > [0,0,0]).all(axis = 2))] = [255,255,255]

#Add the background and the image
final = background + img1

#To be done - Smoothening the edges

cv2.imshow('image', final )

k = cv2.waitKey(0)

if k==27:
cv2.destroyAllWindows()

关于python - OpenCV 去除背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31133903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com