Using OpenCV to Cartoonize an Image | by Himanshu Sharma | Towards Data Science

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.
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.
We will import required libraries that are cv2 and numpy using the code given below.
import cv2import numpy as np
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 Imagegray = 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 imagecolor = 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 windowcv2.destroyAllWindows()
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 scalegrayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)#apply gaussian blurgrayImage = cv2.GaussianBlur(grayImage, (3, 3), 0)#detect edgesedgeImage = cv2.Laplacian(grayImage, -1, ksize=5)edgeImage = 255 - edgeImage#threshold imageret, edgeImage = cv2.threshold(edgeImage, 150, 255, cv2.THRESH_BINARY)#blur images heavily using edgePreservingFilteredgePreservingImage = cv2.edgePreservingFilter(image, flags=2, sigma_s=50, sigma_r=0.4)#create output matrixoutput =np.zeros(grayImage.shape)#combine cartoon image and edges imageoutput = 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 windowcv2.destroyAllWindows()
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()
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()
cv2.imshow('pencil', cartoon_image2)    cv2.waitKey()    cv2.destroyAllWindows()
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.
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.