gpt4 book ai didi

python - 用opencv python删除背景

转载 作者:行者123 更新时间:2023-12-02 17:31:40 24 4
gpt4 key购买 nike

这是我获取二进制图像的解决方案:

import cv2
import numpy as np

img = cv2.imread('crop.png')

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)
ok,thresh = cv2.threshold(blur,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

cv2.imshow('image',img)
cv2.imshow('threshold',thresh)

k = cv2.waitKey(0) & 0xff
if k == ord('q'):
cv2.destroyAllWindows()

下面是我得到的结果。如何从手上去除背景?

original image
threshold image

最佳答案

您可以使用颜色检测来获取手部区域的蒙版。如果要对视频进行背景减法,则可以通过存储背景并从背景中减去即将到来的帧来实现。

import cv2
cap=cv2.VideoCapture(1)
j=0
while 1:
ret,frame=cap.read()
if(j==0):
bg=frame.copy().astype("float")
if(j<30):
cv2.accumulateWeighted(frame,bg,0.5)
j=j+1
diff=cv2.absdiff(frame,bg.astype("uint8"))
diff=cv2.cvtColor(diff,cv2.COLOR_BGR2GRAY)
thre,diff=cv2.threshold(diff,25,255,cv2.THRESH_BINARY)
cv2.imshow("j",diff)
if(cv2.waitKey(1) & 0XFF==ord('q')):
break
cap.release()
cv2.destroyAllWindows()

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

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