<< Chapter < Page Chapter >> Page >

High-level python implementation

The Python OpenCV library exposes an API for the entire Canny edge detection algorithm. The Canny algorithm issimilar to, but more nuanced than, our algorithm, substituting our selective thresholding procedure for hysteresis. However,the end results are very similar. As is evident from Listing 5, the procedure is very straightforward.

Listing 5. python implementation of canny edge detection with opencv

LOW_THRESHOLD = 100 HIGH_THRESHOLD = 200edges = cv2.Canny(img, LOW_THRESHOLD, HIGH_THRESHOLD)

Motion detection algorithm

Our motion detection algorithm compares the edges from two frames temporally separated by a small interval of time and attempts to estimate regions of the frame that exhibitmovement or motion. This process is then repeated continuously to provide a real-time visualization of the regions ofmovement in a continuous video stream. The motion detection algorithm we present is composed of the following steps:

  1. Given two frames F1 and F2 separated by a time interval of Δt, compute its edges E1 and E2.
  2. Create a strict binary difference matrix D0 between E1 and E2 whose value is high at locations at which anedge is present in frame but not the other, and low at locations at which both frames have an edge or no edge.
  3. Apply a parameter-controlled movement tolerance thresholding operation to the matrix D0 to obtainmatrix D, in order to suppress potential false positives of detected motion from incidental camera movement.

F1 and F2 are obtained by sampling frames from a continuous video stream, a task aided by the OpenCV C library.Thus, the time interval Δt will be small enough for a realtime algorithm. The edges E1 and E2 are computed with theedge detection algorithm of Section II. Below, we explore, in greater depth, the procedures for building a thresholdeddiffrence matrix and finally estimating the motion area by analyzing a spatial difference density map.

Thresholded frame-by-frame difference map

The preliminary requirement in determining areas of motion from two frames is a systematic method of determining andquantifying the differences between two frames separated by a small Δt. Intuitively, areas of motion within the time intervalΔt should exist at locations at which the difference between the two frames is large. This process is then continuouslyrepeated for every pair of input frames in order to create a realtime motion detector. In our algorithm, we quantify differenceson a binary basis using edge data from our edge detection algorithm, then determine motion locations from a thresholdeddensity map of the difference matrix D. More formally, we begin with a matrix D0 created from asimple comparison of E1 and E2. Then, for each location (i, j) in D0, a thresholded differencematrix D is created by considering a box of constant size b x b around that pixel and suppressing the value of the surroundingpixels to 0 if its value matches that at D0[i,j]. This step isdesigned to reduce false positives of motion detection arising from stray movement of the frame (e.g. camera shake). Thecomputation of D is demonstrated in Algorithm 2. The output matrix is then used as input to building a spatial differencedensity map, from which we estimate motion locations.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Elec 301 projects fall 2015. OpenStax CNX. Jan 04, 2016 Download for free at https://legacy.cnx.org/content/col11950/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Elec 301 projects fall 2015' conversation and receive update notifications?

Ask