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.


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.