Transforming Reality: Turn Your Photos into Cartoons with OpenCV

A Step-by-Step Guide to Converting Your Photos into Cartoon-style Images Using OpenCV’s Image Processing Techniques.

Aarafat Islam
8 min readApr 8, 2023
Image to Cartoon

In today’s world, we are surrounded by images and videos. From social media to advertising, images have become a powerful medium of communication. But have you ever wondered what would happen if you could turn your photos into cartoons? Imagine creating an animated version of your favorite photograph, or transforming a portrait into a whimsical illustration.

In this article, we will explore how to use the OpenCV library in Python to turn an image into a cartoon. OpenCV is a powerful computer vision library that provides a wide range of functions for image and video processing, including edge detection, color conversion, and filtering. We will use these tools to create a cartoon effect on a given image.

To accomplish this, we will start by importing the necessary modules and loading the input image. Next, we will apply a series of transformations to the image, including edge detection, color quantization, and bilateral filtering. Finally, we will combine these transformations to create a cartoon effect on the input image. Throughout the article, we will provide step-by-step instructions on how to implement each transformation using OpenCV. By the end of this article, you will have a clear understanding of how to use OpenCV to create a cartoon effect on any input image. So let’s dive in and learn how to turn images into cartoons using OpenCV!

Importing necessary libraries:

import cv2
import numpy as np
import os

Explanation of the code:

  • import cv2 imports the OpenCV library, which provides a wide range of functions for image and video processing.
  • import numpy as np imports the NumPy library, which is a popular library for working with arrays and matrices in Python.
  • import os imports the os module, which provides a way to interact with the file system.

Overall, this code imports the necessary modules for performing image processing with OpenCV in Python.

Displaying an Input Image using OpenCV in Python:

