04.12AS3 - LineArt Filter - Experiments
Hi Everyone,
live from Bangkok!! lol. It's songkram here, if you don't want to get wet stay inside and write on your blog.
So I have to detect the location of an item on images for one of my project, I thought of using the LineArt filter to detect high contrast areas, enabling me to pinpoint the exact space on the image (in this case transparent png's), because they are sometimes not centered.
The code from the LineArt filter comes for a very good book called "Kickass Java Programming" published in 1998. I simply ported to AS3, which was trivial.
First the sample
Secondly, the code for the filter
-
package filters
-
{
-
import flash.display.BitmapData;
-
-
/**
-
* ...
-
* @author Martin Legris ( http://blog.martinlegris.com )
-
*/
-
public class LineArt
-
{
-
public static function lineArt(data:BitmapData, intensity:uint, bgColor:uint = 0x000000):BitmapData
-
{
-
// create output
-
var output:BitmapData = new BitmapData(data.width, data.height, false, bgColor);
-
-
var img:Array = [];
-
var pixel:uint;
-
-
var r1:uint;
-
var g1:uint;
-
var b1:uint;
-
-
var r2:uint;
-
var g2:uint;
-
var b2:uint;
-
-
var r:uint;
-
var b:uint;
-
var g:uint;
-
-
for (var row:Number = 1; row <data.height; row++)
-
{
-
for (var col:Number = 1; col <data.width; col++)
-
{
-
// read the pixel to the left
-
pixel = data.getPixel(col-1, row);
-
r1 = (pixel & 0xff0000)>> 16;
-
g1 = (pixel & 0xff00)>> 8;
-
b1 = (pixel & 0xff);
-
-
// read the pixel above
-
pixel = data.getPixel(col, row - 1);
-
r2 = (pixel & 0xff0000)>> 16;
-
g2 = (pixel & 0xff00)>> 8;
-
b2 = (pixel & 0xff);
-
-
// current pixel
-
pixel = data.getPixel(col, row);
-
r = (pixel & 0xff0000)>> 16;
-
g = (pixel & 0xff00)>> 8;
-
b = (pixel & 0xff);
-
-
r = Math.min((Math.abs(r2 - r) + Math.abs(r1 - r)) * intensity, 255);
-
g = Math.min((Math.abs(g2 - g) + Math.abs(g1 - g)) * intensity, 255);
-
b = Math.min((Math.abs(b2 - b) + Math.abs(b1 - b)) * intensity, 255);
-
output.setPixel(col, row, (pixel & 0xff000000) + (r <<16) + (g <<8) + b);
-
}
-
}
-
-
return output;
-
}
-
}
-
}
It is a bit slow right now, taking about .5 seconds to do a 560 x 375 image on my laptop, if somebody knows how to optimize this, I would be soooo grateful!
Thanks
Martin

[...] See the original post here: 04.12AS3 - LineArt Filter - Experiments [...]
April 12th, 2009 at 10:13 pm
[...] [...]
April 14th, 2009 at 6:22 am
Hey man,
If you could require FP 10, you could look into using the Pixel Bender stuff. From the Adobe website:
Create high-performance, real-time effects for cinematic experiences that quickly engage users. With new Adobe Pixel Bender™, the same technology behind many filters and effects in Adobe After Effects® software, these dynamic and interactive effects can be used both in production with After Effects CS4 and live with Flash Player 10. The Pixel Bender just-in- time (JIT) compiler can also be used to process other types of data, such as sound or mathematical functions, asynchronously in a separate thread.
April 18th, 2009 at 8:02 am