<< Chapter < Page Chapter >> Page >

Algorithm 2: determining the difference matrix between two frames, given their edge data: pseudocode for a serial, cpu implementation

Determining the difference matrix between two frames, given their edge data: pseudocode for a serial, CPU implementation

Fig. 13. example of difference matrix computation: original initial frame f1 of dimensions 5312 x 2988

Example of difference matrix computation: original initial frame F1 of dimensions 5312 x 2988

Fig. 14. example of difference matrix computation: original initial frame f2

F2

Fig. 15. example of difference matrix computation: edges of the initial frame e1

E1

Fig. 16. example of difference matrix computation: edges of the initial frame e2

E2

Fig. 17. example of difference matrix computation: difference matrix d with parameter b= 12

D'

Motion area estimation via a spatial difference density map

The difference matrix D determined with Algorithm 2 is effectively a maximally precise estimate of motion area (e.g.,to the resolution of a single pixel, since D is computed on a pixel-by-pixel basis). However, in practice, it is more usefulto consider general regions of the image around which motion exists. For this reason, we build a density map of the matrixD using equal-sized rectangular sections of the input image that approximates the density of the difference per unit area.Thus, the original M x N difference matrix D is reduced to a matrix D' of size m x n such thatM = k1m N = k2nwhere k1, k2 are integers. Intuitively, choosing a higher k1 and k2 will result in a higher resolution over which the density isevaluated. Selection of k1 and k2 should depend on how large of a region over which motion should be estimated. The upperlimits of k1 and k2 are M and N, respectively, at which point D' = D. Each index (m, n) in D' corresponds to the upperleftcorner of a rectangular section of A whose width is M/m and height is N/n .Regions of motion are the rectangular sections whose density exceeds a user-defined threshold􀀀, denoting the number of difference pixels over the total number of pixels consideredin the rectangular section. For each such region R in A of size M/m x N/n

Algorithm 3: estimating regions of motion in an image with a difference density matrix: pseudocode for a serial, cpu implementation

Estimating regions of motion in an image with a difference density matrix: pseudocode for a serial, CPU implementation

Fig. 18. spatial difference density matrix d0 of figure 17, as determined by algorithm 3 with parameters m = 10, n = 6, and gama= 0:01

D'

Fig. 19. final estimate of motion area of figures 13 and 14 based on the density map of figure 18

Motion Estimation

Gpu parallelization and cuda implementation

Our CUDA implementation of our motion detection algorithm is similar to that for our implementation of separableconvolution and edge detection. Using the same 16 threads per block, we divide the input image into the appropriate numberof blocks such that the thresholded difference computation at each index (i, j) of Algorithm 2 has its own thread. Thus, eachelement of D is calculated in parallel.

Listing 6. gpu computation of thresholded difference matrix

__global__ void difference_filter( int *dev_out,int *edges_1, int *edges_2,int width, int height,int threshold ) {const int r = blockIdx.y * blockDim.y + threadIdx.y; const int c = blockIdx.x * blockDim.x + threadIdx.x;const int i = r * width + c; // Set it to 0 initiallydev_out[i] = 0;int crop_size = 7; if (r>crop_size&&c>crop_size&&r<height - crop_size&&c<width - crop_size&&edges_1[i] != edges_2[i]) { // Set to 255 if there is a pixel mismatchdev_out[i] = 255;for (int x_apron = -threshold; x_apron<= threshold; x_apron++) { for (int y_apron = -threshold; y_apron<= threshold; y_apron++) { // Ensure the requested index is within bounds of imageif (c + x_apron>0&&r + y_apron>0&&c + x_apron<width&&r + y_apron<height) { // Check if there is a matching pixel in the apron, within the thresholdif (edges_1[(r + y_apron) * width + c + x_apron] == edges_2[i]) { // Set it back to 0 if a corresponding pixel exists within the vicinity of the matchdev_out[i] = 0;} }} }} }

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