img = cv2.imread('original_picture.jpg')
cv2.imshow("original", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Original Picture

Explanation of the code:

  • cv2.imread('original_picture.jpg') loads the input image named "original_picture.jpg" into a variable named img. This is the name of your desired picture.
  • cv2.imshow("original", img) displays the input image in a window titled "original".
  • cv2.waitKey(0) waits for a key press. The argument 0 means the program will wait indefinitely until a key is pressed.
  • cv2.destroyAllWindows() closes all open windows.

Color Quantization using K-Means Clustering:

def color_quantization(img, k):
# Transform the image
data = np.float32(img).reshape((-1, 3))

# Determine criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 20, 0.001)

# Implementing K-Means
ret, label, center = cv2.kmeans(data, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
center = np.uint8(center)
result = center[label.flatten()]
result = result.reshape(img.shape)
return result

Function Description:

  • The function takes two arguments — the input image img and the number of clusters k.
  • The input image is first transformed into a two-dimensional array of pixel values using the NumPy library.
  • A set of criteria is determined for the K-Means clustering algorithm, including the maximum number of iterations and the minimum change in the cluster centers.
  • The K-Means clustering algorithm is applied to the data using the cv2.kmeans() function, with the specified number of clusters and criteria. If the k variable changes, the function will produce a new quantized image with a different number of color clusters. A smaller value of k will result in a quantized image with fewer colors, while a larger value of k will result in a quantized image with more colors.
  • The resulting cluster centers are converted to 8-bit integers using the NumPy np.uint8() function.
  • The original image is flattened into a one-dimensional array of pixel values, and each pixel is assigned to its nearest cluster center.
  • The resulting array of pixel values is then reshaped back into the shape of the original input image.
  • The resulting quantized image is returned as the output of the function.

Creating Edge Mask:

def edge_mask(img, line_size, blur_value):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray_blur = cv2.medianBlur(gray, blur_value)
edges = cv2.adaptiveThreshold(gray_blur, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, line_size, blur_value)
return edges

Function Description:

  • The function takes three arguments — the input image img, the size of the lines in the mask line_size, and the level of blurring to be applied to the grayscale image blur_value. If the line_size variable changes, the size of the lines in the mask will change accordingly. A smaller value will result in thinner lines, while a larger value will result in thicker lines.
  • The input image is first converted to grayscale using the cv2.cvtColor() function.
  • The grayscale image is then blurred using the cv2.medianBlur() function with a specified blur_value. If the blur_value variable changes, the level of blurring applied to the grayscale image will change. A smaller value will result in less blurring, while a larger value will result in more blurring.
  • An edge mask is created by applying adaptive thresholding to the blurred grayscale image using the cv2.adaptiveThreshold() function. This adaptive thresholding method calculates the threshold value for each pixel based on the mean of the pixel values in a local neighborhood around it.
  • The resulting edge mask is returned as the output of the function.

Generating Pencil Sketch from Image:

line_size = 7
blur_value = 7

edges = edge_mask(img, line_size, blur_value)
cv2.imwrite('pencil_sketch.jpg', edges)
cv2.imshow('pencil sketch', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
Pencil Sketch

Code Description:

  • The code starts by setting the values of line_size and blur_value to 7. If the values of line_size and blur_value change, the resulting pencil sketch will be affected. A smaller value for line_size will result in thinner lines, while a larger value will result in thicker lines. Similarly, a smaller value for blur_value will result in less blurring, while a larger value will result in more blurring, which can affect the overall appearance of the pencil sketch.
  • An edge mask is generated from the input image img using the edge_mask() function with the specified line_size and blur_value.
  • The resulting edge mask is saved as a new image file called ‘pencil_sketch.jpg’ using the cv2.imwrite() function.
  • The pencil sketch image is then displayed using the cv2.imshow() function.
  • The program waits for a key press from the user using the cv2.waitKey(0) function.
  • Finally, all open windows are closed using the cv2.destroyAllWindows() function.

Generating Cartoon from Image:

total_color = 9
img = color_quantization(img, total_color)
cv2.imwrite('cartoonize.jpg', img)
cv2.imshow('Cartoonize', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Cartoonize Picture

Code Description:

  • The code starts by setting the value of total_color to 9. If the value of total_color changes, the resulting cartoon image will be affected. A smaller value for total_color will result in fewer colors, while a larger value will result in more colors, which can affect the overall appearance of the cartoon image.
  • The color_quantization() function is called to reduce the number of colors in the input image img to the specified total_color.
  • The resulting image is saved as a new image file called ‘cartoonize.jpg’ using the cv2.imwrite() function.
  • The cartoon image is then displayed using the cv2.imshow() function.
  • The program waits for a key press from the user using the cv2.waitKey(0) function.
  • Finally, all open windows are closed using the cv2.destroyAllWindows() function.

Applying Bilateral Filter to the Image:

bilateral = cv2.bilateralFilter(img, 15, 75, 75)
# Save the output.
cv2.imwrite('blur.jpg', bilateral)
img = cv2.imread('blur.jpg')
cv2.imshow("Blur", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Bilateral Filtering

Code Description:

  • The code starts by applying a bilateral filter to the input image img using the cv2.bilateralFilter() function. The filter size is set to 15, and the values for sigma color and sigma space are both set to 75.
  • Changing the values of the bilateral filter’s kernel size, sigma color, and sigma space will affect the level of blurring applied to the image. A smaller kernel size and sigma values will result in less blurring, while larger values will result in more blurring. Changing these values can alter the overall appearance of the resulting blurred image.
  • The resulting blurred image is saved as a new image file called ‘blur.jpg’ using the cv2.imwrite() function.
  • The blurred image is then loaded back into memory using the cv2.imread() function and assigned to the img variable.

Overall Output:

Another Way:

import cv2
import numpy as np

# Load the input image
img = cv2.imread('input_image.jpg')

# Apply bilateral filter to smooth the image
img_smooth = cv2.bilateralFilter(img, 9, 75, 75)

# Convert the image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Apply edge detection to the grayscale image
edges = cv2.Canny(gray, 100, 200)

# Apply color quantization to the smoothed image
img_quant = cv2.cvtColor(img_smooth, cv2.COLOR_BGR2RGB)
Z = img_quant.reshape((-1,3))
Z = np.float32(Z)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
K = 8
ret,label,center=cv2.kmeans(Z,K,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)
center = np.uint8(center)
res = center[label.flatten()]
res2 = res.reshape((img_quant.shape))

# Combine the edges and color quantization to create the cartoon effect
cartoon = cv2.bitwise_and(res2, res2, mask=edges)

# Display the output image and save it
cv2.imshow('Cartoon', cartoon)
cv2.waitKey(0)
cv2.destroyAllWindows()

In conclusion, converting an image to a cartoon using OpenCV can be a fun and creative way to give a new life to your photographs. In this article, we have discussed how to use OpenCV to convert an image to a cartoon by applying various image processing techniques like color quantization, edge detection, and bilateral filtering. By combining these techniques, we were able to produce a stylized cartoon-like image from a regular photograph.

While the code provided in this article is a great starting point, there are many ways to customize the conversion process to achieve different results. Experimenting with different parameter values or applying additional image processing techniques can help to create unique and personalized cartoon-style images. With a little bit of creativity and some knowledge of image processing techniques, the possibilities are endless.

--

--

Aarafat Islam

🌎 A Philomath | Predilection for AI, DL | Blockchain Researcher | Technophile | Quick Learner | True Optimist | Endeavors to make impact on the world! ✨