Optimized edge detection algorithm.

I optimized the edge detection (sobel) algorithm found here a little. Still there is a lot potential for optimizing things. You can find tipps in joa’s wiki

Here is my code

private function detectEdges(bmd : BitmapData) : void
{
 
 var myGausianFilter : ConvolutionFilter =
 new ConvolutionFilter(5, 5, 
 [2,4,5,4,2,4,9,12,9,4,5,12,15,12,5,4,9,12,9,4,2,4,5,4,2],
 115);
 
 // Edge Data
 findEdges();
 
 function findEdges() : void
 {	
  //Apply Smoothing Filter
  bmd.lock();
  bmd.applyFilter(bmd, bmd.rect, new Point(0, 0), myGausianFilter);
 
  //Create New Bitmap to hold edge data
  ourEdgeData = new BitmapData(bmd.width, bmd.height, false);
 
  var gx : int;;
  var gy : int;
  var gray : uint;
 
  //Loop through original data and calculate edges
  for(var w : int = 0;w < bmd.width; w++)
  {
   for(var h : int = 0;h < bmd.height; h++)
   {
    pV0 = int(bmd.getPixel(w, h - 1)/ uint(0xffffff)*255);
    pV45 = int(bmd.getPixel(w + 1, h - 1)/ uint(0xffffff)*255);
    pV90 = int(bmd.getPixel(w + 1, h)/ uint(0xffffff)*255);
    pV35 = int(bmd.getPixel(w + 1, h + 1)/ uint(0xffffff)*255);
    pV80 = int(bmd.getPixel(w, h + 1)/ uint(0xffffff)*255);
    pV225 = int(bmd.getPixel(w - 1, h + 1)/ uint(0xffffff)*255);
    pV270 = int(bmd.getPixel(w - 1, h)/ uint(0xffffff)*255);			
    pV315 = int(bmd.getPixel(w - 1, h - 1)/ uint(0xffffff)*255);	 
 
    gx = (pV45+(pV90*2)+pV35)-(pV315+(pV270*2)+pV225);
    gy = (pV315+(pV0*2)+pV45)-(pV225+(pV80*2)+pV35);
    gray = Math.abs(gx) + Math.abs(gy);
 
    // Check to see if values aren't our of bounds
    if(gray > 255)
     gray = 255;
 
    if(gray < 0)
	  gray = 0;
 
    // Build New Pixel
    newPixelValue = (gray << 16)+(gray << 8)+(gray);
 
    // Copy New Pixel Into Edge Data Bitmap
    ourEdgeData.setPixel(w, h, newPixelValue);	
   }	
  }
 
  bmd.unlock();
 }
 
 function getGray(pixelValue : uint) : uint
 {
  var red : uint = (pixelValue >> 16 & 0xFF) * 0.30;
  var green : uint = (pixelValue >> 8 & 0xFF) * 0.59;
  var blue : uint = (pixelValue & 0xFF) * 0.11;
 
  return (red + green + blue);
 }
 
 _bmp3.bitmapData = ourEdgeData;
 
}

 

Leave a Reply

Formatting: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">