问题
I would like to create a NSBezierPath object which corresponds to the outline of an arbitrary bitmap image.
All my images are png solid monochrome shapes with transparent backgrounds. Here is an examplee:
I want the bezier path of the outline of the shape. What is the bast way to achieve this?
回答1:
What you are looking for is a tracing/vectorization algorithm.
DrawKit appears to support bitmap to vector tracing:
- DKImageShape+Vectorization.m
- NSImage+Tracing.m
Some other (non-ObjC) open-source projects implementing tracing (for code reference):
- http://code.google.com/p/vectorizationpackage/ (ActionScript)
- http://code.google.com/p/linetracer/ (C++)
- http://code.google.com/p/vector/ (Python)
Anyone who has ever used Illustrator's (or any of its competitors') tracing tools can assure you that tracing is a non-trivial thing, though. But for your simple b&w images (technically graysacle, due to antialiasing, but that's rather neglectable here) just about any algorithm should do the trick. Especially if you don't even need perfect curve fitting, as you noted.
来源:https://stackoverflow.com/questions/5259268/create-nsbezierpath-from-bitmap-outline