Cartoonizing an image using OpenCV and Python with 15 lines of code

Hello and Welcome to the exciting world of Computer vision where fun never ends and exploration is limitless. Here, in this article we will discuss on how to cartoonize an image using OpenCV and Python with 15 lines of code.

So, lets dive in and see how to cartoonizing an image.

First, Let’s breakdown the pipeline we are going to follow:

— Reading an image as input
 — Import required libraries
 — Getting the edges in the image
 — Cartoonization of the image
 — Display the resultant images

Trending AI Articles:

1. Microsoft Azure Machine Learning x Udacity — Lesson 4 Notes

2. Fundamentals of AI, ML and Deep Learning for Product Managers

3. Roadmap to Data Science

4. Work on Artificial Intelligence Projects

Let’s get into the code now.

  1. For the first step, let us import the required libraries as below
    import cv2
    import numpy as np
  2. For the second step, let us read an image as input
    img = cv2.imread(“my_image.jpg”)
    Here my image name in my_image.jpg
  3. For the third step, let us get the edges of the image read as input
    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)
    In the above step, we converted the normal BGR image to Grayscale and applied median blurring on the image. Post that apply the adaptiveThreshold on the image which will yield the image containing all the edges.
ML Jobs

4. For the fourth step, let us cartoonize the image using the bitwise and of the color generated images using bilateral filter. Let us see the code to do this.
color = cv2.bilateralFilter(img, 9, 250, 250)
cartoon = cv2.bitwise_and(color, color, mask=edges)

5. For the final step, we will visualize our results using the below code
cv2.imshow(“Image”, img)
cv2.imshow(“Edges”, edges)
cv2.imshow(“Cartoon”, cartoon)

6. Always remember to use waitKey and destroy the windows before exiting, hence use the below code.
cv2.waitKey(0)
cv2.destroyAllWindows()

This will produce a cartoonized image for the input image you passed as input, let me share a collage of how my input image looks after it is cartoonized.

Cartoonized output


Cartoonizing an image using OpenCV and Python with 15 lines of code was originally published in Becoming Human: Artificial Intelligence Magazine on Medium, where people are continuing the conversation by highlighting and responding to this story.

Via https://becominghuman.ai/cartoonizing-an-image-using-opencv-and-python-with-15-lines-of-code-af40274b7612?source=rss—-5e5bef33608a—4

source https://365datascience.weebly.com/the-best-data-science-blog-2020/cartoonizing-an-image-using-opencv-and-python-with-15-lines-of-code

Published by 365Data Science

365 Data Science is an online educational career website that offers the incredible opportunity to find your way into the data science world no matter your previous knowledge and experience. We have prepared numerous courses that suit the needs of aspiring BI analysts, Data analysts and Data scientists. We at 365 Data Science are committed educators who believe that curiosity should not be hindered by inability to access good learning resources. This is why we focus all our efforts on creating high-quality educational content which anyone can access online.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Design a site like this with WordPress.com
Get started