Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create NSBezierPath from bitmap outline

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:

Bitmap image of solid padlock shape

I want the bezier path of the outline of the shape. What is the bast way to achieve this?

like image 774
Nick Moore Avatar asked Mar 10 '11 11:03

Nick Moore


1 Answers

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.

like image 98
Regexident Avatar answered Nov 04 '22 01:11

Regexident