Using OpenCV to Cartoonize an Image
By: Himanshu Sharma
Tags: Image Processing, Computer Vision, Opencv, Deep Learning, Python

Using OpenCV to Cartoonize an Image

(Source: By Author)

What is OpenCV?

OpenCV is an open-source python library used for computer vision and machine learning. It is mainly aimed at real-time computer vision and image processing. It is used to perform different operations on images which transform them using different techniques.

It majorly supports all languages like Python, C++, Android, Java, etc. It is easy to use and in demand due to its features. It is used in creating image processing or rendering application using different languages.

In this article, we will try to perform some image transformation using the CV2 version of OpenCV. We will try to make sketches, cartoons, and other transformations on an ordinary image.

Installing OpenCV

In order to install OpenCV, we need to run the following command given below in the command prompt.

pip install opencv-python

As soon as we install OpenCV we are good to go and start with our image transformation process. Open jupyter notebook and import the required libraries.

Importing Required Libraries

We will import required libraries that are cv2 and numpy using the code given below.

import cv2

import numpy as np

First Transformation(Cartoonize)

In this transformation, we will find the edges of the image and make a cartoon using the bilateral filter and bitwise operator. You can use any image for this transformation, I am using my own image for this article.

# Reading the Image

image = cv2.imread("image1.jpg") # Finding the Edges of Image

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

gray = cv2.medianBlur(gray, 7)

edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 10) # Making a Cartoon of the image

color = cv2.bilateralFilter(image, 12, 250, 250)

cartoon = cv2.bitwise_and(color, color, mask=edges) #Visualize the cartoon image

cv2.imshow("Cartoon", cartoon)

cv2.waitKey(0) # "0" is Used to close the image window

cv2.destroyAllWindows()

Original and Cartoon Image(Source: By Author)

Second Transformation(Blurring Image)

In the second transformation, we will try to blur the image with an edge-preserving filter and adding a threshold to the edges. For this, we will use Gaussian Blur.

#convert to gray scale

grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) #apply gaussian blur

grayImage = cv2.GaussianBlur(grayImage, (3, 3), 0) #detect edges

edgeImage = cv2.Laplacian(grayImage, -1, ksize=5)

edgeImage = 255 - edgeImage #threshold image

ret, edgeImage = cv2.threshold(edgeImage, 150, 255, cv2.THRESH_BINARY) #blur images heavily using edgePreservingFilter

edgePreservingImage = cv2.edgePreservingFilter(image, flags=2, sigma_s=50, sigma_r=0.4) #create output matrix

output =np.zeros(grayImage.shape) #combine cartoon image and edges image

output = cv2.bitwise_and(edgePreservingImage, edgePreservingImage, mask=edgeImage) #Visualize the cartoon image

cv2.imshow("Cartoon", output)

cv2.waitKey(0) # "0" is Used to close the image window

cv2.destroyAllWindows()

Blurring Image using Edge Preserving(Source: By Author)

Third Transformation(Stylization)

In this transformation, we will use the stylization technique and create the cartoon of the image.

cartoon_image = cv2.stylization(image, sigma_s=150, sigma_r=0.25)

cv2.imshow('cartoon', cartoon_image)

cv2.waitKey(0)

cv2.destroyAllWindows()

Stylization(Source: By Author)

Fourth Transformation(Pencil Sketch)

In this transformation, we will create a pencil sketch of the image. We will create both colorful and black & white sketches.

cartoon_image1, cartoon_image2 = cv2.pencilSketch(image, sigma_s=60, sigma_r=0.5, shade_factor=0.02)

cv2.imshow('pencil', cartoon_image1)

cv2.waitKey()

cv2.destroyAllWindows()

Black & White(Source: By Author)

cv2.imshow('pencil', cartoon_image2)

cv2.waitKey()

cv2.destroyAllWindows()

Colored Sketch(Source: By Author)

In this article, we saw how we can use OpenCV to create different image transformations. Go ahead try these transformations and in case you feel any difficulty please let me know in the response section.

Before You Go

Thanks for reading! If you want to get in touch with me, feel free to reach me on hmix13@gmail.com or my LinkedIn Profile. You can view my Github profile for different data science projects and packages tutorials. Also, feel free to explore my profile and read different articles I have written related to Data Science.