1
以下程序显示“前景”完全黑色,而不是“帧”。我还检查了'frame'中的所有值都等于'foreground'中的值。 他们有相同的信道,数据类型等 我使用python 2.7.6和OpenCV版本2.4.8使用Python中的Opencv从图像中减去背景
import cv2 import numpy as np def subtractBackground(frame,background): foreground = np.absolute(frame - background) foreground = foreground >= 0 foreground = foreground.astype(int) foreground = foreground * frame cv2.imshow("foreground",foreground) return foreground def main(): cap = cv2.VideoCapture(0) dump,background = cap.read() while cap.isOpened(): dump,frame = cap.read() frameCopy = subtractBackground(frame,background) cv2.imshow('Live',frame) k = cv2.waitKey(10) if k == 32: break if __name__ == '__main__': main()