Einzelnen Beitrag anzeigen
Alt 14-11-2010, 17:27   #8 (permalink)
_geo_
~~~~~~~~~~~~
 
Benutzerbild von _geo_
 
Registriert seit: May 2002
Ort: AUSTRIA (OÖ)
Beiträge: 3.298
_img musst du durch bitmapDataFromVideo ersetzen.

Hab mal das Beispiel um ein Video erweitert:

ActionScript:
  1. /*
  2. mit USE_WEBCAM kann man auswählen ob man nun
  3. das Bild oder die Webcam nutzen möchte. Achtung falls
  4. du mehrere Webcams installiert hast musst du get
  5. getCamera() deine zu nutzende Webcam angeben ;)
  6. */
  7.  
  8. package
  9. {
  10.     import at.geoathome.cv.blob.ColorBlobDetector;
  11.     import flash.display.Bitmap;
  12.     import flash.display.BitmapData;
  13.     import flash.display.Sprite;
  14.     import flash.events.Event;
  15.     import flash.events.MouseEvent;
  16.     import flash.events.TimerEvent;
  17.     import flash.geom.Rectangle;
  18.     import flash.media.Camera;
  19.     import flash.media.Video;
  20.     import flash.utils.Timer;
  21.  
  22.    
  23.     /**
  24.      * @author Georg Kamptner
  25.      *
  26.      * License: Do as you wish, but don`t be evil ;)
  27.      */
  28.     public class Main extends Sprite
  29.     {
  30.         // image
  31.         [Embed(source="image.png", mimeType="image/png")]
  32.         protected var Img:Class;
  33.         protected var _img:Bitmap;
  34.         protected var _imgContainer:Sprite;
  35.         protected var _graphicsContainer:Sprite;
  36.        
  37.         // video / webcam
  38.         protected var _webcam:Camera;
  39.         protected var _video:Video;
  40.         protected var _bitmapDataFromVideo:BitmapData;
  41.         protected var _trackTimer:Timer;
  42.         protected var _colorFromVideoStorage:Object;
  43.        
  44.         public static var USE_WEBCAM:Boolean = false;
  45.        
  46.         // color blob detector
  47.         protected var _colorBlobDetector:ColorBlobDetector;
  48.         private var _roi:Rectangle;
  49.        
  50.         public function Main():void
  51.         {
  52.             if (stage) init();
  53.             else addEventListener(Event.ADDED_TO_STAGE, init);
  54.         }
  55.        
  56.         protected function init(e:Event = null):void
  57.         {
  58.             removeEventListener(Event.ADDED_TO_STAGE, init);
  59.            
  60.             if (!USE_WEBCAM)
  61.             { // image //
  62.                 // create image
  63.                 _img = new Img() as Bitmap;
  64.                
  65.                 // Since Bitmap is not interactive we need a Sprite
  66.                 // to handle mouse events.
  67.                 _imgContainer = new Sprite();
  68.                 _imgContainer.addChild(_img);
  69.                 addChild(_imgContainer);
  70.                
  71.                 // add mouse events
  72.                 _imgContainer.addEventListener(MouseEvent.CLICK, detectColorFromImageUnderMouse);
  73.             }
  74.             else
  75.             { // webcam //
  76.                 _webcam = Camera.getCamera();
  77.                 _webcam.setMode(320, 240, 20, false);
  78.                 _video = new Video();
  79.                 _video.attachCamera(_webcam);
  80.                
  81.                 // Since Video is not interactive we need a Sprite
  82.                 // to handle mouse events.
  83.                 _imgContainer = new Sprite();
  84.                 _imgContainer.addChild(_video);
  85.                 addChild(_imgContainer);
  86.                
  87.                 // add mouse events to select a color
  88.                 _colorFromVideoStorage = { r:255, g:255, b:255 };
  89.                 _imgContainer.addEventListener(MouseEvent.CLICK, getColorFromVideo);
  90.                
  91.                 _bitmapDataFromVideo = new BitmapData(_video.width, _video.height);
  92.                
  93.                 _trackTimer = new Timer(1000 / 20);
  94.                 _trackTimer.addEventListener(TimerEvent.TIMER, detectColorInVideo);
  95.                 _trackTimer.start();
  96.             }
  97.            
  98.             // draw the found regions into this sprite
  99.             _graphicsContainer = new Sprite();
  100.             addChild(_graphicsContainer);
  101.            
  102.             // detector (this is it) - debug version needs a sprite to draw into
  103.             _colorBlobDetector = new ColorBlobDetector( 2, 2000, 2, 2000);
  104.             _colorBlobDetector.setConfig(2.0, 6.0, 9.0);
  105.             _colorBlobDetector.setThresholds(8.0, 0.20, 0.0, 0.0);
  106.            
  107.             if (!USE_WEBCAM)
  108.             {
  109.                 _roi = new Rectangle(0, 0, _img.width, _img.height);
  110.             }
  111.             else
  112.             {
  113.                 _roi = new Rectangle(0, 0, _bitmapDataFromVideo.width, _bitmapDataFromVideo.height);
  114.             }
  115.         }
  116.        
  117.         private function getColorFromVideo(e:MouseEvent):void
  118.         {
  119.             // get color unter mousecursor
  120.             var rgb:uint = _bitmapDataFromVideo.getPixel(_video.mouseX, _video.mouseY);
  121.             _colorFromVideoStorage['r'] = rgb >> 16 & 0xff;
  122.             _colorFromVideoStorage['g'] = rgb >> 8 & 0xff;
  123.             _colorFromVideoStorage['b'] = rgb & 0xff;
  124.         }
  125.        
  126.         private function detectColorInVideo(e:TimerEvent):void
  127.         {
  128.             // draw video into bitmapdata
  129.             _bitmapDataFromVideo.draw(_video);
  130.            
  131.             // detect blobs
  132.             _colorBlobDetector.detectRGB(_bitmapDataFromVideo,  _colorFromVideoStorage['r'],
  133.                                                                 _colorFromVideoStorage['g'],
  134.                                                                 _colorFromVideoStorage['b'], _roi);
  135.            
  136.             // draw regions
  137.             _graphicsContainer.graphics.clear();
  138.             for (var i:uint = 0; i < _colorBlobDetector.numOfBlobs; ++i)
  139.             {
  140.                 _graphicsContainer.graphics.lineStyle(2, 0xff0000);
  141.                 _graphicsContainer.graphics.drawRect(_colorBlobDetector.blobs[i].x,
  142.                                                 _colorBlobDetector.blobs[i].y,
  143.                                                 _colorBlobDetector.blobs[i].width,
  144.                                                 _colorBlobDetector.blobs[i].height);
  145.             }
  146.            
  147.             // debug trace
  148.             trace("Blobs found: " + _colorBlobDetector.numOfBlobs +
  149.             ", Duration in MS: " + _colorBlobDetector.detectionDuration +
  150.             " in FPS: " + int(1000 / _colorBlobDetector.detectionDuration));
  151.  
  152.         }
  153.        
  154.         protected function detectColorFromImageUnderMouse(e:MouseEvent):void
  155.         {
  156.             // get color under mousecursor
  157.             var rgb:uint = _img.bitmapData.getPixel(_img.mouseX, _img.mouseY);
  158.             var r:uint = rgb >> 16;
  159.             var g:uint = rgb >> 8 & 0xff;
  160.             var b:uint = rgb & 0xff;
  161.            
  162.             // detect blobs
  163.             _colorBlobDetector.detectRGB(_img.bitmapData, r, g, b, _roi);
  164.            
  165.             // draw regions
  166.             _graphicsContainer.graphics.clear();
  167.             for (var i:uint = 0; i < _colorBlobDetector.numOfBlobs; ++i)
  168.             {
  169.                 _graphicsContainer.graphics.lineStyle(2, 0xff0000);
  170.                 _graphicsContainer.graphics.drawRect(_colorBlobDetector.blobs[i].x,
  171.                                                 _colorBlobDetector.blobs[i].y,
  172.                                                 _colorBlobDetector.blobs[i].width,
  173.                                                 _colorBlobDetector.blobs[i].height);
  174.             }
  175.            
  176.             // debug trace
  177.             trace("Blobs found: " + _colorBlobDetector.numOfBlobs +
  178.             ", Duration in MS: " + _colorBlobDetector.detectionDuration +
  179.             " in FPS: " + int(1000 / _colorBlobDetector.detectionDuration));
  180.         }
  181.  
  182.     }
  183.    
  184. }
__________________
--- :P ---

Blog
Bei unerwünschten Nebenwirkungen zerreißen Sie die Packungsbeilage oder erschlagen ihren Arzt oder Apotheker

Geändert von _geo_ (14-11-2010 um 17:53 Uhr)
_geo_ ist offline   Mit Zitat antworten