-1

I read about opencv on google and found the following sample code online to play with:

import cv2

def diffImg(t0, t1, t2):
  d1 = cv2.absdiff(t2, t1)
  d2 = cv2.absdiff(t1, t0)
  return cv2.bitwise_and(d1, d2)

cam = cv2.VideoCapture('vid1.mp4')

winName = "Movement Indicator"
cv2.namedWindow(winName, cv2.CV_WINDOW_AUTOSIZE)

# Read three images first:
t_minus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
t = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)

while True:
  cv2.imshow( winName, diffImg(t_minus, t, t_plus) )

  # Read next image
  t_minus = t
  t = t_plus
  t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)

  key = cv2.waitKey(10)
  if key == 27:
    cv2.destroyWindow(winName)
    break

print "Goodbye"

It produces the following kind of output for a sample video that I give to it: opencv image difference frame output

So I am getting such results with this script. Now what I am trying to figure out is - 1) Get the bounding rect for the moving object in the video file 2) copy the contents of that bounding rect from original video frame to another and write the finished video to a file

Also, the screenshots on trying Kanishak Katahra's solution below I am getting the following output in the result window in the right side in the screenshot below -

Harshit Laddha
  • 2,044
  • 8
  • 34
  • 64
  • Could you please reduce your question to the smallest possible topic? Are you asking how to write to a movie file using OpenCV? Are you asking how to write to a new window using OpenCV? Are you asking how to save `diffImg` to a variable to accomplish either of these things? Finally, could you please edit your question to include a link to what resource you're using for writing this script? – Seanny123 Apr 18 '17 at 10:47
  • I want to clip the highlighted portions from diffimg and copy only the highlighted portions in diffimg from the original frame to a video file – Harshit Laddha Apr 18 '17 at 11:07
  • So what have you tried to accomplish this? When you look at the results of `diffImg` can you see how it might be input into a video file? – Seanny123 Apr 18 '17 at 11:34

1 Answers1

0

Make the point of interest in the output you are getting as 1s. Make the rest as 0. Convert it to 3D array of size same as original image. Multiply original image with the output you are getting.

import cv2
import numpy as np
from numpy import newaxis
import time

def diffimg (a,b,c):
    t0 = cv2.absdiff(a,b)
    t1 = cv2.absdiff(b,c)
    t3 = cv2.bitwise_and(t0,t1)
    return t3

cap = cv2.VideoCapture(0)
t = cap.read() [1] 
tp = cap.read() [1]
tpp = cap.read() [1]

t = cv2.cvtColor(t,cv2.COLOR_BGR2GRAY)
tp = cv2.cvtColor(tp,cv2.COLOR_BGR2GRAY)
tpp = cv2.cvtColor(tpp,cv2.COLOR_BGR2GRAY)


while True:

    img = diffimg(t,tp,tpp)
    cv2.imshow("motion detct",img)

    key = cv2.waitKey(10)


    res,img2 = cap.read()
    #print img2.shape

    t = tp
    tp = tpp
    tpp = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)

    cv2.imshow('image',img)
    print img
    img[img <= 10] = 0    #Try adjusting this value for better results.
    img[img !=  0] = 1
    img = np.repeat(img[:, :, np.newaxis], 3, axis=2)
    img3 = np.multiply( img,img2)
    cv2.imshow('result',img3)



    if key == 27:   
        cv2.destroyAllWindows()
        break;



print "Goodbye User"
Major Tom
  • 31
  • 6
  • Hi, updating the question with screenshots of results with your code. What I am trying to do is to grab the region in the result window that is visible from the image window so that it comes with all the colors and interior details now in the result window it is only showing pixelated boundary I think of moving object – Harshit Laddha Apr 18 '17 at 18:10
  • It is giving you the pixels that have changed. When you move a unicolor object by a small distance, not many pixels change. That is why you are getting this kind of output. Try the same code with smaller and colorful object to get better results. – Major Tom Apr 18 '17 at 18:59
  • For clearer understanding, include a 0.5 seconds delay before res,img2 = cap.read(). – Major Tom Apr 19 '17 at 11:27