We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I've been trying to get the degree of redness in an RGB image and show it in another image. But the destination image always appears empty. I'm pretty much a newb at processing. here's my code.
//code to get Degree of Redness
import processing.video.*;
Capture cam;
PImage img,imDeg,imThresh;
void setup()
{
imDeg=createImage(320,240,GRAY);
imThresh=createImage(320,240,GRAY);
size(640, 480);
String [] cameras=cam.list();
cam = new Capture(this,320,240,cameras[0]);
cam.start();
}
void DegreeRed()
{
img.loadPixels();
imDeg.loadPixels();
for(int x=0;x<320;x++)
{
for(int y=0;y<240;y++)
{
//location of pixel
int loc=x*240+y;
//get RGB values
float r1=red(img.pixels[loc]);
float g1=green(img.pixels[loc]);
float b1=blue(img.pixels[loc]);
//Degree of Redness
float r=r1-(g1+b1);
float g=r1-g1;
float b=r1-b1;
if(r<0) r=0;
if(g<0) g=0;
if(b<0) b=0;
float Dr=r+g+b;
imDeg.pixels[loc]=int(Dr);
}
}
imDeg.updatePixels();
}
void draw()
{
scale(2);
if (cam.available()) {
// Reads the new frame
cam.read();
}
img=cam;
DegreeRed();
//Thresh();
image(imDeg, 0, 0);
}
I'm continuously taking images from a camera and passing it through this function and printing the resulting imDeg image on the screen. But the image always appears empty. Thank You for your help.
Answers
http://forum.processing.org/two/discussion/8045/how-to-format-code-and-text
thanx for informing about the format
I believe your RGB algorithm isn't working right.
You better search the forum for how to do it correctly.
Better read about alternative approaches for RGB extraction and reinsertion:
And although not exactly you're looking for, here's an example of getting RGB from
color
:http://forum.processing.org/two/discussion/4461/pimage-color-format
I'm sorry but I don't understand. I think I have used those red,green and blue functions correctly. When i print out the RGB values, it works. Is it perhaps reinsertion in the GRAY image object(imDeg) that is causing the problem?
Indeed
float Dr=r+g+b;
doesn't seem right to me.It needs bitshifting in order to place each primary color in its right place within the
color
!Also, you shouldn't be using
float
while dealing w/ RGB either!But since i declared the image as GRAY, shouldn't it be a single dimensioned colour rather than it having RGBs? Also, the red,blue,green functions return a floating point value. I am converting it to int when assigning it back
imDeg.pixels[loc]=int(Dr);
. So what should i do if i want a grayscale image? Is it perhaps assigning the same value to all three of RGB?Gray scale happens when all RGB channels are similar to each other.
Preferably, when R = G = B! :-B
For a gray image you indeed want the same value for all RGB, but if you just add them together you'll have a number above 255 and that won't be of much use. To get a gray color with the right value you must average the RGB colors, that is, grayColor = (r + g + b)/3.
Thanx for your help
Just an extra addendum. This article explains better what a
color
is in Processing: ;)http://forum.processing.org/two/discussion/8086/what-is-a-color-in-processing