叠加图像质量随着实时视频馈送中的每一帧而降低



我正在尝试使用OpenCV制作一个过滤器,在实时视频捕获中,我将眼镜戴在眼睛上喂养我面临的问题是,视频馈送一开始覆盖眼镜图像的质量很好,但随着每一帧,眼镜的图像质量似乎会自行降低,眼镜的高度似乎会逐帧缓慢增加。

这是我的代码:-


mport cv2
face_Cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
eye_Cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "frontalEyes35x16.xml")
nose_Cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "Nose18x15.xml")
glasses = cv2.imread('glasses.png', -1) 
mustache = cv2.imread('mustache.png',-1)
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

if ret == False:
continue

frame = cv2.cvtColor(frame , cv2.COLOR_BGR2BGRA) # so that we can use glasses and mustaches alpha value
# otherwise we get white box around them

faces = face_Cascade.detectMultiScale(gray_frame, 1.3, 5)

for (x,y,w,h) in faces:

#cv2.rectangle(frame, (x,y), (x+w, y+h), (255,255,255),3)
roi_gray = gray_frame[y:y+h , x:x+w]
roi_color = frame[y:y+h , x:x+w]


eyes = eye_Cascade.detectMultiScale(roi_gray, 1.3, 5)
for (ex,ey,ew,eh) in eyes:
#cv2.rectangle(roi_color, (ex,ey), (ex+ew, ey+eh), (0,255,0),3)
roi_eye_gray = roi_gray[ey:ey+eh, ex:ex+ew]
roi_eye_color = roi_color[ey:ey+eh, ex:ex+ew]
glasses = cv2.resize(glasses, (ew,eh), interpolation = cv2.INTER_AREA)

gw, gh, gc = glasses.shape
# We are going to iterate through every single pixel value in the glasses image and then we
# are going to replace it with roi_color

for i in range (0,gw):
for j in range(0,gh):
if glasses[i, j][3] != 0: # 3rd value [3] means alpha value there is 0 so we want it  
#to  be transparent and we dont need to change that pixel value in roi_color
roi_color[ey + i, ex+ j ] = glasses[i , j]


#nose = nose_Cascade.detectMultiScale(roi_gray, 1.3, 5)
#for (nx,ny,nw,nh) in nose:
#cv2.rectangle(roi_color, (nx,ny), (nx+nw, ny+nh), (255,0,0),3)
#roi_nose_gray = roi_gray[ny:ny+nh , nx:nx+nw]
#roi_nose_color = roi_color[ny:ny+nh , nx:nx+nw]



cv2.imshow("Video Frame",frame)
frame = cv2.cvtColor(frame , cv2.COLOR_BGRA2BGR)

# Wait for user Input s, then you will stop the loop
key_pressed = cv2.waitKey(1) & 0xFF # for converting waitkey(32 bit) into 8 bit
if key_pressed == ord('s'):
break
cap.release()
cv2.destroyAllWindows()

发生在这一行:

glasses = cv2.resize(glasses, (ew,eh), interpolation = cv2.INTER_AREA)

因为你在每次迭代时都会上下调整眼镜的大小,覆盖原来的眼镜,所以同一副眼镜会变得更大,然后更小,然后更大。


相反,您应该从原始的高质量眼镜开始,而不是从上一帧中调整大小的眼镜开始。因此,在循环之外,更改这一行:

glasses = cv2.imread('glasses.png', -1) 

origGlasses = cv2.imread('glasses.png', -1) 

在循环内,更改此行:

glasses = cv2.resize(glasses, (ew,eh), interpolation = cv2.INTER_AREA)

至:

glasses = cv2.resize(origGlasses, (ew,eh), interpolation = cv2.INTER_AREA)

最新更新