Processing即时光绘画


由于暑假去了欧洲旅游,所有16年上半年的东西直到现在才总结。怎么说能,先来说说用Processing的即时光绘画吧。


 
这是一个给大悦城的中庭做的一个装置项目,主要原理就是在黑暗的空间里processing识别到发光物体后把光的颜色记录下来,达到使用光绘画的效果,以前的延时摄影玩过类似的玩意,但那个并不是实时的,你不能即时的看到你挥舞的光棒呈现的效果,必须等曝光时间结束才可以。
原理其实还是前几章那个capture捕获的基本知识,只是对其进行了拓展。
 

源代码:

/*
Copyright (c) 2016 LI Ziyang. All rights reserved.
本程序已在copyright.org和cpcc(中国版权保护中心)备案
如未经作者授权而用于商业活动,将追究其法律责任
-------------------------------------------------
*/
import processing.video.*;
//---------可修改的参数----------
int pixelsSize = 10;//像素分辨率(越低越清晰)
int brightVal = 80;//光明暗识别度(越小识别度高,噪点高;越大识别度低,噪点低)
int paintTimeDefault = 120;//绘画时间(单位为秒)
int photoTimeDefault = 20;//拍照时间(单位为秒)
int paintAphla = 10;
//---------------------------------------------------
//--------------------以下代码请勿修改---------------
PImage fimg;
Capture video;
PImage backgroundImage;
PImage img;
boolean Printing = false;
boolean mouseClick = false;
PFont f;
int rTime=2;
int timeDisplay = paintTimeDefault;
int SystemFrame = 30;
int photoTips = photoTimeDefault;
int PaintStep=0;
int point;
void setup(){
  size(1280,720);
  noCursor();
  //size(720,360);
  frameRate(30);
  f = createFont("MicrosoftYaHei-48",48,true);
  colorMode(RGB, 255, 255, 255, 100);
  background(0);
  saveFrame("Temp/TempImg"+PaintStep+".jpg");
  //mac系统读取摄像头数组数据
  String [] cameras = Capture.list();
  if(cameras.length == 0){
    println("There are no cameras for capture");
    exit();
  }else{
    println("Available cameras:");
    for(int i=0; i<cameras.length; i++){
      println(cameras[i]);
    }
    video = new Capture(this,width,height,cameras[0]);
    video.start();
  }
  //读取默认0号摄像头
  //video = new Capture(this,width,height);
  //video.start();
  //smooth();
  //fill(255, 153);
  //backgroundImage = createImage(video.width,video.height,RGB);
}
void draw(){
  //fill(0,0,0,10);
  //rect(640,360,1280,720);
  //background(0,10);
  //image(backgroundImage,0,0,1280,720);
  /*
  Printing=false;
  if(mousePressed && mouseButton == LEFT){
    mouseClick = true;
    Printing = true;
  }
    if(mouseClick == true){
      PaintStep++;
      saveFrame("Temp/TempImg"+PaintStep+".jpg");
      mouseClick=false;
    }
  */
  //非绘画模式刷新背景
  fimg = loadImage("monkey.png");
  /*
  textSize(49);
  // white float frameRate
  fill(255);
  text(frameRate,20,20);
  */
  if(Printing==false&&mouseClick==false){
    rTime--;
    if(rTime<=0){
      img = loadImage("Temp/TempImg"+PaintStep+".jpg");
      tint(255,15);
      image(fimg,50,50);
      image(img,0,0);
      rTime = 2;
    }
      fill(0,0,0,100);
      rect(640,25,1280,50);//mark
  }
  //读取像素
  if(video.available()){
    video.read();
    video.loadPixels();
    float r,g,b;
    for (int y = 50; y < video.height; y+=pixelsSize) {
      for (int x = 0; x < video.width; x+=pixelsSize) {
        int loc = x+y*video.width;
        r = red(video.pixels[loc]);
        g = green(video.pixels[loc]);
        b = blue(video.pixels[loc]);
        color c = color(r, g, b, paintAphla);
        if(brightness(c)>brightVal){
            pushMatrix();
            translate(width-x, y);
            //rotate((2 * PI * brightness(c) / 255.0));
            rectMode(CENTER);
            fill(c);
            noStroke();
            rect(0,0,pixelsSize,pixelsSize);
            popMatrix();
        }
      }
    }
  }
  //saveFrame("Print"+".jpg");
  /*
  if(Printing == true){
    backgroundImage.copy(0,0,1280,720,0,0,1280,720);
    //backgroundImage.updatePixels();
    image(backgroundImage,0,0,1280,720);
    //saveFrame("TempImg"+".jpg");
  }
  */
  //帧递减
  SystemFrame--;
  if(SystemFrame<=0){
    timeDisplay--;
    photoTips--;
    SystemFrame=30;
  }
  //绘画时间递减
  if(timeDisplay>0){
    photoTips=photoTimeDefault;
    fill(0,0,0,100);
    rect(1180,25,120,50);
    fill(255);
    textFont(f,12);
    textSize(24);
    text("绘画剩余时间:",980,30);
    text(timeDisplay,1160,30);
  }
  //绘画时间结束弹出20秒拍照提示
  if(timeDisplay<=0){
    //20秒提示拍照
    fill(0,0,0,100);
    rect(1180,25,120,48);
    fill(255);
    textFont(f,12);
    textSize(24);
    text("拍照剩余时间:",980,30);
    text(photoTips,1160,30);
    if(photoTips<=0){
      /*
      for(int i=0;i<150;i++){
        fill(0,0,0,100);
        rect(640,360,1280,720);
        saveFrame("Temp/TempImg"+i+".jpg");
      }
      */
      timeDisplay=paintTimeDefault;
      PaintStep = 0;
      fill(0,0,0,100);
      rect(640,360,1280,720);
    }
  }
  //mark 4,3
  print(Printing);
  print("--");
  print(frameRate);
  print("--");
  println(PaintStep);
  //image(backgroundImage,0,0,1280,720);
  //Printing = true;
}
void mousePressed(){
   //fill(0);
   //rect(640,38,1280,76);//mark
  if(mouseButton == LEFT){
    mouseClick = true;
    Printing = true;
    textFont(f,12);
    textSize(24);
    fill(255,0,0,100);
    text("开始作画",660,32);
    ellipse(640,23,30,30);
    fill(0);
    ellipse(640,23,25,25);
    fill(255,0,0,100);
    ellipse(640,23,20,20);
     //PImage paintStart = loadImage("paintStart.png");
      //tint(255,100);
      //image(paintStart,0,0);
  }
  if(mouseButton == RIGHT){
    if(PaintStep>0){
      PImage PrevImg = loadImage("Temp/TempImg"+PaintStep--+".jpg");
      //tint(255,100);
      image(PrevImg,0,0);
    }
  }
}
void mouseReleased(){
  if(mouseButton == LEFT){
    if(mouseClick == true){
      rTime=2;
      PaintStep++;
      saveFrame("Temp/TempImg"+PaintStep+".jpg");
      mouseClick=false;
      Printing = false;
    }
  }
}

《“Processing即时光绘画”》 有 2 条评论

  1. 你好 这个效果就是您图片中展示的那样吗?为什么我测试的代码光效记录不到屏幕上呢?是和您说的可修改的参数有关系吗?需要根据环境光来调节?

发表回复

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