Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

Re : Issues with TargetRegion in frame differencing

$
0
0
  1. now fixed....


  2. float checkMovement(int x, int y, int w, int h)
  3. {
  4.   int movementSum = 0; // Amount of movement in the frame
  5.   for(int ypos = y;ypos < y + h; ypos ++)
  6.   {
  7.     for(int xpos = x;xpos < x + w; xpos ++)
  8.     {
  9.       int index = xpos + (ypos * video.width);
  10.       color currColor = video.pixels[index];

  11.         color prevColor = previousFrame[index];
  12.         int currR = (currColor >> 16) & 0xFF; // Like red(), but faster
  13.         int currG = (currColor >> 8) & 0xFF;
  14.         int currB = currColor & 0xFF;
  15.       // Extract red, green, and blue components from previous pixel
  16.         int prevR = (prevColor >> 16) & 0xFF;
  17.         int prevG = (prevColor >> 8) & 0xFF;
  18.         int prevB = prevColor & 0xFF;
  19.       // Compute the difference of the red, green, and blue values
  20.         int diffR = abs(currR - prevR);
  21.         int diffG = abs(currG - prevG);
  22.         int diffB = abs(currB - prevB);

  23. //
  24.         movementSum += diffR + diffG + diffB;
  25.       // Render the difference image to the screen
  26.         pixels[index] = color(diffR, diffG, diffB);
  27.       // The following line is much faster, but more confusing to read
  28.       //pixels[i] = 0xff000000 | (diffR << 16) | (diffG << 8) | diffB;
  29.       // Save the current color into the 'previous' buffer
  30.       
  31.       // keep the current pixel colour for comparison next time
  32.       previousFrame[index] = currColor;
  33.     }
  34.   }

  35.   int totalPossibleMovement = w*h*255*3; // width * height (of area of interest) * 255 (is the max difference per colour) * 3 (number of colour channels eg:rgb)
  36.   float percentage = (float)movementSum / (float)totalPossibleMovement;
  37.   percentage = percentage * 100.0f;
  38.   
  39.   return percentage;
  40. }


Viewing all articles
Browse latest Browse all 1768

Trending Articles