How to make a photo look as a drawing (the silly way)

I’ve been asked to explain how I converted the photo of our friend singing and playing her guitar into something that barely resembles a handmade drawing. This isn’t, of course, anything original and a lot of image editors have a function to do it, but I prefer it to follow the longest path because I’m a masochist.

To do this I used MATLAB with Image Processing Toolbox functions. There’s also a free software counterpart (GNU Octave) that has a lot of image processing functions in the Octave Forge project, but I thought I would need grayscale dilation and erotion, thus I decided to use the propietary MATLAB (hey! it won’t happen again! I promise). This probably will run well in Octave though, if we consider the subtle differences in imread behaviour (it doesn’t return a MxNx3 matrix, but 3 matrices, one for each color plane instead), and handle the fact that imresize function is missing (perhaps interp2 could work as a replacement for this task).

First of all you can see the original image (resized to a smaller size) here:

Original image

I’ll focus on the amplifier and the mixing desk which are on the right so I can include the images without having to resize them:

Detail of the amplifier and the mixing desk in the original image

The first step was to open the original image and convert it to grayscale:

%% Read original image
i=imread('campaneta.jpg');

%% Convert it to grayscale
a=rgb2gray(i);
Detail of the mixing desk in the original image as a grayscale image
Afterwards I resized the resulting image to a size easier to handle (half in each direction):

%% Resize it to a more usable format
b=imresize(a,.5,'bicubic');
Detail of the mixing desk in the original image resized to a smaller size
Since the original image was taken from a digital camera I used a basic contrast stretching. Probably some kind of filtering would help increase quality of the final result (in addition to the filtering that bicubic resizing already does):

%% Adjust contrast
d=imadjust(b);
Image after contrast stretching

Then I used four different methods to obtain the edges of the image (Canny, Sobel, Roberts and zero crossing detection):

%% Get edges using 4 different methods
bw1=edge(d,'canny');
bw2=edge(d,'sobel');
bw3=edge(d,'roberts');
bw4=edge(d,'zerocross');

You can see the results of these operations here:

Canny edge detection
Canny
Sobel edge detection
Sobel
Roberts edge detection
Roberts
Zero crossing edge detection
Zero crossing
After that I combined the edges into one image. The trick is to sum all results and then normalize (and I actually negate the image to obtain a white background). This way, the places where all edge detectors get an edge are black, and pixels become brighter depending on how many edge detectors get an edge in it.

%% We sum all edges and then
%% normalize it and negate it
bwr=1-(double(bw1)+double(bw2)+
        double(bw3)+double(bw4))/4;
All edge detection added
It can be seen that some pixels are unconnected. Perhaps a closing morphological operation could be used here to solve this (or, even better, before combining the images). I simply resized the image because I needed a smaller one, and the bicubic operation did the trick.
All edge detection added and resized


%% resizes image to 300 pixel-width, its final size
prefinal=imresize(bwr,300/744,'bicubic');
Now you can see that the image is too much bright. That can be solved simply by using a contrast stretching operation.
All edge detection added and resized and contrast stretched


%% auto-stretches image contrast
final=imadjust(prefinal);

This is how the whole image looks like.

The whole resulting image

There’s only one thing left: drawing a border to the image, because leaving all those lines open doesn’t look very good. After that imwrite saves the image to disk.

%% 1 pixel wide black border
s=size(final);
final(1,:)=0;
final(s(1),:)=0;
final(:,1)=0;
final(:,s(2))=0;

%% write result to disk
imwrite(final, 'neverland_fairy.png');

The result doesn’t look bad (if you take into account that this is automatable and doesn’t need any hand-editing), don’t you agree?

The whole resulting image with a thin black border

Perhaps it would be nice to try to stick with only one edge detector method and play with its threshold. If you want to give it a try, please tell me how does that work.

Hey… why don’t you do it the easy way?

Errr… yes… Just do this (I’ve translated the menu labels from Catalan to English so they could be slightly different; but you are smart enough to find them):

  • Open the file in the Gimp.
  • Filters/Edge detection/Gaussian
  • Radius 1: 10; Radius 2: 1, Normalize and Invert.
  • Image/Mode/Grayscale
  • Image/Scale
  • Width: 300px, Cubic interpolation.
  • Layers/Colors/Levels
  • Set central level to 0.13

Well this is what you’ll get. Looks nicer, doesn’t it? This is just because the threshold of the edge detector is set much lower. Using graphical programs lets you choose the best-looking settings very quickly. But the first way was much more fun!

Easier and nicer method using The Gimp

5 Responses to “How to make a photo look as a drawing (the silly way)”

  1. wonderland Says:

    també es pot fer amb photoshop, però suposo que com tu no vols saber res de microsoft……

    ostres, quina gràcia de fer de model jijijijijijiji

  2. SiCkBoY4 Says:

    photoshop-microsoft como que no, és photoshop-apple. apa! ja et pots ficar un altre cop amb els maqueros…

  3. jmones Says:

    Bah! No em compraré un programa que val el que val quan tinc el Gimp que és lliure i és més que suficient per les meves habilitats.

    I si em falta alguna cosa programo un plugin. :)

  4. jmones Says:

    Ei! A mi els maqueros ja m’agraden! Per això tenia un iBook. Molt millor el MacOs X que Windows! I molt maco el ferro d’Apple.

    El que passa és que, mentre uns només valoren el preu i les funcionalitats (Windowseros) als altres de vegades els perd l’estètica i tant se’ls en fot que les coses siguin pràctiques mentre siguin boniques (animacions llarguíssimes, etc… tots sabem de què parlem).

    És divertit.

    Als Linuxeros ens perd la consola. Triguem 3 vegades més en fer una cosa que un maquero fa en 3 clics per saber com funciona per sota i perquè la pantalla negra és més geek.

  5. jmones Says:

    Ostres! Avui estic xerraire!

    Només dir que els que utilitzem GNOME ja tenim un escriptori que estèticament està força currat i, a més, molt còmode. Una de les grans preocupacions del projecte és fer aplicacions senzilles i que tot vagi amb el menor nombre de clics possibles.

    A més està molt inspirat en els macs….

    I en català…

    Proveu-lo si podeu…

Leave a Reply