0

I have write down a code that can detect a moving object on a stable background and return a dilated binary spot that can be used to track position in term of x,y coordinates using "cv2.findContours" method in real-time. My problem is that when i run this code it shows two spot one is stable spot which shows the exact initial position of object while one spot continuously moving and showing the current position in real-time. now i just want to show the real-time position rather the the stable spot

import scipy.misc
import cv2
import time

cam = cv2.VideoCapture("VID_20150401_191129.3gp")

r, f1 = cam.read()
f1 = scipy.misc.imresize(f1, 0.4)

while(1):


    r2, f2 = cam.read()
    f2 = scipy.misc.imresize(f2, 0.4)




    frameDelta = cv2.absdiff(f2,f1)
    thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
    thresh = cv2.dilate(thresh, None, iterations=4)

    cv2.imshow('im',thresh)

    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break
jax
  • 3,927
  • 7
  • 41
  • 70

1 Answers1

0

In this code you are doing is that setting initial frame (reading image here r, f1 = cam.read() f1 is frame) as a background-frame and the reading frame as current-frame. You are subtracting first frame with the rest of frames. To get the motion objects you can use an another function called backgroundUpdate. like this

def backgroundUpdate(): backgroundFrame = np.uint8((0.1* currentFrame) + ((0.9) * previousFrame))

Here current frame is the reading frame and previous frame was last read.

So here your code can be change like this

 cam = cv2.VideoCapture("VID_20150401_191129.3gp")

 while(1):
     r, currentFrame = cam.read()
     currentFrame = scipy.misc.imresize(f2, 0.4)
     previousFrame = currentFrame

     if backgroundFrame is None:
         previousFrame = currentFrame
         backgroundUpdate()
     else:
         backgroundUpdate()

     previousFrame = currentFrame     
     frameDelta = cv2.absdiff(backgroundFrame, currentFrame)
     thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
     thresh = cv2.dilate(thresh, None, iterations=4)

     cv2.imshow('im',thresh)

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

The backgroundUpdate function updates the backgroundFrame through out the capture. This will give an good result and and small motions also neglecting. Make sure that both functions can access the variables. For that you can use global.

And for more optimal solution after capture you can use gray conversion and blur.Here is the code for that.

In [1]: currentFrame = cv2.cvtColor(Frame, cv2.COLOR_BGR2GRAY) In [2]: currentFrame = cv2.GaussianBlur(currentFrame, (25, 25), 0)

Rahul K P
  • 15,740
  • 4
  • 35
  • 52
  • Thanks for your comments . Its showing "NameError: name 'backgroundFrame' is not defined " what should i do.. – jax Sep 30 '15 at 10:27
  • In the start of the function before while loop just declare the `backgroundFrame = None`. Then it will be solve. – Rahul K P Sep 30 '15 at 18:20
  • Hey, thanks i already did that but now its showing error like this: OpenCV Error: Sizes of input arguments do not match (The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array') in arithm_op, file /build/buildd/opencv-2.3.1/modules/core/src/arithm.cpp, line 1253 Traceback (most recent call last): File "stack.py", line 26, in frameDelta = cv2.absdiff(backgroundFrame, currentFrame) – jax Oct 05 '15 at 08:09
  • can you please comment the resize line in the code (`currentFrame = scipy.misc.imresize(f2, 0.4)`). And post the result. – Rahul K P Oct 05 '15 at 11:09
  • yes i already comment out the respective line but error still exist – jax Oct 05 '15 at 11:11
  • Can you send that video sample for testing. – Rahul K P Oct 05 '15 at 11:16
  • yes sure how can i send it after compression its about 50 MB in size – jax Oct 05 '15 at 11:26
  • can u please provide me your E mail ID – jax Oct 05 '15 at 11:27
  • I have sent with google drive hopefully U have received the mail – jax Oct 05 '15 at 12:10