Ciao 访客, welcome back to old school! :p
import codeanticode.gsvideo.*;int threshold;float posX;float posY;int setW=400,setH=400,videoW=160,videoH=120;GSCapture video;int[] previousFrame;void setup(){ size(400,400,P3D); frameRate(30); video=new GSCapture(this,width,height); video.start(); }void draw(){ if (video.available()) { video.read(); // Read the new frame from the camera video.loadPixels(); // Make its pixels[] array available int movementSum = 0; loadPixels(); int i = 0,II,xz=0,yz=0,Iz=0; for (int y = 0; y < video.height; y++) { for (int x = 0; x < video.width; x++) { color currColor = video.pixels[i]; color prevColor = previousFrame[i]; // Extract the red, green, and blue components from current pixel int currR = (currColor >> 16) & 0xFF; // Like red(), but faster (see p. 673) int currG = (currColor >> 8) & 0xFF; int currB = currColor & 0xFF; // Extract red, green, and blue components from previous pixel int prevR = (prevColor >> 16) & 0xFF; int prevG = (prevColor >> 8) & 0xFF; int prevB = prevColor & 0xFF; // Compute the difference of the red, green, and blue values int diffR = abs(currR - prevR); int diffG = abs(currG - prevG); int diffB = abs(currB - prevB); // Add these differences to the running tally movementSum += diffR + diffG + diffB; // Render the difference image to the screen pixels[i] = color(diffR, diffG, diffB); if (diffR+diffG+diffB>threshold) { II=1; } else { II=0; } xz += x*II; yz += y*II; Iz += II; // The following line is much faster, but more confusing to read //pixels[i] = 0xff000000 | (diffR << 16) | (diffG << 8) | diffB; // Save the current color into the 'previous' buffer previousFrame[i] = currColor; i++; } } if (movementSum >0 && Iz> 0) { posX=(video.width-xz/Iz)*setW/videoW; posY=yz/Iz*setH/videoH; }} background(0);rectMode(CENTER);fill(255,0,0);rect(posX,posY,20,20);}