Have you ever wondered how to turn a regular image into a cartoon-like image? Well, today we will be diving into the world of computer vision to learn how to accomplish this task.
By using powerful libraries in Python, you can learn how to convert your personal or company logo into a cartoon that could be used for numerous applications. In this article, I will be teaching you step by step, in an easy-to-understand format, how to pre-process, turn your image into a cartoon, and customize the style of the outputted image.
Importing necessary modules
Before we dive in, let’s import the necessary modules. These modules are essential in performing image processing and editing.
Python has a plethora of libraries that provide tools for image manipulation, but we will be covering the following:
- OpenCV: A computer vision library that is widely used for image processing, scientific processing, and machine learning in python.
- Numpy: A python library used for scientific computing.
- Matplotlib: A python plotting library that provides tools for creating static, animated, and interactive visualizations in python.
Now let’s import these modules!
import cv2
import numpy as np
from matplotlib import pyplot as plot
Loading and plotting the original image
To get started, we need to load and plot the original image. In this article, we will be using a logo as our image.
We will be loading the image using cv2.imread
, which reads an image from a specified file path. We load the image in RGB format, to use with Matplotlib later.
Then, we will plot the image using imshow
with a title of “Original Image”.
# Load the image
image = cv2.imread("logo.png")
# Convert to RGB format
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Plot the image
plot.imshow(image)
plot.title("Original Image")
plot.show()
This portion of the code will result in the original image being plotted.
The image is displayed using the Matplotlib library, which provides various functions for plot customization.
Converting image to grayscale
Now that we have our original image loaded, we can pre-process it to turn it into a cartoon-like image. The first step in the pre-processing phase is to convert the image to grayscale.
Grayscale images are images that have 256 different shades of gray and no other color. To convert the image to grayscale, we use the cvtColor
method and pass in cv2.COLOR_BGR2GRAY
.
# Convert to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply median blur to smoothen the image
gray_image = cv2.medianBlur(gray_image, 5)
# Plot the image
plot.imshow(gray_image, cmap="gray")
plot.title("Grayscale Image")
plot.show()
We use the cv2.medianBlur
method to smoothen out any salt and pepper noise in the image. The output will be a gray-scale image with a higher quality, ready for edges detection.
Getting edged image
Now that we have the grayscale image, we need to generate an edged image. An edged image will identify the boundaries of the features in the image.
It can be achieved using different algorithms such as Canny, Gradients, and Laplacian filters.
In this article, we will be using an adaptive threshold method to generate an edged image.
The method is quite dynamic and uses calculation methods to find optimum thresholds for all regions in an image.
# Get edged image
edged_image = cv2.adaptiveThreshold(gray_image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 9)
# Plot the image
plot.imshow(edged_image, cmap="gray")
plot.title("Edged Image")
plot.show()
In the code block above, we use cv2.adaptiveThreshold
to perform this task.
Two adaptive threshold algorithms can be used: cv2.ADAPTIVE_THRESH_MEAN_C
and cv2.ADAPTIVE_THRESH_GAUSSIAN_C
. These algorithms calculate a local threshold for each pixel.
The threshold is computed based on the mean or Gaussian weighted sum of neighborhood pixel values.
Conclusion:
In this article, we covered everything you need to know about turning a regular image into a cartoon-like image.
We started by importing the necessary packages and continued on to load the image using cv2.imread. Then, we demonstrated how to convert the image to grayscale and how to obtain an edged image using adaptiveThreshold.
There are many different techniques available for image processing and editing, and learning this skill could be applied in numerous fields like advertising, graphic design, and social media marketing to mention a few. Now that you have learned the basics of transforming images into cartoons, I highly suggest testing your knowledge on different images alike.
Applying convolutional filter
Applying a convolutional filter to an image can drastically enhance the quality of an image. Convolutional filters use matrix calculations to sharpen, blur, or emboss an image.
The filter is passed over each pixel of the image and calculates a new value based on the filter’s kernel matrix.
In our task to turn an image to a cartoon type, we will use a bilateral filter.
A bilateral filter is quite suitable for this task as it reduces noise and smoothes the image while preserving its edges’ sharpness.
# Apply bilateral filter to the image
filtered_image = cv2.bilateralFilter(image, 9, 250, 250)
# Plot the filtered image
plot.imshow(filtered_image, cmap='gray')
plot.title('Filtered Image')
plot.show()
In the code above, we use cv2.bilateralFilter
to apply the bilateral filter to our original image and obtain our filtered image.
Using bitwise operation to turn image into cartoon
After we have our filtered image, we can use bitwise operations to turn the image into a cartoon. The operation is quite simple, we take the edged image previously obtained and apply it as a mask to our filtered image using a bitwise_and
operation.
# Apply bitwise and operation to the edged image and the filtered image
cartoon_image = cv2.bitwise_and(filtered_image, filtered_image, mask=edged_image)
# Plot the cartoon image
plot.imshow(cartoon_image, cmap='gray')
plot.title('Cartoon Image')
plot.show()
In this code block we use the cv2.bitwise_and
method to apply the mask previously generated and get our final cartoon image.
Displaying all images from original to final output
Now that we have our final output – our cartoon image, it is time to display all images from original to final output. This will allow us to see the different stages and how the image is processed.
# Plot all versions of the image
fig, axs = plot.subplots(1, 4, figsize=(15, 8))
axs[0].imshow(image)
axs[0].set_title('Original Image')
axs[1].imshow(gray_image, cmap='gray')
axs[1].set_title('Grayscale Image')
axs[2].imshow(edged_image, cmap='gray')
axs[2].set_title('Edged Image')
axs[3].imshow(cartoon_image, cmap='gray')
axs[3].set_title('Cartoon Image')
for ax in axs:
ax.axis('off')
plot.show()
In the code above, we use the subplots method from matplotlib library to create four subplots to display the original image, grayscale image, edged image, and cartoon image, respectively.
Recap and congratulating on learning
In this brief addition article, we discussed how to pre-process, apply convolutional filter, and bitwise operations. These are very powerful concepts that many image processing libraries use usually behind the scenes.
Understanding these basic techniques can allow you to create your own output figures and transform your images into more abstract and visually appealing designs. Congratulations on learning these few lines of code, and we wish you all the best.
In this article, we learned how to convert an image into a cartoon using Python’s OpenCV, NumPy, and Matplotlib libraries. We started by loading the image and pre-processing it by converting it to grayscale and getting an edged image using an adaptive threshold method.
Next, we applied a bilateral filter to smooth the image and preserve its edges. We then used bitwise operations to turn the image into a cartoon.
Finally, we displayed all the versions of the image from the original to the cartoon version. We hope that readers have learned the basic techniques of image processing that can help them generate their own output figures and transform their images into more visually appealing designs.
Remember to keep practicing and testing your skills on various images.