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

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

Let’s get into the code now.

  • For the first step, let us import the required libraries as below
# Import required libraries

import cv2
import numpy as np
  • For the second step, let us read an image as input, here the image name is my_image.png which is present in the same directory with this script.
# Read Image from the local directory

img = cv2.imread("my_image.png")

  • For the third step, let us get the edges of the image read as input, In this 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.
# Get Edges from the images

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)

  • 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)

  • For the final step, we will visualize our results using the below code
# Display the resulting images on a display named window

cv2.imshow('Image', img)
cv2.imshow('Edges', edges)

cv2.imshow('Cartoon', cartoon)

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

Let me share the resultant output and input passed to this script so that you can have a gist of what we have achieved

Left: Original Image and Right: Cartoonized Image
Left: Original Image
Right: Cartoonized Image

To Wrap this up, let us have a look on the full code snippet, please find below code.

# Import required libraries
import cv2
import numpy as np

# Read Image from the local directory
img = cv2.imread("my_image.png")

# Get Edges from the images
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)

color = cv2.bilateralFilter(img, 9, 250, 250)
cartoon = cv2.bitwise_and(color, color, mask=edges)

# Display the resulting images on a display named window
cv2.imshow('Image', img)
cv2.imshow('Edges', edges)
cv2.imshow('Cartoon', cartoon)

cv2.waitKey(0)
cv2.destroyAllWindows()

This concludes today's article, I hope you enjoyed. Do try the code on your own images and keep engaged with Computer Vision.

Also, do share your feedbacks and suggestions on the same. Your feedback will be hugely appreciated.

Thanks and Regards,

Akash Bhiwgade

要查看或添加评论,请登录

Akash Bhiwgade的更多文章

社区洞察

其他会员也浏览了