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


In reply to Re: Image Sketch Convertion by toma
in thread Image Sketch Convertion by Everyone

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.