0 Replies Latest reply on Oct 21, 2012 6:09 AM by LostInCrossCompilation

    ConvolutionFilter with big matrices

    LostInCrossCompilation Level 1

      I'm trying to use convolutionfilter to create a Gaussian Blur effect. It works well only for small matrices (or vectors, when I divided it in H/V passes) but when I choose a large radius, say, 20, for the transformation [matrix side=(2radius+1] I get a low quality image as well a darkening of the image.

       

      Here's the chunk for the code divided in 2 HV passes:

       

       

      
      
      
      var gaussRadius:int=3; // in pixels
      
      
      
      
      var gaussSigma:Number=gaussRadius/2; // at a distance of 2 sigmas, the influence is very low (2%?)
      
      
      
      
      
      
      ///
      
      
      
      
      
      
      
      
      
      var matrixSide:int=((gaussRadius*2)+1);
      
      
      
      
      var gaussianBlurVector:Array = new Array(matrixSide);
      
      
      
      
      var tmpNumber:Number;
      
      
      
      
      { // fill matrix
      
      
      
      
      
      var ixcount:int=0;
      
      
      
      
      
      tmpNumber=0;
      
      
      
      
      
      for (var xc:int=-gaussRadius; xc<=gaussRadius; xc++)
      
      
      
      
      
      {
      
      
      
      
      
      
      var g1:Number=(1/Math.sqrt(2*Math.PI*(gaussSigma*gaussSigma)))
      
      
      
      
      
      
      var g2:Number=Math.pow( Math.E, (-((xc*xc))/(2*gaussSigma*gaussSigma)) );
      
      
      
      
      
      
      var g:Number=g1*g2;
      
      
      
      
      
      
      gaussianBlurVector[ixcount]=(new Number(g));
      
      
      
      
      
      
      tmpNumber+=g;
      
      
      
      
      
      
      ixcount++; 
      
      
      
      
      
      }
      
      
      
      
      }
      
      
      
      
      { // normalize matrix
      
      
      
      
      
      var checkSum:Number=0;
      
      
      
      
      
      for (var i:int=0; i<gaussianBlurVector.length; i++)
      
      
      
      
      
      {
      
      
      
      
      
      
      gaussianBlurVector[i]/=tmpNumber;
      
      
      
      
      
      
      checkSum+=gaussianBlurVector[i];
      
      
      
      
      
      
      trace("vector(", i, ")= ", gaussianBlurVector[i]);
      
      
      
      
      
      }
      
      
      
      
      
      
      
      
      
      
      trace("checksum: ",checkSum);
      
      
      
      
      }
      
      
      
      
      var gaussianBlurHConvolutionMatrix:ConvolutionFilter= new ConvolutionFilter(matrixSide, 1, 
      
      
      
      
      
      gaussianBlurVector, 1, 0, true, true, 0x000000, 0);
      
      
      
      
      var gaussianBlurVConvolutionMatrix:ConvolutionFilter= new ConvolutionFilter(1, matrixSide, 
      
      
      
      
      
      gaussianBlurVector, 1, 0, true, true, 0x000000, 0);
      
      
      
      
      
      
      
      
      
      this.TestImageControl.source=testImage.bitmapData;
      
      
      
      
      this.TestImageControl.filters=[gaussianBlurHConvolutionMatrix, gaussianBlurVConvolutionMatrix];
      
      
      
      
      trace("Done. gaussianBlurMatrix(", gaussianBlurVector.length, ")");