Search code examples
pythonalgorithmimage-processingpython-imaging-libraryedges

Smoothen edges of a non-binary image


I'm making some photo-editing tools in python using PIL (Python Imaging Library), and I was trying to make a program which converts a photo to its 'painted' version.

I've managed to make a program which converts a photo into its distinct colours, but the problem is that the algorithm I'm using is operating on every pixel, meaning that the resulting image has very jagged differences between colours.

Ideally, I'd like to smoothen out these edges, but I don't know how!

I've checked out this site for some help, but the method there produces quite different results to what I need.

My Starting Image: Starting Image

My Image with Distinct Colours: Image with Distinct Colours

I would like to smoothen the edges in the image above.

Results of using the method which doesn't quite work: Results of using he method which doesn't quite work

As you can see, using the technique doesn't smoothen the edges into natural-looking curves; instead it creates jagged edges.

I know I should provide sample output, but suprisingly, I haven't actually got it, so I'll describe it as best as I can. Simply put, I want to smoothen the edges between the different colours.

I've seen something called a Gaussian blur, but I'm not quite sure as to how to apply it here as the answers I've seen always mention some sort of threshold, and are usually to do with binary images, so I don't think it can apply here.


Solution

  • Edge enhancement does the opposite of edge smoothing, so this is certainly not the tool you should use.

    Unfortunately, there is little that you can do because edge smoothing will indeed smoothen the jaggies, but it will also destroy the true edges, resulting in a blurred image. Edge-preserving smoothing is also a dead-end.

    You should have a look at the methods to extract the "cartoon part" of an image. There is a lot of literature on this topic, though often pretty sophisticated.