Skip to content

Instantly share code, notes, and snippets.

@micromeeeter
Created June 20, 2017 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micromeeeter/dec670a48c0b5cc666ee159a27ec0861 to your computer and use it in GitHub Desktop.
Save micromeeeter/dec670a48c0b5cc666ee159a27ec0861 to your computer and use it in GitHub Desktop.
SFC 17年度春学期 画像処理プログラミング 中間発表
import processing.video.*;
import processing.opengl.*;
Capture video;
ArrayList<PVector> ball = new ArrayList<PVector>(); //ボール位置の可変長配列
float g_x, g_y; //抽出した青い部分の重心座標
int sum; //抽出した青いpixelの総数
float ball_z; //カメラからの距離をもとに生成するボールのz座標
float eye_x, eye_y, eye_z; //カメラ位置
void setup(){
size(640, 480, P3D);
frameRate(30);
colorMode(RGB);
background(180, 210, 220, 255);
video = new Capture(this, width, height, 30);
video.start();
eye_x = 0.0;
eye_y = 0.0;
}
void draw(){
if(video.available()){
video.read();
g_x = 0.0;
g_y = 0.0;
sum = 0;
for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
if(red(video.get(x,y))+50 < blue(video.get(x,y)) && green(video.get(x,y))+50 < blue(video.get(x,y))){
g_x += x;
g_y += y;
sum++;
}
}
}
g_x = g_x / sum; //重心の位置を計算
g_y = g_y / sum;
ball_z = sum / 150.0 - 100.0;
translate(width/2, height/2);
camera(eye_x, eye_y, 500, 0, 0, 0, 0, 1, 0);
stroke(255, 0, 0); //基準線
line(20, 0, 0, -5, 0, 0);
stroke(0, 255, 0);
line(0, 20, 0, 0, -5, 0);
stroke(0, 0, 255);
line(0, 0, 20, 0, 0, -5);
lights(); //ライトをenableに
fill(80, 90, 255);
noStroke();
for(int i = 0; i < ball.size(); i++){
pushMatrix();
translate(ball.get(i).x - width/2, ball.get(i).y - height/2, ball.get(i).z);
sphere(20);
popMatrix();
}
println(ball_z);
}
}
void keyPressed(){
if(key == ' '){
PVector v = new PVector(g_x, g_y, ball_z);
ball.add(v);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment