2015-10-25 72 views
-2

我想創建一個顏色跟蹤鳥羣,使用來自我的攝像頭的實時視頻。我被指示使用一個構造函數來創建一個可以獨立工作並遵循視頻周圍特定顏色的.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

回答

0

你需要給它一個來聲明變量:

Movie video = new Movie(); 

你在這裏還有其他奇怪的事情發生。你爲什麼要專門撥打setup()功能?處理自動爲你做。你在草圖底部的函數外還有一堆代碼。也許你打算把這個代碼放在setup()函數中?

如果仍然出現錯誤,請編輯您的問題以包含其確切全文。