Processing 摄像头代码改成灰度显示

有朋友在百度贴吧提了下面的问题【参考1】

“求助 帮我把下面的摄像头代码改成黑白的摄像头”

// Original Source created by Ben Stephenson, University of Calgary
// Code modified by Sandy Graham

////////////////////////////////////////////////////////////////////////////////
// DO NOT CHANGE ANYTHING IN THIS FILE EXCEPT IN THE MARKED REGION NEAR THE 
// BOTTOM OF THE FILE.
////////////////////////////////////////////////////////////////////////////////
import processing.video.*;

Capture cam; // the object representing the camera
PImage camimg; // image from the camera
PImage transformed; // the transformed image

float cam_r, cam_g, cam_b; // camera pixel red, green and blue
float trans_r, trans_g, trans_b; // transformed pixel red, green and blue

void setup()
{
String[] cameras = Capture.list(); // get a list of all the available cameras
// and their resolutions / frame rates

if (cameras.length == 0) // check that we have an avaialble camera 
{ 
println("There are no cameras available for capture.");
exit();
} 
else // display a list of the cameras 
{
println("Available cameras:");
for (int i = 0; i < cameras.length; i++) 
{
println(cameras[i]);
}

// This attempts to find a camera with a resolution of 640x480 at 30 frames per
// second. However, there is no guarantee that any particular camera provides
// this resolution / framerate. As an alternative, you can initialize the
// camera as:
//
// cam = new Capture(this, cameras[0])
//
// Use an index other than 0 if you don't like the resolution or framerate of 
// camera 0. While the following code won't do anything about a low framerate,
// it will scale whatever image it gets back from the camera to 640x480 with a
// frame rate of 30 frames per second.
//
cam = new Capture(this, 640, 480, 30);
cam.start();
}

size(640,480); // set the window size
camimg = new PImage(640,480); // create images to hold the original
transformed = new PImage(640,480); // and transformed data

// read the first frame from the camera
if (cam.available() == true) {
cam.read(); // read a new frame of data from the camera
camimg.copy(cam, 0, 0, 640, 480, 0, 0, 640, 480); 
}
}

void draw()
{

if (cam.available() == true) {
cam.read(); // read a new frame of data from the camera
camimg.copy(cam, 0, 0, 640, 480, 0, 0, 640, 480); 
}

////////////////////////////////////////////
// ONLY MAKE CHANGES BELOW THIS POINT
////////////////////////////////////////////

transformed.copy(camimg, 0, 0, 640, 480, 0, 0, 640, 480);

////////////////////////////////////////////
// ONLY MAKE CHANGES ABOVE THIS POINT
////////////////////////////////////////////

image(transformed,0,0); // draw the transformed image on the screen
}

修改方法很简单,加入下面红色代码即可:

////////////////////////////////////////////
// ONLY MAKE CHANGES BELOW THIS POINT
////////////////////////////////////////////

transformed.copy(camimg, 0, 0, 640, 480, 0, 0, 640, 480);

transformed.filter(GRAY);

////////////////////////////////////////////
// ONLY MAKE CHANGES ABOVE THIS POINT
////////////////////////////////////////////

参考:

1.http://tieba.baidu.com/p/4156597158

发表评论

电子邮件地址不会被公开。 必填项已用*标注