Ask Your Question

Revision history [back]

You can extract foreground and backgroung from original image using copyTo` :

import numpy as np
import cv2 as cv
cap = cv.VideoCapture(0)
fgbg = cv.createBackgroundSubtractorMOG2()
while(1):
    ret, frame = cap.read()
    fgmask = fgbg.apply(frame)
    cv.imshow('frame',fgmask)
    fg = cv.copyTo(frame,fgmask)
    cv.imshow('Foreground',fg)
    cv.imshow('Background',cv.copyTo(frame,cv.bitwise_not(fgmask)))
    k = cv.waitKey(30) & 0xff
    if k == 27:
        break
cap.release()

cv.destroyAllWindows()

You can extract foreground and backgroung from original image using copyTo` :

import numpy as np
import cv2 as cv
cap = cv.VideoCapture(0)
fgbg = cv.createBackgroundSubtractorMOG2()
while(1):
    ret, frame = cap.read()
    fgmask = fgbg.apply(frame)
    cv.imshow('frame',fgmask)
    fg = cv.copyTo(frame,fgmask)
    cv.imshow('Foreground',fg)
cv.imshow('Foreground', cv.copyTo(frame,fgmask))
    cv.imshow('Background',cv.copyTo(frame,cv.bitwise_not(fgmask)))
    k = cv.waitKey(30) & 0xff
    if k == 27:
        break
cap.release()

cv.destroyAllWindows()

You can extract foreground and backgroung from original image using copyTo` :

import numpy as np
import cv2 as cv
cap = cv.VideoCapture(0)
ret, frame = cap.read()
if ret is False:
    print("Cannot read video stream")
    exit()
myvideo=cv.VideoWriter("forground.avi", cv.VideoWriter_fourcc('M','J','P','G'), 30, (int(frame.shape[1]),int(frame.shape[0])))
fgbg = cv.createBackgroundSubtractorMOG2()
while(1):
    ret, frame = cap.read()
    if ret is False:
        print("Cannot read video stream")
        exit()    
    fgmask = fgbg.apply(frame)
    cv.imshow('frame',fgmask)
    cv.imshow('Foreground', cv.copyTo(frame,fgmask))
fg = cv.copyTo(frame,fgmask)
    myvideo.write(fg)
    cv.imshow('Foreground',fg)
    cv.imshow('Background',cv.copyTo(frame,cv.bitwise_not(fgmask)))
    k = cv.waitKey(30) & 0xff
    if k == 27:
        break
cap.release()
 myvideo.release()
cv.destroyAllWindows()

You can extract foreground and backgroung from original image using copyTo` :

import numpy as np
import cv2 as cv
cap = cv.VideoCapture(0)
ret, frame = cap.read()
if ret is False:
    print("Cannot read video stream")
    exit()
myvideo=cv.VideoWriter("forground.avi", cv.VideoWriter_fourcc('M','J','P','G'), 30, (int(frame.shape[1]),int(frame.shape[0])))
fgbg = cv.createBackgroundSubtractorMOG2()
while(1):
    ret, frame = cap.read()
    if ret is False:
        print("Cannot read video stream")
        exit()    
    fgmask = fgbg.apply(frame)
    cv.imshow('frame',fgmask)
    fg = cv.copyTo(frame,fgmask)
    myvideo.write(fg)
    cv.imshow('Foreground',fg)
    cv.imshow('Background',cv.copyTo(frame,cv.bitwise_not(fgmask)))
    k = cv.waitKey(30) & 0xff
    if k == 27:
        break
cap.release()
myvideo.release()
cv.destroyAllWindows()

My answer is good with opencv >=4.0 (you have to build opencv yourself) If opencv version is <4.0 you can use code given in this issue

You can extract foreground and backgroung from original image using copyTo` :

import numpy as np
import cv2 as cv
cap = cv.VideoCapture(0)
ret, frame = cap.read()
if ret is False:
    print("Cannot read video stream")
    exit()
myvideo=cv.VideoWriter("forground.avi", cv.VideoWriter_fourcc('M','J','P','G'), 30, (int(frame.shape[1]),int(frame.shape[0])))
fgbg = cv.createBackgroundSubtractorMOG2()
while(1):
    ret, frame = cap.read()
    if ret is False:
        print("Cannot read video stream")
        exit()    
    fgmask = fgbg.apply(frame)
    cv.imshow('frame',fgmask)
    fg = cv.copyTo(frame,fgmask)
    myvideo.write(fg)
    cv.imshow('Foreground',fg)
    cv.imshow('Background',cv.copyTo(frame,cv.bitwise_not(fgmask)))
    k = cv.waitKey(30) & 0xff
    if k == 27:
        break
cap.release()
myvideo.release()
cv.destroyAllWindows()

My answer is good with opencv >=4.0 (you have to build opencv yourself) If opencv version is <4.0 you can use code given in this issue for copyTo