# How to convert photo to a vector drawing

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

import cv2
import numpy as np

# 1) Edges
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.medianBlur(gray, 5)

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

# 3) Cartoon

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


the result is as follows:

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

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.

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:

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.