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.
- You usually don't want to include much detail that is in
the background. Say you have a picture of an animal in
its environment. The animal will share many colors with
the background, and will be difficult for any algorithm
to distinguish. The easiest pictures to convert have no
detail in the background, such as a bird against a blue
sky or a musical instrument against black velvet.
- Image compression techniques are working against you.
JPEG files, in particular, have compression artifacts that
are much more noticable after you process the images to
make the sketch.
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