Learn how you can become a Python programmer in just 12 weeks.

    We respect your privacy. Unsubscribe at anytime.

    Create Cartoon Background in Webcam Stream using OpenCV

    What will we cover in this tutorial?

    How to create this effect.

    Create this effect in a few lines of code

    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.

    1. Capture a frame from the webcam.
    2. Get the foreground mask fg_mask.
    3. To get greater effect dilate the fg_mask.
    4. From the original frame, create a cartoon frame.
    5. 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.
    6. 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'):

    Python for Finance: Unlock Financial Freedom and Build Your Dream Life

    Discover the key to financial freedom and secure your dream life with Python for Finance!

    Say goodbye to financial anxiety and embrace a future filled with confidence and success. If you’re tired of struggling to pay bills and longing for a life of leisure, it’s time to take action.

    Imagine breaking free from that dead-end job and opening doors to endless opportunities. With Python for Finance, you can acquire the invaluable skill of financial analysis that will revolutionize your life.

    Make informed investment decisions, unlock the secrets of business financial performance, and maximize your money like never before. Gain the knowledge sought after by companies worldwide and become an indispensable asset in today’s competitive market.

    Don’t let your dreams slip away. Master Python for Finance and pave your way to a profitable and fulfilling career. Start building the future you deserve today!

    Python for Finance a 21 hours course that teaches investing with Python.

    Learn pandas, NumPy, Matplotlib for Financial Analysis & learn how to Automate Value Investing.

    “Excellent course for anyone trying to learn coding and investing.” – Lorenzo B.

    Leave a Comment