Approximate a tracing effect to "clean" an image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
BigNoub
Posts: 33
Joined: 2015-12-29T14:49:52-07:00
Authentication code: 1151

Approximate a tracing effect to "clean" an image

Post by BigNoub » 2019-01-02T10:29:01-07:00

I'm using Imagemagick 7.0.8-14 on ubuntu 16.04

I have a jpg that is quite noisy and blurry with lots of shapes and colors:

Image

I would like to "clean" it, meaning remove the noise in the white areas, make sharper edges, maybe reduce the number of colors in the image. Basically I would like the image to appear as if it was a vector illustration that had been rasterized. I was able to get to this with potrace (by tracing then converting back to jpg):

Image

It's not bad, but it's very slow, and I'd like to still reduce the complexity of the image if possible, to obtain something like this (roughly painted in photoshop just to give you an idea):

Image

How would you approach such a task? Do I necessarily have to try and test with the sharp, denoise, reduce colors parameters, or is there a command / script to do the heavy-lifting?

snibgo
Posts: 12286
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Approximate a tracing effect to "clean" an image

Post by snibgo » 2019-01-02T11:27:40-07:00

You want to blur the image heavily where it is fairly smooth, and sharpen it where it is fairly sharp. This is often called "adaptive blur" or "adaptive sharpen". See Adaptive contour blur and sharpen.

A more complex method is shown at Cartoon and texture.

Broadly, the technique is to decide what blur does what you want (eg "-blur 0x10") in blurry areas, and what sharpening does what you want for the edges. Then blend those images with a mask.
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 25657
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Approximate a tracing effect to "clean" an image

Post by fmw42 » 2019-01-02T11:59:50-07:00

There are several other options.

Input:
Image


1) Kuwahara filtering (fast)

Code: Select all

convert image.jpg -kuwahara 5 image_kuwahara5.png
Image

Code: Select all

convert image.jpg -kuwahara 9 image_kuwahara9.png
Image


2) mean shift (slow - iterative)

Code: Select all

convert image.jpg -colorspace YIQ -monitor -mean-shift 21x21+10% +monitor -set colorspace YIQ -colorspace sRGB image_mshift21yiq.png
Image


3) kmeans script (slow - iterative)

Code: Select all

kmeans -n 16 -m 5 image.jpg image_kmeans16.png
Image

BigNoub
Posts: 33
Joined: 2015-12-29T14:49:52-07:00
Authentication code: 1151

Re: Approximate a tracing effect to "clean" an image

Post by BigNoub » 2019-01-02T13:47:07-07:00

Thanks, kuwahara looks promising!

User avatar
fmw42
Posts: 25657
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Approximate a tracing effect to "clean" an image

Post by fmw42 » 2019-01-02T14:07:26-07:00

Here is the results from adaptive-sharpening as snibgo had suggested. It also is fast.

Code: Select all

convert image.jpg -adaptive-sharpen 0x35 image_as35.png
Image

Post Reply