使用网络摄像头Feed进行颜色跟踪

时间:2015-10-25 17:31:42

标签: java animation processing video-processing

我正在尝试使用我网络摄像头的实时视频创建一个颜色跟踪鸟群。我被指示使用构造函数来创建一个可以独立工作并遵循视频周围特定颜色的.gif数组。 我做了一些研究,这是我得到的。现在我收到一个我不太懂的错误。有关代码的意图的早期虚拟示例,请参阅此.gif:Flock of birds

import processing.video.*;
import gifAnimation.*;
video = new Movie(); /// This is the line that gives me the error

// class
Birdy [] arrayOfBirds;
int numberOfBirds = 10;
class Birdy 
{
  //variables

  int numberOfBeaks;
  String birdName;
  color birdColor;
  PVector location;
    // constructor, allows you to make new Birds in the rest of the code
 // A constructor is part of the class 
  Birdy (int nob, String bname, color bColor, PVector loc) {
    numberOfBeaks = nob;
    birdName = bname;
    birdColor = bColor;
    location = loc;

  }
 //The bird appears
 void showBird() 
 {
    fill(birdColor);
    textSize(24);
    text(birdName, location.x, location.y);
    ellipse(location.x, location.y, 20, 20);
  }    
}

void setup() {

   size(640, 480);

  //fill the array Of Birds with new Birds
  arrayOfBirds = new Birdy[numberOfBirds];

  //to make 10 birds and put them in the array   
  for (int i = 0; i < numberOfBirds; i++)
  {                                           
    // each new bird needs its own set of parameters but will do this when i figure out how to work with this one first!
   arrayOfBirds[i]= new Birdy(2, "Tweety "+i, color(255-(i*25), i*25, 255), new PVector(i*40, i*40));
  }
}
void draw(int x, int y) {
 if (video.available()) {
   video.read();
   image(video, 0, 0, width, height); // Draw the webcam video onto the screen
   int colorX = 0; // X-coordinate of the closest in color video pixel
   int colorY = 0; // Y-coordinate of the closest in color video pixel
   float closestColor = 500; //we set this to be abritrarily large, once program runs, the first pixel it scans will be set to this value
   // Search for the closest in color pixel: For each row of pixels in the video image and
   // for each pixel in the yth row, compute each pixel's index in the video

     background(0);

  //show that first bird we called Tweety by calling the showBird() function on Tweety
  Tweety.showBird();

  //show all the birds in the array by calling the showBird() method on each object in the array
  for(int i = 0; i < arrayOfBirds.length; i++){
    arrayOfBirds[i].location = new PVector(x,y);
    arrayOfBirds[i].showBird();
  }

}
setup();
Gif loopingGif;
Capture video;
 size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480 // Uses the default video input ---- but i dont think it works
 video = new Capture(this, width, height, 30);
 video.start();
 noStroke();
 smooth();

  frameRate(10);
    loopingGif  = new Gif(this, "circle.gif");

    String [] animas = {};

   video.loadPixels();
   int index = 0;
   for (int y = 0; y < video.height; y++) {
     for (int x = 0; x < video.width; x++) {
       // Get the color stored in the pixel
       color pixelValue = video.pixels[index];
       // Determine the color of the pixel
       float colorProximity = abs(red(pixelValue)-27)+abs(green(pixelValue)-162)+abs(blue(pixelValue)-181); //select pixel
       // If that value is closer in color value than any previous, then store the
       // color proximity of that pixel, as well as its (x,y) location
       if (colorProximity < closestColor) {
         closestColor = colorProximity;
         closestColor=closestColor-10; //Once it "locks" on to an object of color, it wont let go unless something a good bit better (closer in color) comes along
         colorY = y;
         colorX = x;
       }
       index++;
     }
     draw(x,y);
   }
   image (loopingGif, colorX, colorY);
    loopingGif.play();
}here

1 个答案:

答案 0 :(得分:0)

您需要通过为其提供类型

来声明您的变量
Movie video = new Movie();

你还有其他一些奇怪的事情发生在这里。你为什么专门调用setup()函数?处理会自动为您完成。你还在草图底部的函数外面得到了一堆代码。也许您打算将该代码放在setup()函数中?

如果您仍然遇到错误,请修改您的问题以包含其确切的全文。

相关问题