Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.

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()

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.up as berak suggested.

 import cv2
 import numpy as np

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()

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

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()

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()

This appeared to work too, I think its because I have 2 functions and I need to declare the global variables inside each function. I thought if I declare something as global anywhere it could be seen by everything; but one has to declare global variables from within a function for that function to see it from outside.

import cv2
import numpy as np

def accu_w(frame):

    global avg1
    global avg2


    if avg1 is None: avg1 = np.float32(frame)
    if avg2 is None: avg2 = np.float32(frame)

    cv2.accumulateWeighted(frame, avg1, 0.1)
    cv2.accumulateWeighted(frame, avg2, 0.0006)

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


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




def main():

    c = cv2.VideoCapture(0)


    global avg1
    avg1 = None
    global avg2
    avg2 = None

    while(True):


        got_frame,frame = c.read()

        if got_frame:

            accu_w(frame)

            k = cv2.waitKey(20)

            if k == 27:
                break

    cv2.destroyAllWindows()
    c.release()


main()

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()

This appeared to work too, I think its because I have 2 functions and I need to declare the global variables inside each function. I thought if I declare something as global anywhere it could be seen by everything; but one has to declare global variables from within a function for that function to see it from outside.

import cv2
import numpy as np

def accu_w(frame):

    global avg1
    global avg2


    if avg1 is None: avg1 = np.float32(frame)
    if avg2 is None: avg2 = np.float32(frame)

    cv2.accumulateWeighted(frame, avg1, 0.1)
    cv2.accumulateWeighted(frame, avg2, 0.0006)

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


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




def main():

    c = cv2.VideoCapture(0)


    global avg1
    avg1 = None
    global avg2
    avg2 = None

    while(True):


        got_frame,frame = c.read()

        if got_frame:

            accu_w(frame)

            k = cv2.waitKey(20)

            if k == 27:
                break

    cv2.destroyAllWindows()
    c.release()


main()