Ask Your Question
0

How to add background in webcam using python and opencv

asked 2017-10-21 05:07:51 -0600

opencvproblems gravatar image

updated 2017-10-21 05:46:14 -0600

I am trying to add video as background when (like this video https://www.youtube.com/watch?v=G_AVZ... am connecting to webcam. For this i did Background extraction first in this code i folow these steps

  • First it will read the data from videos by using "VideoCapture"
  • Next create and update the background model by using "createBackgroundSubtractorMOG2()" class because i am using opencv3
  • Finally it will get and show the foreground mask by using "imshow"
  • Then i added the background to the webcam
  • It is not capturing frames in color In this the video is added as background but the webcam captures the frames in gray.

How to capture the frames in color and i am attaching my code and i tried using convert from grayscale to RGB but it is not working.

I am getting error like

\color.cpp:10655: error: (-215) scn == 1 && (dcn == 3 || dcn == 4) in function cv::cvtColor And here is my code

import cv2 
import numpy as np

cap=cv2.VideoCapture(0)

fgbg=cv2.createBackgroundSubtractorMOG2()
history = 100

background_capture = 
cv2.VideoCapture('/house.mp4')

while True:

frame = get_frame(cap, 0.5)

ret, background = background_capture.read()
background=cv2.resize(background, (640,480), interpolation = 
cv2.INTER_AREA)
ret, frame = cap.read()
   # gray=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
mask=fgbg.apply(frame,learningRate=1.0//history)
#convert from grayscale to RGB
gray=cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)


bitwise= cv2.bitwise_and(background,background, mask = mask)
gaussian = cv2.GaussianBlur(bitwise, (5, 5), 10)
add=cv2.add(background,gaussian)
add=cv2.resize(add, (1366,768), interpolation = cv2.INTER_AREA)



cv2.imshow('add',add)

k=cv2.waitKey(30) & 0xff
if k==27:
   break

cap.release()
cv2.destroyAllWindows()
edit retag flag offensive close merge delete

Comments

i have no idea, why your webcam is showing grayscale (like) images only, but the error hints at it giving you 3channel (all identical) images, not single channel ones, thus your cvtColor conversion fails.

could you check this ?

berak gravatar imageberak ( 2017-10-22 07:41:36 -0600 )edit

Hi, thank you for your valuable reply, i am not getting RGB images using webcam i dont know why i am getting like this. any changes in code ??

opencvproblems gravatar imageopencvproblems ( 2017-10-23 00:58:46 -0600 )edit

can you check frame.shape ?

berak gravatar imageberak ( 2017-10-23 01:03:05 -0600 )edit

Yaa i checked using mask = np.zeros(frame.shape[:2], np.uint8) still i am not getting RGB image

opencvproblems gravatar imageopencvproblems ( 2017-10-23 01:12:39 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-10-22 02:30:23 -0600

moHe gravatar image

Hi, I've done this before, but the method is a bit different: link text.

Unfortunately, the speed of changing the frame is low now, so it still needs to be improved a lot. I hope this can help to inspire you.

edit flag offensive delete link more

Comments

Hi thankyou so much, yes its frame rate is very slow how to improve

opencvproblems gravatar imageopencvproblems ( 2017-10-22 23:57:37 -0600 )edit

Hello, I've just figured out why it's so slow, and updated my codes.

Here are the two main reasons:

  1. Before this, more than 90% time was spent on this function: cv2.grabCut(frame, mask, rect, bgdModel, fgdModel, 1, cv2.GC_INIT_WITH_RECT) to get your figure, and the 6-th parameter means the iteration of the algorithm, I changed it from 5 to 1, and now each frame takes around 0.7 seconds, and the effect is still good.

  2. Besides, the rect(size of window in cv2.grabCut()) also affects speed apparently, the previous one was a bit large(But remember that the window should contain both figure and background or the extraction effect is poor).

Here is the concrete definition of the cv2.grabCut().

May this can help you, ha.

moHe gravatar imagemoHe ( 2017-10-23 07:53:09 -0600 )edit

Thankyou so much..

opencvproblems gravatar imageopencvproblems ( 2017-10-24 07:01:10 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-10-21 05:07:51 -0600

Seen: 2,476 times

Last updated: Oct 22 '17