python - 实时视频源中的叠加图像质量随每一帧而下降

标签 python opencv

我正在尝试使用 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)

关于python - 实时视频源中的叠加图像质量随每一帧而下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63279093/

相关文章:

python - 如何列出静态目录中的文件?

python - 如何将包含模块的目录导入到Python路径,该路径是从子文件夹到原始源的符号链接(symbolic link)?

python - python集合包中Counter使用的算法?

Android光流与opencv

c++ - 来自窗口截图的模板匹配

python - 用于使用一组匹配多个子字符串的正则表达式?

python - Matplotlib - x 轴与日期时间之间的间隔不均匀

windows - OpenCV Windows 安装 : can not open file "OpenCV-2.4.5.exe" as archive

java - 如何使用 OpenCV 访问两个图像中的每个像素并找到它们的绝对差异? (安卓工作室)

基于 OpenCV 的迷宫迷宫求解器