Ask Your Question
0

accumulateWeighted code problem

asked 2015-01-31 11:04:44 -0500

reggie gravatar image

updated 2015-01-31 16:38:40 -0500

The following code is not doing what is expected. If i copy the original code from here it doesnt work at all.

In this state I get three windows from my webcam showing me, but when i wave my hand it doesn't disappear, as expected. I've moved things round to get something working, but I'm stuck now

Any pointers would be welcomed :)

import cv2
import numpy as np

c=cv2.VideoCapture(0) 

while(1):

    x,f=c.read()
    avg1=np.float32(f)
    avg2=np.float32(f)   

    if x==True: 
         #print ("x is true")
         #x,f=c.read()


         cv2.accumulateWeighted(f, avg1, 1)
         cv2.accumulateWeighted(f, avg2, 1)


         res1=cv2.convertScaleAbs(avg1)
         res2=cv2.convertScaleAbs(avg2)

         cv2.imshow('avg1', res1)
         cv2.imshow('avg2', res2)

         cv2.imshow('f',f)

    k=cv2.waitKey(20)

    if k==27:
        break

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

Comments

your code works fine here.

can it be , that you're using an older webcam, that needs a 'warmup, and delivers empty frames in the meantime ? (so avg1 would be empty, and f and avg1 do not have the same size.)

also, note this pythonic antipattern: throwing away important result values:

_,f = c.read()

you should check '_' for True, not ignore it ! (or throw it away)

berak gravatar imageberak ( 2015-01-31 14:08:00 -0500 )edit

check c.isOpened() as well

berak gravatar imageberak ( 2015-01-31 14:26:10 -0500 )edit

same result for last 2 comments.

reggie gravatar imagereggie ( 2015-01-31 14:29:57 -0500 )edit

at least you know now, that the capture is your problem, not accumulateWeighted

berak gravatar imageberak ( 2015-01-31 14:38:09 -0500 )edit

I took the spaces out of the = and it appears to be working eg a = b to a=b but each screen is the same, and not as expected.

reggie gravatar imagereggie ( 2015-01-31 14:58:21 -0500 )edit

aww, cmon, rather apply occam's razor, than the voodoo above.

as long as your imshow() stays black, still a capture prob.

berak gravatar imageberak ( 2015-01-31 17:17:49 -0500 )edit

My three windows are not black, they show me sat in front of the laptop. When I wave at myself, I'm expecting the different alpha values, currently set to one, to make my moving hand disappear (depending on the differential alpha values I set). I get the same result if I set the alpha values to 0.1 and 0.01. Could it still be a capture problem if I can see three moving video feeds of my ugly mug?

but if I set them to 0.1 and 0.01, to make the forground disapear (depending on the alpha values i set)

reggie gravatar imagereggie ( 2015-02-01 13:06:40 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-05-11 05:40:43 -0500

reggie gravatar image

updated 2015-05-12 05:27:00 -0500

By shifting the avg1=np.float32(f) and avg2=np.float32(f) after the frame has been checked, ie if x==True: and adding if avg(1&2) is None: in front of avg(1&2)=np.float32(f) and lastly adding

avg1 = None 
avg2 = None

has done the trick.

I'm not entirely sure why this has worked, but I think it's to do with letting the camera warm up as berak suggested.

import cv2
import numpy as np

c=cv2.VideoCapture(0) 

avg1 = None
avg2 = None

while(1):

    x,f=c.read()



    if x==True: 

         if avg1 is None: avg1=np.float32(f)
         if avg2 is None: avg2=np.float32(f)   
         #print ("x is true")
         #x,f=c.read()


         cv2.accumulateWeighted(f, avg1, 0.01)
         cv2.accumulateWeighted(f, avg2, 0.1)


         res1=cv2.convertScaleAbs(avg1)
         res2=cv2.convertScaleAbs(avg2)

         cv2.imshow('avg1', res1)
         cv2.imshow('avg2', res2)

         cv2.imshow('f',f)

    k=cv2.waitKey(20)

    if k==27:
        break

cv2.destroyAllWindows()
c.release()
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2015-01-31 11:04:44 -0500

Seen: 488 times

Last updated: May 12 '15