We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
change color manipulated pixels (Read 2241 times)
change color manipulated pixels
Jun 9th, 2009, 8:33am
 
Hi there,
I'm verry new to processing. I'm currenty working on a tool that wil take a certain color from a captured image by webcam.

this is my code so far:

Quote:
/**
* Background Subtraction
* by Golan Levin.
*
* Detect the presence of people and objects in the frame using a simple
* background-subtraction technique. To initialize the background, press a key.
*/


import processing.video.*;

int numPixels;
int[] backgroundPixels;
Capture video;

void setup() {
 // Change size to 320 x 240 if too slow at 640 x 480
 size(640, 480, P2D);
 
 video = new Capture(this, width, height, 24);
 numPixels = video.width * video.height;
 // Create array to store the background image
 backgroundPixels = new int[numPixels];
 // Make the pixels[] array available for direct manipulation
 loadPixels();
}

void draw() {
 if (video.available()) {
   video.read(); // Read a new video frame
   video.loadPixels(); // Make the pixels of video available
   // Difference between the current frame and the stored background
   int presenceSum = 0;
   for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame...
     // Fetch the current color in that location, and also the color
     // of the background in that spot
     color currColor = video.pixels[i];
     color bkgdColor = backgroundPixels[i];
     // Extract the red, green, and blue components of the current pixel’s color
     int currR = (currColor >> 16) & 0xFF;
     int currG = (currColor >> 8) & 0xFF;
     int currB = (currColor >> 8) & 0xFF;
     
     if (currR >= 180 && currG >=180 && currB >= 186) {
   
     float r = red(255);
     currR = int(r);
     
     float g = blue(255);
     currG = int(g);
     
     float b = blue(255);
     currB = int(b);
     
if(mousePressed) {
         if(mouseButton == RIGHT){
     
     float rr = blue(255);
     currR = int(rr);
     
     float gr = green(255);
     currG = int(gr);
     
     float br = red(10);
     currB = int(br);
 }
   
   
if(mouseButton == LEFT){
     
     float rl = red(90);
     currG = int(rl);
     
     float gl = green(255);
     currR = int(gl);
     
     float bl = blue(200);
     currB = int(bl);
   }
       }



   }
     
     // Render the difference image to the screen
     //pixels[i] = color(diffR, diffG, diffB);
     // The following line does the same thing much faster, but is more technical
     pixels[i] = 0xFF000000 | (currR << 16) | (currG << 8) | currB;
   }
   updatePixels(); // Notify that the pixels[] array has changed
   println(presenceSum); // Print out the total amount of movement
 }
}

// When a key is pressed, capture the background image into the backgroundPixels
// buffer, by copying each of the current frame’s pixels into it.
void keyPressed() {
 video.loadPixels();
 arraycopy(video.pixels, backgroundPixels);
}


I wonder if it is possible to change the colors of the manipulated pixels at random. For example: If you press "space" the color of the manipulated pixels will change to an other color and stay that color.

Thanks!,

bart
Re: change color manipulated pixels
Reply #1 - Jun 10th, 2009, 12:48pm
 
Hi there!

It is quite possible to manipulate the pixels in an image, but I am not quite sure what you mean when you say 'stay that color' ... The original pixel will be lost in the next frame, but most pixels would probably be more or less the same colors. One way of manipulating the colors is to just add a (random) number to the pixels and see what happens.



Re: change color manipulated pixels
Reply #2 - Jun 11th, 2009, 1:26pm
 
Hi,

By "stay that color" I mean if you release the key the manipultated pixels stay a certain color and not the original.

Where do I have to put the "(random)"

Thanks
Page Index Toggle Pages: 1