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
openCV quickie (Read 3266 times)
openCV quickie
Mar 14th, 2009, 10:39am
 
all,
this should be easy to solve:

i'm trying to load up all the pixels from one video feed in open cv, into an integer array so that i can cycle through them with a for loop...

something like this color filter i wrote; but it won't work; i'm obviously using the wrong code in the first line to load up the pixels.

how do i do this? sure it must be simple enough...
s




void colorFilter(){
 int[] img =  opencv.image(); //get the normal image of the camera
 
 float r,g,b;
 loadPixels();

 if (filtering){
  for(int i=0;i<width*height;i++){ //loop through all the pixels
   float rval = red(img[i]);
   float gval = green(img[i]);
   float bval = blue(img[i]);
 
   r = sampleR*((bval - rval) + (bval - bval));
   g = sampleG*((bval - rval) + (bval - bval));
   b = sampleB*((bval - rval) + (bval - gval));
   pixels[i] = color(r,g,b); //draw each pixel to the screen
 }}
else {
 for(int i=0;i<width*height;i++){ //loop through all the pixels
   float rval = red(img[i]);
   float gval = green(img[i]);
   float bval = blue(img[i]);
 
  r = rval;
  g = gval;
  b = bval;
  pixels[i] = color(r,g,b); //draw each pixel to the screen
}}  
 updatePixels();
}
Re: openCV quickie
Reply #1 - Mar 19th, 2009, 10:30am
 
The loadPixels() pull the pixels currently on the screen if you want the pixels from the openCV image you could either: put the image on screen with image(opencv.image(), 0, ) and the do loadPixels();
or, if you don't need to display the image, do opencv.image().loadPixels()
manipulate the pixels as you need to and then call opencv.image().updatePixels()
and the display it with: image(opencv.image(), 0, )

hope it did the trick
Color Filter for openCV
Reply #2 - Mar 20th, 2009, 11:38am
 
this now works; provides a color filter system for openCV images.

click on the color to start a filter for it
works particularly well for blue

s

Quote:
//Sam McElhinney

import hypermedia.video.*;
color colorSample = color (255,255,255);
float  sampleR;
float  sampleG;
float  sampleB;
boolean filtering = false;
OpenCV opencv;
 
void setup(){
  size(320,240);
  opencv = new OpenCV( this );  
  opencv.capture(width,height);
}

void draw(){
  opencv.read();
  colorFilter();
}
    
//COLOR FILTER
void colorFilter(){
  image(opencv.image(), 0, 0);
  loadPixels();
if (filtering){
   for(int i=0;i<width*height;i++){ //loop through all the pixels
    float rval = red(pixels[i]);
    float gval = green(pixels[i]);
    float bval = blue(pixels[i]);
    float r;
    float g;
    float b;
    r = sampleR*((bval - rval) + (bval - bval)); 
    g = sampleG*((bval - rval) + (bval - bval));
    b = sampleB*((bval - rval) + (bval - gval)); 
    pixels[i] = color(r,g,b); //draw each pixel to the screen
  }}
else {
  for(int i=0;i<width*height;i++){ //loop through all the pixels
    float rval = red(pixels[i]);
    float gval = green(pixels[i]);
    float bval = blue(pixels[i]);
    float r;
    float g;
    float b;
    r = rval; 
    g = gval;
    b = bval; 
    pixels[i] = color(r,g,b); //draw each pixel to the screen
}}  
  updatePixels();
}

void mousePressed(){
  int loc = mouseX + mouseY*width;
  colorSample = pixels[loc];
  sampleR = red(colorSample);
  sampleG = green(colorSample);
  sampleB = blue(colorSample);
  filtering = !filtering;
}

void keyPressed(){
  filtering = false;
}

public void stop(){
  opencv.stop();//stop the object
  super.stop();
}


Re: openCV quickie
Reply #3 - Mar 20th, 2009, 1:44pm
 
hey ricki

one last point - it is a bit odd - maybe i am doing something wrong
when i use your commands:

opencv.image().loadPixels();
opencv.image().updatePixels();

instead of the usual loadPixels and updatePixels (ie when i just want to selectively load up the pixels in the opencv image)

it comes back with a null pointer error when cycling through the pixel array - so it isn't loading up properly

any thoughts? do i have something wrong there? it would be a lot handier to just load up the opencv image pixels without having to display them!

best

s


Re: openCV quickie
Reply #4 - Mar 20th, 2009, 2:49pm
 
ok
i have solved it myself...

Quote:
//Sam McElhinney

import hypermedia.video.*;
color colorSample = color (255,255,255);
float  sampleR;
float  sampleG;
float  sampleB;
int cam_width;
int cam_height;
int comb_width;
int comb_height;
int x = 10;
int y = 10;
boolean filtering = false;
OpenCV opencv;
 
void setup(){
  size(320,480);
  cam_width = 320-x;
  cam_height = 240-y;
  comb_width = cam_width;
  comb_height = 2*cam_height;
  opencv = new OpenCV( this );  
  opencv.capture(cam_width,cam_height);
}

void draw(){
  opencv.read();
  colorFilter();
}
    
//COLOR FILTER
void colorFilter(){
  
  int[] cam =  opencv.pixels();
  loadPixels();
if (filtering){
   for(int i=0;i<cam_width*cam_height;i++){ //loop through all the camera pixels
    float rval = red(cam[i]);
    float gval = green(cam[i]);
    float bval = blue(cam[i]);
    float r;
    float g;
    float b;
    r = sampleR*((bval - rval) + (bval - bval)); 
    g = sampleG*((bval - rval) + (bval - bval));
    b = sampleB*((bval - rval) + (bval - gval)); 
    cam[i] = color(r,g,b); //change each pixel
   }}
    
else {
  for(int i=0;i<cam_width*cam_height;i++){ //loop through all the camera pixels
    float rval = red(cam[i]);
    float gval = green(cam[i]);
    float bval = blue(cam[i]);
    float r =0;
    float g = 0;
    float b =0;
    r = rval; 
    g = gval;
    b = bval; 
    cam[i] = color(r,g,b);  // dont change each pixel
}}  
 updatePixels();
 
 PImage comb = createImage(comb_width, comb_height, RGB);
   comb.loadPixels();
   for (int i = 0; i < comb_width*comb_height/2; i++) {
      comb.pixels[i] = cam [i];
    }
   for (int i = comb_width*comb_height/2; i < comb_width*comb_height; i++) {
      comb.pixels[i] = cam [i-comb_width*comb_height/2];
    } 
   comb.updatePixels();
   image(comb,5,5);
}

void mousePressed(){
  int loc = mouseX + mouseY*width;
  colorSample = pixels[loc];
  sampleR = red(colorSample);
  sampleG = green(colorSample);
  sampleB = blue(colorSample);
  filtering = !filtering;
}

void keyPressed(){
  filtering = false;
}

public void stop(){
  opencv.stop();//stop the object
  super.stop();
}





Page Index Toggle Pages: 1