【问题标题】:color tracking using webcam feed使用网络摄像头进行颜色跟踪
【发布时间】:2015-10-25 17:31:41
【问题描述】:

我正在尝试使用来自我的网络摄像头的实时视频创建一个颜色跟踪鸟群。我被指示使用构造函数创建一个 .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

【问题讨论】:

  • 您在类之外声明变量(videoarrayOfBirdsnumberOfBirds)。这是语法错误。
  • 另外,krizz:对于未来,你应该考虑更有意义的问题标题。
  • 我真的很想知道,当你不了解一门语言的基础知识时,你们是如何开始研究这些高级概念的。
  • @Kayman 这是处理,而不是 Java。处理变量不需要在类内部。 (更准确地说,它们被 Processing 编译器自动放入一个类中。)

标签: java animation processing video-processing


【解决方案1】:

你需要通过给它一个type来声明你的变量:

Movie video = new Movie();

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-24
    • 1970-01-01
    • 2011-01-17
    • 1970-01-01
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多