Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Using OpenCV in Python to Cartoonize an Image
Image cartoonization is a popular computer vision technique that transforms regular photos into cartoon-style images. While many professional cartoonizer applications exist, most are paid software. Using OpenCV in Python, we can achieve this effect by combining bilateral filtering for color reduction and edge detection for bold outlines.
Algorithm Overview
The cartoonization process involves five key steps:
Apply bilateral filter to reduce the color palette of the image
Convert the original image to grayscale
Apply median blur to reduce image noise in the grayscale image
Create an edge mask from the grayscale image using adaptive thresholding
Combine the color image from step 1 with the edge mask from step 4
Complete Implementation
Here's the complete code to cartoonize an image using OpenCV ?
import cv2
import numpy as np
def cartoonize_image(image_path, output_path=None):
# Step 1: Apply bilateral filter for edge-aware smoothing
num_down = 2 # number of downsampling steps
num_bilateral = 7 # number of bilateral filtering steps
img_rgb = cv2.imread(image_path)
# Downsample image using Gaussian pyramid
img_color = img_rgb.copy()
for _ in range(num_down):
img_color = cv2.pyrDown(img_color)
# Apply multiple small bilateral filters instead of one large filter
for _ in range(num_bilateral):
img_color = cv2.bilateralFilter(img_color, d=9, sigmaColor=9, sigmaSpace=7)
# Upsample image back to original size
for _ in range(num_down):
img_color = cv2.pyrUp(img_color)
# Step 2 & 3: Convert to grayscale and apply median blur
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
img_blur = cv2.medianBlur(img_gray, 7)
# Step 4: Create edge mask using adaptive thresholding
img_edge = cv2.adaptiveThreshold(img_blur, 255,
cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY,
blockSize=9,
C=2)
# Step 5: Combine color image with edge mask
img_edge = cv2.cvtColor(img_edge, cv2.COLOR_GRAY2RGB)
img_cartoon = cv2.bitwise_and(img_color, img_edge)
# Display results
cv2.imshow("Original", img_rgb)
cv2.imshow("Cartoonized", img_cartoon)
# Save output if path provided
if output_path:
cv2.imwrite(output_path, img_cartoon)
cv2.waitKey(0)
cv2.destroyAllWindows()
return img_cartoon
# Usage example
cartoonized = cartoonize_image("input_image.jpg", "cartoon_output.jpg")
How It Works
Bilateral Filtering: This preserves edges while smoothing the image, reducing the number of colors to create a poster-like effect typical of cartoons.
Edge Detection: Adaptive thresholding finds edges in the blurred grayscale image, creating bold black outlines characteristic of cartoon drawings.
Combination: The bitwise AND operation merges the smoothed color image with the edge mask, producing the final cartoon effect.
Key Parameters
-
num_down=2: Controls image downsampling for faster processing -
num_bilateral=7: Number of bilateral filter applications -
blockSize=9: Size of neighborhood area for adaptive thresholding -
C=2: Constant subtracted from the mean in adaptive thresholding
Conclusion
OpenCV provides an effective way to cartoonize images using bilateral filtering and edge detection. The bilateral filter reduces colors while preserving edges, and adaptive thresholding creates bold outlines that give the cartoon appearance. This technique offers a free alternative to commercial cartoonization software.
