M1KES
YaBB Newbies
Offline
Posts: 19
Color tracking in frame differencing
Oct 6th , 2009, 4:12pm
Hi everyone! I am trying to implement color detection in my code but i'm not sure how to do it.. here's my code: import processing.video.*; // Size of each cell in the grid int cellSize = 8; // Number of columns and rows in our system int cols, rows; // Variable for capture device Capture video; int firsttime = 0; int numPixels; int[] previousFrame; int[] differenceFrame; void setup() { size(640, 480, P3D); frameRate(30); cols = width / cellSize; rows = height / cellSize; colorMode(RGB, 255, 255, 255, 100); println(cols); println(rows); // Uses the default video input, see the reference if this causes an error video = new Capture(this, cols, rows, 30); numPixels = video.width * video.height; // Create an array to store the previously captured frame previousFrame = new int[numPixels]; differenceFrame = new int[numPixels]; loadPixels(); background(0); } void draw() { float corx, cory; if (video.available()) { // When using video to manipulate the screen, use video.available() and // video.read() inside the draw() method so that it's safe to draw to the screen video.read(); // Read the new frame from the camera video.loadPixels(); // Make its pixels[] array available if (firsttime < 10) { firsttime++; for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame... previousFrame[i] = video.pixels[i]; } } fill(color(255, 255, 255, 5)); noStroke(); rectMode(CORNER); rect(0,0,width, height); int movementSum = 0; // Amount of movement in the frame for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame... color currColor = video.pixels[i]; color prevColor = previousFrame[i]; // Extract the red, green, and blue components from current pixel int currR = (currColor >> 16) & 0xFF; // Like red(), but faster int currG = (currColor >> 8) & 0xFF; int currB = currColor & 0xFF; // Extract red, green, and blue components from previous pixel int prevR = (prevColor >> 16) & 0xFF; int prevG = (prevColor >> 8) & 0xFF; int prevB = prevColor & 0xFF; // Compute the difference of the red, green, and blue values int diffR = abs(currR - prevR); int diffG = abs(currG - prevG); int diffB = abs(currB - prevB); // Add these differences to the running tally movementSum += diffR + diffG + diffB; // Render the difference image to the screen //differenceFrame[i] = color(diffR, diffG, diffB); // The following line is much faster, but more confusing to read differenceFrame[i] = 0xff000000 | (diffR << 16) | (diffG << 8) | diffB; // Save the current color into the 'previous' buffer previousFrame[i] = currColor; // To prevent flicker from frames that are all black (no movement), // only update the screen if the image has changed. } // To prevent flicker from frames that are all black (no movement), // only update the screen if the image has changed. if (movementSum >= 0) { for (int i = 0; i < cols; i++) { for (int j = 0; j < rows; j++) { int x = i*cellSize; int y = j*cellSize; int loc = (video.width - i - 1) + j*video.width; // Reversing x to mirror the image if ((brightness(differenceFrame[loc]) >= 20) && (brightness(previousFrame[loc]) < brightness(get(x+cellSize/2, y+cellSize/2)) )) { float r = red(video.pixels[loc]); float g = green(video.pixels[loc]); float b = blue(video.pixels[loc]); color c = color(r, g, b, 50); pushMatrix(); translate(x+cellSize/2, y+cellSize/2); rotate((2 * PI * brightness(c) / 125.0)); rectMode(CENTER); noFill(); stroke(c); corx =0+random(-cellSize,cellSize); cory =0+random(-cellSize,cellSize); line(corx, cory, corx+pow(brightness(differenceFrame[loc])/20,2)+cellSize, cory+ pow(brightness(differenceFrame[loc])/20,2)+cellSize); popMatrix(); } } } } } } How would i proceed so that it would only detect the color red?