68

My app works with photos and videos of people, which I want to cartoonify. So I need an algorithm to do it manually (we use c++/Qt for our product, which has image manipulation classes) or perhaps some CLI program that will do it for me that I can call and use from our own app.

mido
  • 24,198
  • 15
  • 92
  • 117
JimDaniel
  • 12,513
  • 8
  • 61
  • 67
  • 4
    can you please give some examples of cartoonifyed pictures ? – ThibThib Aug 31 '09 at 13:22
  • To get more search results, papers etc look for "non photorealistic rendering (2d)". – felix Aug 31 '09 at 13:40
  • 1
    I've lost count of the number of times I've seen a new user's question downvoted into oblivion for not describing what he/she's tried so far, research, code, etc. And this question gets 55 upvotes! – andydavies Jul 10 '18 at 16:50

8 Answers8

47

Here's some algorithms to play with:

  • Median or repeated box blur filter to obtain cartoonish color palette
    • Edit: Bilateral filtering should suit your needs even better
  • Min filter (zeroth percentile) to enhance some types of edges
  • Color image segmentation using either small subcube or sphere in the RGB color cube
  • Generic edge enhancement on segmented image using edge detection such as Sobel kernels or 8-way edge tracing
  • Composit blurred/median-filtered image with enhanced edges

These are fairly basic and all very easy to implement. Keep in mind that median and box blur filters can be implemented with linear time complexity w.r.t. the kernel radius.

More edits:

Once you get the idea of Huang's algorithm, implementing a box blur filter is a delicious piece of cake.

Reading material:

  • Fast Median and Bilateral Filtering (get the PDF)
  • Median Filtering Constant time (get the PDF) Note: I have an implementation of this in C# using Mono/SIMD to accelerate histogram coalescence, however it only seems better than the O(r) algorithm when the diameter exceeds ~60 pixels due to the comparable number of add/sub instructions (the break-even point), a C++ implementation is probably much better suited to harness SIMD.

Other reading materials include Gonzalez & Woods' Digital Image Processing (seems to be an older edition) for segmentation and edge tracing. 8-way edge tracing can be really hard to bend your head around (choosing between on-pixel or between-pixel edges and how to latch onto edges). I'd be happy to share some code, but the hundred-liners don't exactly fit smoothly in here.

Cecil Has a Name
  • 4,962
  • 1
  • 29
  • 31
  • This seems to suit my purposes best - do you happen to know any good online resources for these algorithms we could post here, for completeness? – JimDaniel Sep 01 '09 at 13:28
  • 1
    Posterization is quantization transform that doesn't take geometrical information into consideration. – Cecil Has a Name Nov 10 '09 at 17:34
  • Anyone got a sample using ImageMagick, or Gimp in batch mode? I'm trying to figure this out on Ubuntu Linux for a project. – Volomike Feb 22 '10 at 06:06
  • Great answer! Would you elaborate on "Color image segmentation" step? I thought segmentation is used to identify objects after edge detection, no? – NeoWang Mar 17 '17 at 04:44
22

You could try rotoscopy, like toonyphotos.com does:

rotoscopy example

Jason Sundram
  • 12,225
  • 19
  • 71
  • 86
mooware
  • 1,722
  • 2
  • 16
  • 25
13

You might want to check out Freestyle, an open-source (Google Summer of Code, even) project to implement a non-photorealistic renderer for Blender. Here's an example of its output, in cartoon-mode: alt text
(source: sourceforge.net)

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
unwind
  • 391,730
  • 64
  • 469
  • 606
  • +1 - That's a great image, particularly with the lines which dramatically help the look and interpretation of the image. – tom10 Aug 31 '09 at 18:50
8

If there's some set of parameters which achieve the desired effect in the GIMP's Cartoon filter (or some other combination of filters) it can be run in a batch processing mode.

John Barrett
  • 545
  • 4
  • 9
6

I have not done this myself, but thinking about two steps that might give the image a cartoonish look.

  1. Detect edges, and draw a fairly fairly thick line (a few pixels) on those edges.

  2. Decrease the number of colours in your image.

martiert
  • 1,636
  • 2
  • 18
  • 23
5

Not sure if this will help, but this tutorial for Photoshop suggests doing the following:

  1. Open your image in Photoshop
  2. Filter > Blur > Gaussian Blur. Set the radius at 3.0 or higher, to taste.
  3. Edit > Fade Gaussian Blur. A window will pop up . . . set the mode to darken. You may also need to lower the opacity.

Here's the result.

enter image description here

I imagine that you could do something similar in your program.

Jason Sundram
  • 12,225
  • 19
  • 71
  • 86
RiddlerDev
  • 7,370
  • 5
  • 46
  • 62
  • Could not find a tutorial there, the website looks more like a link-farm... please fix the link. – redtuna Sep 01 '09 at 14:36
  • Weird, that was working when I posted it. Fixed it to what it is now, and appears to be working again. Thanks for the heads up. – RiddlerDev Sep 01 '09 at 15:04
4

It's relatively easy to do. Here are the steps:

  • bilateral filtering to simplify/abstract the photo. You may want to separate the bilateral filter so that it's faster. Perform the bilateral filter in 1d along the gradient and then along the normal to the gradient.

  • detect the edges. For instance, using a Difference of Gaussians algo. You may want to use the DoG in the gradient direction and smooth it following the flow lines. To get the flow lines, you would need to get the Edge Tangent Flow (ETF) which you can get via structure tensor.

  • quantize the colors. Actually, you quantize the luminance to simulate cel shading aka toon shading.

  • blend the abstracted image afer quantize and the edges you detected.

This will give you a rendered image that looks like a cel shaded cartoon.

I made some free software (for win64) that does exactly this at: http://3dstereophoto.blogspot.com/p/painting-software.html

The name of the software is "The Cartoonist" and you can see it in action here: http://3dstereophoto.blogspot.com/2018/07/non-photorealistic-rendering-software_9.html

Those are links to my blog which primarily deals with 3d photography (depth maps, photogrammetry, etc).

Ugo Capeto
  • 61
  • 3
1

actually i dont know a tool but you can look to osg (openSceneGraph)

there is a osgFX library and there is cartoon effect... maybe you can inspire from that library...


maybe (i dont know) imagemagick has many features, maybe it has a feature like that but i dont know...

ufukgun
  • 6,889
  • 8
  • 33
  • 55
  • A scenegraph is part of a display system, it handles storing a graph of objects to show - not manipulating images. Something like openCV would be more appropriate – Martin Beckett Aug 31 '09 at 14:00