WebCamera中的边缘检测与碰撞

人机交互 everyinch 3737℃ 0评论

提取WebCamera视频图像中的边缘并与之交互的示例从早起的雪花飘落开始,这方面的演示已经越来越多。本示例演示掉落的小球与视频图像边缘碰撞并反弹的效果
源代码:WebCamera_Collision

package{
import com.bit101.components.Panel;
import com.coreyoneil.collision.CollisionList;
import com.quasimondo.bitmapdata.CameraBitmap;

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.filters.BlurFilter;
import flash.filters.ColorMatrixFilter;
import flash.filters.ConvolutionFilter;
import flash.geom.Point;
import flash.geom.Rectangle;

[SWF(frameRate=”31″)]
public class WebCamera_Collision extends Sprite{
private var camera:CameraBitmap;
private var edgeCamera:CameraBitmap;
private var edgeBmpd:BitmapData;
private var edgeBmp:Bitmap;

private var w:int = 400;
private var h:int = 300;

private var collisionList:CollisionList;
private var bubbles:Array;
private var gravity:Number = 1;
private var friction:Number = 0.95;

private var panel:Panel;

public function WebCamera_Collision(){
if(stage) init();
else addEventListener(Event.ADDED_TO_STAGE,init);
}

private function init(e:Event = null):void{
initStage();

edgeCamera = new CameraBitmap(w,h,15,true);
edgeCamera.addEventListener(Event.RENDER,onRenderCamera);
edgeBmpd = edgeCamera.bitmapData;
edgeBmp = new Bitmap(edgeBmpd);

camera = new CameraBitmap(w,h,15,true);
addChild(new Bitmap(camera.bitmapData));

var refBmp:Bitmap = new Bitmap(edgeBmpd);
refBmp.x = w;
addChild(refBmp);

collisionList = new CollisionList(edgeBmp);
collisionList.excludeColor(0xff000000,255,0,0,0);

bubbles = new Array();
for(var i:int=0;i<20;i++){ var bubble:Bubble = new Bubble(14,0xFFB6DD); bubble.x = Math.random()*w; bubble.vx = bubble.vy = 0; bubbles.push(bubble); addChild(bubble); collisionList.addItem(bubble); } addEventListener(Event.ENTER_FRAME,onEnterframe); } private function onRenderCamera(e:Event):void{ var ca:Number = 1.2; var cb:Number = -40; var contrastMatrix:Array = [ca, ca, ca, 0, cb, ca, ca, ca, 0, cb, ca, ca, ca, 0, cb, 0, 0, 0, 1, 0]; var rect:Rectangle = new Rectangle(0,0,w,h); var pt:Point = new Point(); edgeBmpd.applyFilter(edgeBmpd,rect,pt,new BlurFilter(5,5,1)); edgeBmpd.applyFilter(edgeBmpd,rect,pt,new ConvolutionFilter(3, 3, [0, 3, 0, 0.5, -6, 0.5, 0, 2, 0], 0.2, 0, true, true)); edgeBmpd.applyFilter(edgeBmpd,rect,pt,new BlurFilter(5,2,1)); edgeBmpd.applyFilter(edgeBmpd,rect,pt,new ColorMatrixFilter(contrastMatrix)); } private function initStage():void{ stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.NO_SCALE; } private function onEnterframe(e:Event):void{ var collisions:Array = collisionList.checkCollisions(); var numbers:Number = collisions.length; for(i = 0; i < numbers; i++){ var collision:Object = collisions[i]; var angle:Number = collision.angle; var overlap:int = collision.overlapping.length; var bubble:Bubble = collision.object1; bubble.rotation = angle*180/Math.PI; var sin:Number = Math.sin(angle); var cos:Number = Math.cos(angle); var vx0:Number = bubble.vx * cos + bubble.vy * sin; var vy0:Number = bubble.vy * cos - bubble.vx * sin; vx0 = ((bubble.mass - 50) * vx0) / (bubble.mass + 50); bubble.vx = vx0 * cos - vy0 * sin; bubble.vy = vy0 * cos + vx0 * sin; bubble.vx -= cos * overlap / bubble.radius; bubble.vy -= sin * overlap / bubble.radius; } for(var i:uint = 0; i < 20; i++){ bubble = bubbles[i]; bubble.vy += gravity; bubble.vy *= friction; bubble.vx *= friction; bubble.y += bubble.vy; bubble.x += bubble.vx; if(bubble.y > h){
bubble.y = 0;
bubble.vy = bubble.vx = 0;
bubble.x = Math.random() * w;
}
else if(bubble.x > w){
bubble.x = w – bubble.width/2;
bubble.vx *= -1;
}
else if(bubble.x < 0){ bubble.x = bubble.width/2; bubble.vx *= -1; } } } } } [/code] 运行效果:

分享&收藏

转载请注明:陈童的博客 » WebCamera中的边缘检测与碰撞

喜欢 (0)
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
'; } if( dopt('d_footcode_b') ) echo dopt('d_footcode'); ?>