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

问题描述:

我想创建一个颜色跟踪鸟群,使用来自我的摄像头的实时视频。我被指示使用一个构造函数来创建一个可以独立工作并遵循视频周围特定颜色的.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

您正在类的外部声明变量('video','arrayOfBirds','numberOfBirds')。这是一个语法错误。 – Kayaman

+3

另外,krizz:为了将来,你应该考虑更有意义的问题标题。 –

+0

我真的很想知道,如果你不了解语言的基础知识,你们是如何开始研究这些高级概念的。 – CrakC

你需要给它一个来声明变量:

Movie video = new Movie(); 

你在这里还有其他奇怪的事情发生。你为什么要专门拨打setup()功能?处理自动为你做。你在草图底部的函数外还有一堆代码。也许你打算把这个代码放在setup()函数中?

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