在actionscript中为视频添加噪音

时间:2011-05-25 20:31:21

标签: actionscript-3 video augmented-reality noise flartoolkit

在开始之前,如果我要问的是被视为超级新手问题,我想道歉。

所以我正在尝试使用actionscript3创建一个简单的AR场景的Lee Brimelow教程。 (http://blog.theflashblog.com/?p=901) 考虑到我之前从未使用Adobe Flex Builder创建过某些东西,因此效果非常好。 (我在看到这些教程后立即安装了Flex Builder)

这是源代码

package {

    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.media.Camera;
    import flash.media.Video;
    import flash.utils.ByteArray;

    import org.libspark.flartoolkit.core.FLARCode;
    import org.libspark.flartoolkit.core.param.FLARParam;
    import org.libspark.flartoolkit.core.raster.rgb.FLARRgbRaster_BitmapData;
    import org.libspark.flartoolkit.core.transmat.FLARTransMatResult;
    import org.libspark.flartoolkit.detector.FLARSingleMarkerDetector;
    import org.libspark.flartoolkit.support.pv3d.FLARBaseNode;
    import org.libspark.flartoolkit.support.pv3d.FLARCamera3D;
    import org.papervision3d.lights.PointLight3D;
    import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
    import org.papervision3d.materials.utils.MaterialsList;
    import org.papervision3d.objects.primitives.Cube;
    import org.papervision3d.render.BasicRenderEngine;
    import org.papervision3d.scenes.Scene3D;
    import org.papervision3d.view.Viewport3D;

    [SWF(width="640", height="480", frameRate="30", backgroundColor="#FFFFFF")]

    public class FLARdemo extends Sprite
    {
        [Embed(source="pat1.pat", mimeType="application/octet-stream")]
        private var pattern:Class;

        [Embed(source="camera_para.dat", mimeType="application/octet-stream")]
        private var params:Class;

        private var fparams:FLARParam;
        private var mpattern:FLARCode;
        private var vid:Video;
        private var cam:Camera;
        private var bmd:BitmapData;
        private var raster:FLARRgbRaster_BitmapData;
        private var detector:FLARSingleMarkerDetector;
        private var scene:Scene3D;
        private var camera:FLARCamera3D;
        private var container:FLARBaseNode;
        private var vp:Viewport3D;
        private var bre:BasicRenderEngine;
        private var trans:FLARTransMatResult;

        public function FLARdemo()
        {
            setupFLAR();
            setupCamera();
            setupBitmap();
            setupPV3D();
            addEventListener(Event.ENTER_FRAME, loop);
        }

        private function setupFLAR():void
        {
            fparams = new FLARParam();
            fparams.loadARParam(new params() as ByteArray);
            mpattern = new FLARCode(16, 16);
            mpattern.loadARPatt(new pattern());
        }

        private function setupCamera():void
        {
            vid = new Video(640, 480);
            cam = Camera.getCamera();
            cam.setMode(640, 480, 30);
            vid.attachCamera(cam);
            addChild(vid);
        }

        private function setupBitmap():void
        {
            bmd = new BitmapData(640, 480);
            bmd.draw(vid);
            raster = new FLARRgbRaster_BitmapData(bmd);
            detector = new FLARSingleMarkerDetector(fparams, mpattern, 80);
        }

        private function setupPV3D():void
        {
            scene = new Scene3D();
            camera = new FLARCamera3D(fparams);
            container = new FLARBaseNode();
            scene.addChild(container);

            var pl:PointLight3D = new PointLight3D();
            pl.x = 1000;
            pl.y = 1000;
            pl.z = -1000;

            var ml:MaterialsList = new MaterialsList({all: new FlatShadeMaterial(pl)});

            var Cube1:Cube = new Cube(ml, 30, 30, 30);                  
            var Cube2:Cube = new Cube(ml, 30, 30, 30);
            Cube2.z = 50
            var Cube3:Cube = new Cube(ml, 30, 30, 30);
            Cube3.z = 100

            container.addChild(Cube1);
            container.addChild(Cube2);
            container.addChild(Cube3);

            bre = new BasicRenderEngine();
            trans = new FLARTransMatResult();

            vp = new Viewport3D;
            addChild(vp);
        }

        private function loop(e:Event):void
        {
            bmd.draw(vid);

            try
            {
                if(detector.detectMarkerLite(raster, 80) && detector.getConfidence() > 0.5)
                {
                    detector.getTransformMatrix(trans);
                    container.setTransformMatrix(trans);
                    bre.renderScene(scene, camera, vp);
                }
            }
            catch(e:Error){}
        }
    }
}

我要问的是: 我们可以在最终的视频输出中添加一些噪音吗?我试图通过分别添加噪音来找出PSNR。 我可以在噪音和视频之间进行一些卷积吗?

哦,顺便说一句,我正在为大学的任务做这件事。我的教授希望我解释一下FlarToolKit的工作原理。 (详见Matrix Projections,通过迭代方法获取错误等)

谢谢。 Prama

1 个答案:

答案 0 :(得分:1)

动态创建细粒度噪声在计算上非常昂贵,因此我会生成低分辨率噪声并进行扩展。在项目中,只需在addEventListener调用之后添加位图设置代码,然后添加单行以生成活动循环的噪声。

package 
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.BlendMode;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.Matrix;
    import flash.media.Camera;
    import flash.media.Video;
    import flash.utils.getTimer;

    public class NoiseMain extends Sprite
    {
        protected static const VID_WIDTH : uint = 640;
        protected static const VID_HEIGHT : uint = 480;

        protected static const NOISE_SCALE_X : Number = 4;
        protected static const NOISE_SCALE_Y : Number = 2;

        protected var _vid : Video;
        protected var _noise : BitmapData;
        protected var _composite : BitmapData;
        protected var _matrix : Matrix;

        public function NoiseMain()
        {
            // We're creating a webcam outlet, but not adding it to the stage since we want to post-process the image.
            _vid = new Video(VID_WIDTH, VID_HEIGHT);
            var cam : Camera = Camera.getCamera();
            cam.setMode(VID_WIDTH, VID_HEIGHT, 30);
            _vid.attachCamera(cam);     

            var w : uint = Math.ceil(VID_WIDTH / NOISE_SCALE_X);
            var h : uint = Math.ceil(VID_HEIGHT / NOISE_SCALE_Y);

            _noise = new BitmapData(w, h, false, 0xFFFFFF);
            _composite = new BitmapData(VID_WIDTH, VID_HEIGHT, false, 0x000000);
            var bmp : Bitmap = new Bitmap(_composite);
            addChild(bmp);

            _matrix = new Matrix(NOISE_SCALE_X, 0, 0, NOISE_SCALE_Y);

            stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
        }

        protected function enterFrameHandler(evt : Event) : void
        {
            _noise.noise(getTimer(), 204, 255, 7, true);
            _composite.lock();
            _composite.draw(_vid);
            _composite.draw(_noise, _matrix, null, BlendMode.MULTIPLY); 
            _composite.unlock();        
        }
    }
}
编辑:我修改了我的例子,以便更符合你的目标。您可以调整噪声标度常数以更改噪声的“纹理”以及混淆噪声的强度(噪声()方法的第二和第三个参数)以及通过更改BlendMode来显示噪声的视觉外观在第二次抽奖()中。)