in reply to Image Sketch Convertion

The best way to make the sketch depends on the type of images that you are starting with. If you only have a few colors in your image you just need to decide which colors are black and which are white. This is easy with the cartoonish kinds of pictures.

Photographs of the real world are much more difficult to convert to sketches. There are several problems to be overcome.

Most JPEG artifacts can be eliminated somewhat by blurring the images, followed by reducing their size. So it is best to convert large images. The large images take a long time to process, making experimentation tedious. Despeckling is an algorithm available in ImageMagick and it sometimes helps with pictures that look noisy.

Drawing the lines can be done by color mapping, as you suggested in your question, but this is difficult for photographs where shadows and angles cause surfaces to have many different colors. Skin has an especially rich set of colors.

A better method is to use edge detection, which looks for sharp transitions from one color to another. Edge detection still has troubles with dark, sharp shadows, such as those caused by a point source of light.

Once the edges have been detected, a threshhold function converts the various edge intensities to just black and white. Despeckling may also help here.

Here is a simple sequence of ImageMagick commands that make a line drawing from a photo.

Blur 90
Detect Edges 99.9
Threshhold 128
Negate

These are Image Magick commands with parameters, which I use from the menubar of the 'display' command, which is the interactive image manipulator that comes with Image Magick. The same commands should be available through the perl API.

If you have large photographs, you might try this sequence:

Blur 75
View half size
View half size
Enhance brightness 20
Greyscale
Spiff
Spiff
Spiff
Save as gif
Despecle

The spiff command enhances contrast. I use it three or four times in a row. For some unkown reason I seemed to need to save as a gif file to get the despecle command to work. This sequence creates a greyscale sketch.

Once you have your sketches, save them as a png. I found that png files are much smaller than both jpg and gif for this type of image.

I don't have perl code for these commands but it shouldn't be too difficult to write. I have used ImageMagick with perl before and didn't have any trouble with it, mostly because of the first tutorial provided by merlyn, which is referenced above.

If you want really nice, artistic sketches you have to be prepared to do some hand editing of the final result. Look at the sketches by an artist that you like. You will see that the work has a unique style associated with the artist. This style is difficult to capture in a simple algorithm. But not so difficult that I won't stop trying.

It should work perfectly the first time! - toma

Replies are listed 'Best First'.
Re: Re: Image Sketch Convertion
by mattr (Curate) on Jun 04, 2001 at 12:49 UTC
    Some people have hacked together a Quake rendering engine which displays things as blueprints or as hand drawn sketches like what you are talking about.

    So you could play with that engine's source code, or just import what you want to draw into Quake and display it (in 3d of course.. with monsters if you like).

    You can get the source code to the renderer, which would be really neat for someone to interface with Perl, at http://www.cs.wisc.edu/graphics/Gallery/NPRQuake/

      This 3D Quake sketch rendering engine is *very* cool.

      It would be a great project to take the renderer and integrate it with the FreeWRL perl VRML viewer. This would give you the ability to draw virtual worlds with a variety of 3D VRML tools and prebuilt objects and explore them in sketch form.

      It looks to me like this is possible since both codestreams render with OpenGL. FreeWRL uses Mesa library on Linux instead of OpenGL. This shouldn't be a problem, since Mesa typically works in place of OpenGL.

      Of course, working with 3D vector models is very different from working with bitmaps.

      It should work perfectly the first time! - toma