I am using the following code to convert photo to a drawing:

import cv2
import numpy as np

img = cv2.imread("adventure.jpeg")

# 1) Edges
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.medianBlur(gray, 5)
edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 9)

# 2) Color
color = cv2.bilateralFilter(img, 9, 300, 300)

# 3) Cartoon
cartoon = cv2.bitwise_and(color, color, mask=edges)

cv2.imshow("Image", img)
cv2.imshow("Cartoon", cartoon)
cv2.imshow("color", color)
cv2.imshow("edges", edges)

the result is as follows: enter image description here

However I do not want the above result, what I want is this:

enter image description here

How do I do it using opencv / deep learning ?


This problem is known as "style transfer", and deep learning can achieve some pretty amazing results.

Here's a fantastic GitHub repo with an easy-to-use implementation of neural style transfer (usage instructions). I think you'll have better luck using a pre-built model like this rather than training your own.

If you want to train your own, this repo might be a good starting point. It has a training script included, you'll just need a bunch of training images and a style image.

| improve this answer | |

Its actually a bit more than zachdj wrote. You will propably need several things to reach this point. As he said, changing the style from real life photography to comic can be achieved by mere filters or building own filters (quite hard and not intuitive). Another way is to train a network to detect and adapt a specific style, like cGAN for example, which is able to find common style or patterns between huge, unsorted groups of images and learns to transform one in another. Like that:

horse to Zebra

Actually this can work well, but for that you may need a huge amount of data. Lets say arround 2000 images of people with dresses, and another 2000 of the style you want to adapt... so yeah not that easy to get his hands on .. generally the problem you try to tackle is called Domain adaption, and actually the way you doing it it can also be have addons like supervised or unsupervised. For both ways you need some data and preprocessing like, a neural network that do a segmentation for you like cutting out the background and isolates the person, but for that you need also masks which show the network whats interesting and what not. Check out Pix2Pix, UNET (for simple segmentation tasks) and cycle-GAN for unsorted image to image transformation.

Good Luck, stay tuned

Later Edit: Even came across that paper, maybe helps you.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.