Cartoonize images with Raspberry PI and OpenCV

Check my RPI articles in Best Raspberry PI projects article or peppe8o.com home page. Or subscribe my newsletter (top right in this page) to be notified when new projects are available! Also interested to start 3D printing with a cheap budget? Visit my cheap 3D printers list
0
(0)

Beside its core functionalities on Artificial Intelligence, OpenCV can be used to manipulate images. With support of Python, you can use your Raspberry PI to transform every image in a cartoon image.

In this tutorial I’m going to show you how to create a script which cartoonize images with OpenCV on Raspberry PI.

OpenCV (Open Source Computer Vision Library) is an open source library used to manipulate and manage visual media, so delivering advanced computer vision and machine learning services. It runs on many platforms, including Linux, Windows, MacOS, iOS and Adroid.

Instead of a classic installation, you can also install OpenCV with Python support from default Raspberry PI package manager, This way you can get a fast OpenCV environment prefectly integrated with Python scripts.

This tutorial is focused on a very simple first example, where we are going to create cartoon images from a common photo. We will create a script which accepts input images filename (path) and saves the cartoonized version..

For this project I’m going to use a Raspberry PI 3 model A+, but this should work with all Raspberry PI boards.

What We Need

As usual, I suggest adding from now to your favourite ecommerce shopping chart all needed hardware, so that at the end you will be able to evaluate overall costs and decide if continuing with the project or removing them from shopping chart. So, hardware will be only:

Check hardware prices with following links:

Amazon raspberry pi boards box
Amazon Micro SD box
Amazon Raspberry PI Power Supply box

Step-by-Step Procedure

Prepare Operating System

Start preparing your Raspberry PI OS, with install Raspberry PI OS Lite guide (to have a fast, headless OS) or install Raspberry PI OS Desktop (getting a Desktop environment, so working with its internal terminal). I will use Lite distribution and I will move files by sftp session (with filezilla).

Make your OS up to date. From terminal:

sudo apt update -y && sudo apt upgrade -y

Install required packages:

sudo apt install python3-opencv python3-skimage

OpenCV Cartoonize Image Script

You can get a copy of this script directly in your Raspberry PI with following terminal command:

wget https://peppe8o.com/download/python/opencv_cartoonize.py

You can also explore script content with cat or nano commands. Please find belo script description.

I’ll use a Colosseum image, adding more images during script explaination showing what single commands are making on file. My file will have “colosseum.jpeg” name and will be passed to script with following command:

python3 opencv_cartoonize.py colosseum.jpeg

Original image one is the following one:

colosseum

Required libraries are imported. sys libary will be used to get terminal arguments (which will include filename of image to cartoonize)

import cv2
import sys
from skimage import io

Before starting image manipulation operations, using split function from sys library allows us to get filename separated from file extension. “sys.argv[1]” keep the first argument passed on terminal (“colosseum.jpeg” in my case). This process supposes that input image file doesn’t uses dot (“.”) char in its name and that the only dot char present is before file extension:

filename = sys.argv[1].split(".")

Now, filename variable is a string array with these values:

  • filename[0] = “colosseum”
  • filename[1] = “jpeg”

Following line simply load image file in “img” variable:

img = cv2.imread(sys.argv[1])

Next command converts image into a RGB array:

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

Image after this conversion is still the same:

colosseum image after opencv RGB conversion

Next lines convert colors to gray, also applying a Blur filter to create an out-of-focus effect:

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.medianBlur(gray, 5)

Please find in following picture outputs from this operation:

colosseum opencv gray blur filter

Following line detects main edges from image:

edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 9)

Following picture shows result:

colosseum opencv edges filter

Final image manipulation lines will extract inperpolation of colors from RGB image array and will use this inside boudaries marked from edges:

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

Next picture shows image resulting:

colosseum opencv cartoon

Final script line simply saves cartoonized image keeping same extension, but adding a “_cartoon” suffix to file name:

io.imsave(filename[0]+"_cartoon."+filename[1], cartoon)

Final Toughts

Cartoonize images with OpenCV is really simple and funny. Results can vary depending on image details and dimensions. Examples are following pictures:

OpenCV cartoonize final results comparisons

With default settings, working on high resolution images can bring in “too realistic” results like the one with Ferrari Formula 1 car (hi Charles!) An image with lower resolution, like the one with Raspberry PI board, gets better interpolation filtering.

Working on original file resizing plus leveraging Blur, edges and color filters can improve results with a wider set of images.

Enjoy!

How useful was this post?

Click on a star to rate it anonymously!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?