Selecting majority color from video image and using it to create action
in
Programming Questions
•
1 years ago
hello Processing gods and goddesses: I am trying to create a way to recognize the majority of either r,g or b pixels in a video image and generate a response to that recognition. Specifically I want each instance of that color (up to 10 instances and then reset) to eventually influence the direction of a servo motor. Am I dreaming?
So far I have got the code for breaking down the video capture into pure rgb and now I'm stuck. All inspiration gratefully accepted.
import processing.video.*;
Capture camera;
void setup()
{
size(320, 240);
println(Capture.list());
camera = new Capture(this, 320, 240, 12);
}
// void captureEvent(Capture camera)
//{
//camera.read();
//}
void draw()
{
if (camera.available()) {
camera.read();
image(camera, 0, 0);
loadPixels();
for(int i=0;i<pixels.length;i++)
{
int c=pixels[i];
int r=(c>>16)&0x000000FF;
int g=(c>>8)&0x000000FF;
int b=c&0x000000FF;
if(r>g && r>b)
{
r=255;
g=0;
b=0;
}
else if(g>r && g>b)
{
r=0;
g=255;
b=0;
}
else if(b>r && b>g)
{
r=0;
g=0;
b=255;
}
else
{
r=255;
g=255;
b=255;
}
pixels[i]=0xFF000000|(r<<16)|(g<<8)|b;
}
updatePixels();
}}
So far I have got the code for breaking down the video capture into pure rgb and now I'm stuck. All inspiration gratefully accepted.
import processing.video.*;
Capture camera;
void setup()
{
size(320, 240);
println(Capture.list());
camera = new Capture(this, 320, 240, 12);
}
// void captureEvent(Capture camera)
//{
//camera.read();
//}
void draw()
{
if (camera.available()) {
camera.read();
image(camera, 0, 0);
loadPixels();
for(int i=0;i<pixels.length;i++)
{
int c=pixels[i];
int r=(c>>16)&0x000000FF;
int g=(c>>8)&0x000000FF;
int b=c&0x000000FF;
if(r>g && r>b)
{
r=255;
g=0;
b=0;
}
else if(g>r && g>b)
{
r=0;
g=255;
b=0;
}
else if(b>r && b>g)
{
r=0;
g=0;
b=255;
}
else
{
r=255;
g=255;
b=255;
}
pixels[i]=0xFF000000|(r<<16)|(g<<8)|b;
}
updatePixels();
}}
1