Python Opencv2 : Background appearing on a no background image after adding it on webcam frame

标签 python opencv computer-vision opencv-python

我正在使用 opencv2 编写一个 Python 脚本,该脚本采用我的网络摄像头框架 (img) 并在我的脸上添加眼镜,但眼镜图像以某种方式发生了变化,我不明白为什么。
这是添加两个图像的代码片段:

 # Load two images  
 ret, img = cap.read()      
 img2 = cv2.imread('glasses_2.png', -1)
 img2 = img2[:,:,0:3]
 img2 = cv2.resize(img2, (glasses_width, glasses_height)) 
 rows,cols,channels = img2.shape 
 for i in range(0, rows):
     for j in range(0, cols):
         if img2[i, j][2] != 0:
             img[round(glasses_center_y - glasses_height/2) + i, round(glasses_center_x - glasses_width/2) + j] = img2[i, j]

这是我要展示的玻璃
This is the glasses that i want to display

这是实际的结果

enter image description here

谁能看出问题出在哪里? A搜索了很多,没有发现任何有用的东西。它与颜色 channel 有关吗?
谢谢

最佳答案

尝试将您的 if 语句更改为等于零而不是不等于。

关于Python Opencv2 : Background appearing on a no background image after adding it on webcam frame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51470948/

相关文章:

python - 在Python中查找0-100之间的值范围内的索引

python - 删除 Pandas 系列中的行并清理索引

python - Plotly Python 中的刻度格式

python - 如何使用python包装器在opencv中正确使用网络摄像头?

python - ValueError:层激活_1调用的输入不是符号张量

opencv - 如何计算平移矩阵?

MATLAB:从二进制图像中分割单个字母

python - 在 Django 中,如何在没有显式查询的情况下从多对多关系的额外字段中检索数据?

c++ - QT creator添加opencv库报错: linker command failed with exit code 1

python - TensorFlow Sigmoid 交叉熵与一维数据的 Logits