PDA

View Full Version : Vectorization - Raster To Vector At Runtime



skineh
February 12th, 2009, 08:41 PM
About 2 weeks ago I saw a post from senocular on the actionscript.org forums. In it, he was replying to a user who was asking if you could do Trace Bitmap at runtime. Of course, you can't do that, and senocular said that as far as he knew he hadn't seen a class that did it.

So, being the masochist I am, I decided to take up the challenge. 2 weeks of spare time later and I now have the Vectorization Package. It's three classes written in AS3 for Flash Player 9 and up. I gotta say, it was a really fun exercise in using the tools available to do something new.

http://coreyoneil.com/vector.jpg (http://www.coreyoneil.com/Flash/Vector/index.html)
Launch Example (http://www.coreyoneil.com/Flash/Vector/index.html)

Check out the example and feel free to play around. Unlike Trace Bitmap, you can get results that will have gaps between shapes if you kick up the curve smoothness setting. I felt this was acceptable mainly because I liked some of the abstract results that would come from it. Plus it saves me from having to write yet another crazy intensive routine. ;)

Accessors include:


threshold - Just like Trace Bitmap, this is the color threshold used to determine the range of acceptable colors when grouping shapes.
minimumArea - Lets you limit the smallest-sized shape allowed, by number of pixels.
curveSmoothness - Controls how curvy shapes can be drawn. This ties into the next property...
stepSize - As each final vector shape is drawn, stepSize controls how many pixels the package moves along the edge before evaluating whether or not it needs to draw the next part of the outline (that evaluation compares the angle of change to the value provided in curveSmoothness). So, as you increase curveSmoothness, you'll also want to increase stepSize a little to help keep your curves large and bold.
transparentColor - For a lot of good reasons I'm not going to get into, this package does not support transparency in the final vector image. The transparentColor property lets you choose what color will go in place of any transparent areas in your original raster image.

The Vectorization Package works with all DisplayObjects. So even through they're already vector, you could re-vectorize things like Shapes and Textfields (makes for fun results). You can even do an FLV (it would vectorize whatever frame was up when you run it).

In the process of making this, I had to write a despeckle/posterize routine and a find edges routine. They work so nicely that I plan on making them their own classes and releasing them as well.

The Vectorization Package is released under the MIT license. As it is with any code I release, I encourage you to let me know if you use it for anything! I always like to know when my code can help someone out. :)

-----> VECTORIZATION PACKAGE - DOWNLOAD AND DOCS (http://code.google.com/p/vectorizationpackage/) <------

I'd love feedback on it! I really don't know if anyone's done this before, so kinda neat to think I might be the first. And if I'm not, I'm still pretty darn happy with how this turned out. Not sure if there's much use for this, but I've seen it requested enough times over the years that at least now people can stop asking. ;)

And if you happen to read this senocular, thanks for inadvertantly sprouting the idea in my head! :D

nortago
February 13th, 2009, 08:46 AM
Nice one! Incredible work there... any chance of releasing the class? IU'd love to play about with it?

skineh
February 13th, 2009, 08:48 AM
Nice one! Incredible work there... any chance of releasing the class? IU'd love to play about with it?

Absolutely. I was going to release it last night with the post but there were some issues with e-mail yesterday. The source and documentation should (hopefully) be up tonight, at which point I'll update the post here. :)

claudio
February 13th, 2009, 09:32 AM
Very nice! :thumb:

jimhere
February 14th, 2009, 09:40 AM
Hmmm, Obama, Dubya and... Chester. Chester A. Arthur was unexpected.

senocular
February 14th, 2009, 10:37 AM
Awesome! I should say more things don't exist.

skineh
February 14th, 2009, 11:36 AM
Hmmm, Obama, Dubya and... Chester. Chester A. Arthur was unexpected.

Nobody expects Chester A. Arthur! :D


Awesome! I should say more things don't exist.

Haha, right? Just stick to Flash-related stuff. I'm pretty sure world peace and clouds that rain beer are out of my league. ;)


Okay, the source, documentation, and FLA for that example are all online! Enjoy!

senocular
February 14th, 2009, 03:33 PM
Haha, right? Just stick to Flash-related stuff. I'm pretty sure world peace and clouds that rain beer are out of my league. ;)

You know what else doesn't exist? CLOUDS THAT RAIN BE... crap!

theonlycarmire
February 15th, 2009, 01:26 AM
Huzza! This is mad cool skineh!

skineh
February 17th, 2009, 08:28 AM
Huzza! This is mad cool skineh!

Thanks, carmire! :) It's weird, because I really don't have a use for this, but just being able to write a stable vectorization routine makes it one of my favorite bits of code.

nicoptere
June 5th, 2009, 04:41 AM
hi,
very neat piece of code here :)

a couple of things in the Vectorize class:


l. 291: function branch limited to namespace > should go private
l. 425: untyped var startX > Number
l. 850: untyped var numPixels > intin your sample code (http://www.coreyoneil.com/Flash/Vector/documentation/Vectorize.html) there's a missing right brace at:

myVector.addEventListener(Vectorize.COMPLETE, showVector ;<<here :)otherwise it very precise (a bit heavy to process though).

recently I had a take too with a component labelling pass and a marching cube algorithm (by Sakri (http://www.sakri.net/blog/2009/05/28/detecting-edge-pixels-with-marching-squares-algorithm/)).
further explainations available here: http://en.nicoptere.net/?p=166
(http://en.nicoptere.net/?p=166)

skineh
June 5th, 2009, 08:44 AM
Thanks for pointing those out, nicoptere. I was quick to get the code out there once it was done and working that I'm not surprised I overlooked a few typecastings. Since it doesn't impede on its functioning, I probably won't rush to update it. I was considering coming back to this project, however, for an FP10 version that can let you download an SVG file of your vector graphic. I figure then it'll be significantly more useful. :)

I'll definitely correct that missing parenthese in the docs. Thanks for bringing it to my attention.

Hahaha, yeah, it's definitely CPU intensive. Hard to avoid that, I'm afraid. Glad to hear it works well for you, though!

djbracing
August 12th, 2009, 06:50 AM
I am currently having a flash application developed, which will be embeded into a website, and will allow people to design their own text. I would like the text created to be exported as a vector based EPS file.

Could this class be used to vectorise the raster file created during runtime, and output an EPS file?

Hope that makes sense?

Thanks,

David.