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

Processing导入模型

最近玩玩 Processing , 尝试导入一个 DXF 的模型,OBJ 格式的. 使用 Processing 加载之。具体做法是这样的:

1. 下载 OBJLoader 库 【参考1】
2. 把这个库放在 Processing -> File -> Preferences -> Sketchbook location 指定的位置中,例如:

prc1

特别注意,默认目录中可能有空格或者中文,那么请你指定一个不含有中文或者空格的目录,像我一样,放在 D:\ 下面最好。就是下面这个样子

prc2

prc3

这个库可以直接去【参考1】给出的网站上下载,也可以用 Tools -> Add Tool 调出 Tool Manager,他会自动下载列表,你选择 ObjLoader 进行安装(可能是 GFW 的缘故,这个功能不稳定)

toolm

3.安装好之后,可以编译 ObjLoader 中 Examples 下面的例子,确保安装正确 (如果出现错误,并且错误不是 ObjLoader 导致的,那么请换另外的例子,有些例子用到了第三方的库)

4.最后编写自己的程序。从 Sketch -> Add Files 中导入 OBJ 模型,再输入下面的代码

import saito.objloader.*;

OBJModel model;

void setup() {
  size(600,600,P3D);
  model= new OBJModel(this);
  model.load("teapot.obj");
  model.setDrawMode(POLYGON);
  noStroke();
}  

void draw() {
  background(0);
  lights();
  pushMatrix();
  translate(width/2,height,-width);
  rotateY(map(mouseX,0,width,-PI,PI));
  rotateX(PI/4);
  scale(6.0);
  model.draw();
  popMatrix();
}  

 

最终运行结果如下

teapot

完整的代码下载

sketch_150823b

参考:

1.https://processing.org/reference/libraries/ 上提到的 OBJLoader by Tatsuya Saito and Matt Ditton .OBJ 3D model file loader