Displaying black and white video from color webcam feed

Hello,
How would one convert a color video feed coming from a webcam into black and white in real-time.
Rather than having to go into the camera settings to set this manually, I would like to automate it in code.

Thanks!
Jack

There are several ways to do this, one of the easiest is to just set up an ofTexture that uses GL_LUMINANCE instead of GL_RGB.

  
	ofVideoGrabber grab;  
	ofTexture bw;   

and then:

  
void app::setup() {  
	grab.initGrabber(320, 240);  
	  
	bw.allocate(320, 240, GL_LUMINANCE);  
	  
}  
  
void app::update(){  
	  
	grab.grabFrame();  
	if(grab.isFrameNew()) {  
		bw.loadData(grab.getPixelsRef());  
	}  
}  

You can also walk the pixels from grab.getPixelsRef() and average them by adding (R+G+B)/765 or with a shader (probably overkill).

really interesting answer josh :slight_smile: i feel like that’s the “OF 2011” answer.

a few years ago, you would have got a different answer. something like this: go to the “movieGrabberExample” and look at how it’s inverting the image. it goes through each pixel and says “255 - pixels”. if you change this for loop to look like this:

  
  
for (int i = 0; i < totalPixels; i+=3){  
	int r = pixels[i+0], g = pixels[i+1], b = pixels[i+2];  
	unsigned char avg = (r + g + b) / 3;  
	pixels[i+0] = avg;  
	pixels[i+1] = avg;  
	pixels[i+2] = avg;  
}  
  

there is one more solution, which is also kind of “OF 2011”, that uses ofPixels, getColor(x,y) and getBrightness(). but i think josh’s answer is the best if you’re only doing display, and not further modification of the image.

Yeah, actually, for learning purposes I think walking the pixels is more informative and (wonky meta-commentary) I think if this question were in the beginner forum I would have answered it that way, but converting using the pixels seems more advanced. It’s funny, this is 1st edition vs 2nd edition of my book in a nutshell.

yeah, i didn’t knew that you could do that.

Cool, I realize that this is a bit late, but just wanted to say thanks for all the suggestions!