在 Actionscript 中向视频添加噪音

发布于 2024-11-09 11:01:00 字数 4777 浏览 5 评论 0原文

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

所以我正在尝试 Lee Brimelow 的关于使用 actionscript3 创建简单 AR 场景的教程。 (http://blog.theflashblog.com/?p=901) 考虑到我以前从未使用 Adob​​e 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 到底是如何工作的。 (详细信息如矩阵投影、通过迭代方法获取误差等)

谢谢。 普拉玛

Before I begin, I would like to apologize if what I'm going to ask is to be considered as super newbie question.

so I was trying out Lee Brimelow's tutorial on creating a simple AR scene using actionscript3. (http://blog.theflashblog.com/?p=901)
It worked out pretty well, considering I have never created something out of Adobe Flex Builder before. (I installed Flex Builder right after I saw those tutorial)

here is the source code

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){}
        }
    }
}

what I am asking is :
is it possible for us to add some noise in the final video output? I'm trying to find out the PSNR by adding noises respectively.
can I do some convolution between the noise and the video?

oh btw I'm doing this for my assignment in college. My prof wanted me to explain how exactly the FlarToolKit works. (details such as Matrix Projections, Obtaining Errors by doing Iterative Method, etc.)

Thank you.
Prama

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幼儿园老大 2024-11-16 11:01:00

动态创建细粒度噪声的计算成本很高,因此我生成低分辨率噪声并将其放大。在您的项目中,只需在 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 来扰乱噪声的强度(noise() 方法的第二个和第三个参数)和噪声的视觉外观在第二次 draw() 调用中)。

Creating fine-grained noise dynamically is computationally expensive, so I generate low-res noise and scale it up. In your project, simply add the bitmap setup code after the addEventListener call and then add the single line to generate the noise to your activity loop.

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();        
        }
    }
}

EDIT: I've revised my example to be more in line with what you're trying to do. You can tweak the noise scale constants to change the "texture" of the noise as well as messing with the strength of the noise (the 2nd and 3rd arguments to the noise() method) and the visual appearance of the noise by changing the BlendMode in the 2nd draw() call).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文