MITB Banner

Converting An Image To A Cartoon Using OpenCV

Share

Computer vision is one of the hottest fields in Artificial Intelligence with a wide variety of applications. OpenCV is the most popular library used in computer vision with a lot of interesting stuff. If you want to start your journey in computer vision you can start from learning OpenCV. It is easy to understand and implement by everyone. In this article using OpenCV, let’s have fun with converting normal images into cartoons.

We will cover the following steps in this article to convert the image to cartoon:-

  1. Importing libraries
  2. Reading the input image
  3. Detecting edges in the image
  4. Converting into grayscale & applying the medium blur
  5. Cartoonifying the image

Converting Image to Cartoon Using OpenCV

Now, let us proceed step-by-step.

Step-1: Reading the libraries

Here we are importing the required libraries. If you are working in Google Colab then we need to import google.colab.patches.

#Importing required libraries
import cv2
import numpy as np
from google.colab.patches import cv2_imshow

Step-2: Reading the image

In this step, we will read the image. We have download an image if Virat Kohli from Google Image and will try to perform our experiment on this image.

#Reading image 
img = cv2.imread("/content/virat.jpeg")
from skimage import io 
io.imshow(img)

As we can see that the input image read by OpenCV is being shown as a BGR (Blue-Green-Red) image so we need to convert it to the RGB (Red-Green-Blue).

#Converting to RGB
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
io.imshow(img)

Step-3: Detecting edges

Here we are going to detect the edges in the image using adaptive thresholding methods.

#Detecting edges of the input image
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)
io.imshow(edges)
images to cartoon

Step-4: Cartoonifying image

In this step, we will be cartoonifying the image using bilateral filter method.

#Cartoonifying the image
color = cv2.bilateralFilter(img, 9, 250, 250)
cartoon = cv2.bitwise_and(color, color, mask=edges)

Step-5: Final Output (Cartoon Image)

Finally, we will visualize the final output

io.imshow(cartoon)

images to cartoon

The transformation from input to output

images to cartoon
images to cartoon

Conclusion

In the above demonstration, we converted a normal image into a cartoon by implementing a few lines of code using computer vision techniques. we shall have great fun using computer vision techniques.

The complete code of this implementation is available on AIM’s GitHub repository. Please go through this link to find the notebook.

Share
Picture of Prudhvi varma

Prudhvi varma

AI enthusiast, Currently working with Analytics India Magazine. I have experience of working with Machine learning, Deep learning real-time problems, Neural networks, structuring and machine learning projects. I am a Computer Vision researcher and I am Interested in solving real-time computer vision problems.
Related Posts

CORPORATE TRAINING PROGRAMS ON GENERATIVE AI

Generative AI Skilling for Enterprises

Our customized corporate training program on Generative AI provides a unique opportunity to empower, retain, and advance your talent.

Upcoming Large format Conference

May 30 and 31, 2024 | 📍 Bangalore, India

Download the easiest way to
stay informed

Subscribe to The Belamy: Our Weekly Newsletter

Biggest AI stories, delivered to your inbox every week.

AI Courses & Careers

Become a Certified Generative AI Engineer

AI Forum for India

Our Discord Community for AI Ecosystem, In collaboration with NVIDIA. 

Flagship Events

Rising 2024 | DE&I in Tech Summit

April 4 and 5, 2024 | 📍 Hilton Convention Center, Manyata Tech Park, Bangalore

MachineCon GCC Summit 2024

June 28 2024 | 📍Bangalore, India

MachineCon USA 2024

26 July 2024 | 583 Park Avenue, New York

Cypher India 2024

September 25-27, 2024 | 📍Bangalore, India

Cypher USA 2024

Nov 21-22 2024 | 📍Santa Clara Convention Center, California, USA

Data Engineering Summit 2024

May 30 and 31, 2024 | 📍 Bangalore, India

Subscribe to Our Newsletter

The Belamy, our weekly Newsletter is a rage. Just enter your email below.