What will we cover in this tutorial?
How to create this effect.
The idea behind the code
The idea behind the above effect is simple. We will use a background subtractor, which will get the background of an image and make a mask of the foreground.
The it simple follows this structure.
- Capture a frame from the webcam.
- Get the foreground mask fg_mask.
- To get greater effect dilate the fg_mask.
- From the original frame, create a cartoon frame.
- Use the zero entries of fg_mask as index to copy the cartoon frame into frame. That is, it overwrites all pixel corresponding to a zero (black) value in fg_mask to the values of the cartoon in the original frame. That results in that we only get cartoon effect in the background and not on the objects.
- Show the frame with background cartoon effect.
The code you need to create the above effect
This is all done by using OpenCV. If you need help to install OpenCV I suggest you read this tutorial. Otherwise the code follows the above steps.
import cv2
backSub = cv2.createBackgroundSubtractorKNN(history=200)
cap = cv2.VideoCapture(0)
while True:
_, frame = cap.read()
fg_mask = backSub.apply(frame)
fg_mask = cv2.dilate(fg_mask, None, iterations=2)
_, cartoon = cv2.pencilSketch(frame, sigma_s=50, sigma_r=0.3, shade_factor=0.02)
idx = (fg_mask < 1)
frame[idx] = cartoon[idx]
cv2.imshow('Frame', frame)
cv2.imshow('FG Mask', fg_mask)
keyboard = cv2.waitKey(1)
if keyboard == ord('q'):
break
Learn Python

Learn Python A BEGINNERS GUIDE TO PYTHON
- 70 pages to get you started on your journey to master Python.
- How to install your setup with Anaconda.
- Written description and introduction to all concepts.
- Jupyter Notebooks prepared for 17 projects.
Python 101: A CRASH COURSE
- How to get started with this 8 hours Python 101: A CRASH COURSE.
- Best practices for learning Python.
- How to download the material to follow along and create projects.
- A chapter for each lesson with a description, code snippets for easy reference, and links to a lesson video.
Expert Data Science Blueprint

Expert Data Science Blueprint
- Master the Data Science Workflow for actionable data insights.
- How to download the material to follow along and create projects.
- A chapter to each lesson with a Description, Learning Objective, and link to the lesson video.
Machine Learning

Machine Learning – The Simple Path to Mastery
- How to get started with Machine Learning.
- How to download the material to follow along and make the projects.
- One chapter for each lesson with a Description, Learning Objectives, and link to the lesson video.