Ask Your Question
-1

Compare video frames

asked 2018-06-22 02:48:14 -0600

Nimya gravatar image

I want to compare each frames of video so that I can detect movements. I am trying to do in OpenCV Python. it would be great if anyone can help me on this.

edit retag flag offensive close merge delete

Comments

@Nimya, can you try to read over your question again, and improve it ? it's all too broad. movement of what ? compare ?

what are you trying to achieve ? what is the purpose ?

try do do some research on your own, like looking up optical flow.

berak gravatar imageberak ( 2018-06-22 03:05:56 -0600 )edit

I want to compare two frames in a video

Nimya gravatar imageNimya ( 2018-06-22 06:08:24 -0600 )edit

sorry, too broad.

berak gravatar imageberak ( 2018-06-22 06:21:41 -0600 )edit

step 1:read two fames into two mats step 2:compare them

jsxyhelu gravatar imagejsxyhelu ( 2018-06-22 07:10:19 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2018-06-22 14:54:54 -0600

MikeTronix gravatar image

This little program runs with python 3.5 using OpenCV 3.0 and compares image frames from an AVI file, displaying the difference image in a window. The differences are just scaled to fit, but you can come up with all kinds of variations on this.

import numpy as np
import cv2

# Capture video from file
cap=cv2.VideoCapture('/data/Code/Python/AAT/sim_imagery.avi')

old_frame = None

while True:

    ret, frame = cap.read()

    if ret == True:

        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        if old_frame is not None:
            diff_frame = gray - old_frame
            diff_frame -= diff_frame.min()
            disp_frame = np.uint8(255.0*diff_frame/float(diff_frame.max()))
            cv2.imshow('diff_frame',disp_frame)
        old_frame = gray

        if cv2.waitKey(30) & 0xFF == ord('q'):
            break

    else:
        print('ERROR!')
        break

cap.release()
cv2.destroyAllWindows()
edit flag offensive delete link more
-1

answered 2018-07-03 11:13:06 -0600

Nimya gravatar image

updated 2020-02-27 21:12:46 -0600

supra56 gravatar image

Code:

 import numpy as np
    import cv2

    cap = cv2.VideoCapture('/Users/nimya/Desktop/people-walking2.mp4')

    ret, current_frame = cap.read()

    fgbg = cv2.createBackgroundSubtractorMOG2()

    previous_frame = current_frame

    def framediff(current_frame_gray,previous_frame_gray):

        frame_diff = cv2.absdiff(current_frame_gray,previous_frame_gray)

        cv2.imshow('frame diff ',frame_diff)

        return frame_diff


    while(cap.isOpened()):

        current_frame_gray = cv2.cvtColor(current_frame, cv2.COLOR_BGR2GRAY)

        previous_frame_gray = cv2.cvtColor(previous_frame, cv2.COLOR_BGR2GRAY)

        frame_diff=framediff(current_frame_gray,previous_frame_gray)

    if cv2.waitKey(1) & 0xFF == ord('q'):

            break

        previous_frame = current_frame.copy()

        ret, current_frame = cap.read()


    cap.release()

    cv2.destroyAllWindows()
edit flag offensive delete link more

Comments

Hi, this far I reached can anyone suggest me a better way to improve this. Thanks.

Nimya gravatar imageNimya ( 2018-07-03 11:18:59 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-06-22 02:48:14 -0600

Seen: 13,186 times

Last updated: Feb 27 '20